0

我一直在用这个扯掉我的头发,并在这里尝试了很多很多解决方案,但无济于事。

我正在尝试向图像添加一些文本,但它所做的只是显示我的背景图像,这里有什么明显的地方我做错了吗?

提前致谢

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

$fbid = $_POST['fbid'];
$background_img = $_POST['background'];
$message = $_POST['text'];
$ts = $_POST['ts'];

$filename = $fbid . "-" . $ts . ".jpg";

$image_canvas = imagecreatetruecolor(640,400);

$background = imagecreatefromjpeg($background_img);
$overlay    = imagecreatefrompng("../images/image-overlay.png");

imagecopyresampled($background, $overlay, 0, 0, 0, 0, imagesx($overlay), imagesy($overlay), imagesx($overlay), imagesy($overlay));

imagefilledrectangle($image_canvas, 0,0,150,30, $background);

$white = imagecolorallocate($background, 255, 255, 255);

imagettftext($image_canvas, 25, 0, 50, 50, $white, "arial.TTF", $message);

imagejpeg($background,"../created/" . $filename, 100);

imagedestroy($background);
4

2 回答 2

0

你错过了画布。使用imageCreateTrueColor开始构建。

$imageCanvas = imageCreateTrueColor($width, $height);
//your code
$background = imagecreatefromjpeg($background_img);
//more of your code
imagefilledrectangle($imageCanvas, 0, 0, 150, 30, $background);
//now do the same for the text only us imag
imagettftext($imageCanvas, 25, 0, 50, 50, $white, "arial.TTF", $message);

您在 $imageCanvas 上合并 jpeg 和文本元素。

于 2013-06-18T16:38:36.837 回答
0

看看这个。它有效,页面链接如下。根据您的原始帖子,我相信这就是您想要的。

 /* first composite the canvas with the background */
 $background_img="../img/adfuba_october.png";
 $compositeString = "composite.png";

 list($width,$height) = getimagesize($background_img);
 $image_canvas = imagecreatetruecolor($width,$height);
 $background = imagecreatefrompng($background_img);
 imagecopyresampled($image_canvas,$background,0,0,0,0,$width,$height,$width,$height);

 /* now add the text */
 $fontPath = "path/to/your/fontFile/ARIAL.TTF";
 $fontSize = 24;
 $percent = 0.25;
 $txt_x = abs($width*$percent);
 $txt_y = abs($height*$percent);
 $color = "008844";
 $message = "This is User Text";
 imageTTFtext($image_canvas, $fontSize, 0, $txt_y, $txt_y, $color, $fontPath, $message);

 /* now generate the file */
 imagepng($image_canvas, $compositeString, 0) or die("error saving png");

 ?>
 <p>This is a composite image:<br><img src="<?php echo $compositeString;?>"></p>

您可以在此处查看合成图像。几件事情要记住。即使 TrueType 字体文件与脚本位于同一目录中,TrueType 字体的路径也应该是绝对路径。

此外,画布是您的背景对象,然后您将图像或文本叠加在画布之上。

最后,(你可能已经明白了)你的分层元素是从画布到文本的顺序依赖。意思是画布->背景->另一个图形->然后是文本。否则,您最终可能会掩盖要在前面渲染的元素。希望有帮助。

于 2013-06-19T13:46:25.973 回答