将文本与图像合并


Merge text with image

我正在尝试将文本字符串与图像合并。我在Stackoverlowo上找到了如何做到这一点的例子,但当我加载脚本时,我得到的只是一个jibberish。我认为这是一个字体问题,但我不知道如何解决这个问题。我已经将True类型字体(arial)放在与脚本相同的目录中。建议?这是我的代码:

<?php

$im = imagecreatefromjpeg('image.jpg');  
//The numbers are the RGB values of the color you want to use 
$black = ImageColorAllocate($im, 255, 255, 255); 
//The canvas's (0,0) position is the upper left corner 
//So this is how far down and to the right the text should start 
$start_x = 10; 
$start_y = 20; 
$font = 'arial.ttf';

Imagettftext($im, 12, 0, $start_x, $start_y, $black, $font, 'text to write'); 
//Creates the jpeg image and sends it to the browser 
//100 is the jpeg quality percentage 
Imagejpeg($im, '', 100); 
ImageDestroy($im)
?>

您并没有告诉浏览器您实际上是在向它发送图像,所以它试图将二进制数据解释为文本。

为了让浏览器知道它即将接收图像,你必须向它发送一个内容类型。你需要把这行放在imagejpeg()之前。

header('Content-Type: image/jpeg');

根据文档,如果不保存到文件,imagejpeg()的第二个参数应该为NULL,而不是空字符串:

Imagejpeg($im, NULL, 100);