dsgwdigu84950 2014-11-21 13:04
浏览 99
已采纳

创建文本到图像以通过函数调用在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
  imagejpeg($jpg_image);
  // Clear Memory
  imagedestroy($jpg_image);
 }

I am calling the function as follows:

Print ('<img itemprop="image" src="');
txt_to_png($my_text_input);
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
���C�     

 $.' " ,#(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 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]);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考