2014-11-21 13:04 阅读 92

创建文本到图像以通过函数调用在html src中呈现

I'm trying to render an image generated by a function call in php.

I've copy pasted a function found at php for kids and found elsewhere here.

 function txt_to_png($textin){
  //Set the Content Type
  header('Content-type: image/jpeg');
  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('../images/100.jpg');
  // Allocate A Color For The Text
  $white = imagecolorallocate($jpg_image, 255, 255, 255);
  // Set Path to Font File
  $font_path = '../fonts/lato-regular.ttf';;
  // Set Text to Be Printed On Image
  $text = "This is a sunset!"; // just hardcoding for moment
  // Print Text On Image
  imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
  // Send Image to Browser
  // Clear Memory

I am calling the function as follows:

Print ('<img itemprop="image" src="');
Print ('">');

but it's not rendering the image correctly:

<img itemprop="image" src="�����JFIF���������&gt;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality

 $.' " ,#(7),01444'9="82<.342���C" ="" ="" 2!!22222222222222222222222222222222222222222222222222����d�d"��������������="" �������}�!1aqa"q2���#b��r��$3br�="" %&'()*456789:cdefghijstuvwxyzcdefghijstuvwxyz�����������������������������������������������������������������������������������="" ������w�!1aqaq"2�b����="" #3r�br�="" $4�%�&'()*56789:cdefghijstuvwxyzcdefghijstuvwxyz���������������������������������������������������������������������������="" ��?��j(��="" (��="" (��?��"="">

Thanks for any help! Bill

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dras2334 dras2334 2014-11-21 13:09

    Your txt_to_png function is creating an image as a binary code. You need to add src="somefile.php?textin=abcd and in the somefile.php call your function with parameter txt_to_png($_GET['textin]);

    点赞 评论 复制链接分享