所以我创建了一个基于输入返回引号的网站。我希望报价作为图片上的文字返回,所以我在http://jonlim.ca/2011/11/creating-an-image-with-dynamic-text-in-php/找到了一位向导来帮助我。
我正在运行xampp来测试我的php并且代码目前看起来像......
<?php
header("Content-Type: image/png");
/*$string = $_GET['days'];*/
$im = imagecreatefrompng("twainpen.png");
imagealphablending($im, false);
imagesavealpha($im,true);
$transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagepng($im);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'daniel.ttf';
$initialX = '153';
$initialY = '35';
$increaseY = '23';
$fontSize = '16';
$fontRotation = '0';
$firstline = 'What do you mean';
$secondline = 'there are ' . $string . ' days until';
$thirdline = 'Christmas?! What am';
$fourthline = 'I supposed to do until';
$fifthline = 'then? WAIT?! I am';
$sixthline = 'not a patient man!';
//imagestring($im, $font, $px, 20, $string, $black);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY, $black, $font, $firstline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+$increaseY, $black, $font, $secondline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*2), $black, $font, $thirdline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*3), $black, $font, $fourthline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*4), $black, $font, $fifthline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*5), $black, $font, $sixthline);
imagepng($im);
imagedestroy($im);
?>
这段代码与指南几乎完全相同,真正令人沮丧的是,几周前我上次工作的时候我开始工作了,但是在我开始使用其他东西后,我又回来了,它不会&# 39;不再工作了(所有发生的事情就是图片显示没有任何文字)。我打赌解决方案是我做的一些愚蠢的错误,但不确定它是什么。字体与代码和图片位于同一文件夹中。
任何可以给予的帮助或想法都表示赞赏!谢谢。
答案 0 :(得分:0)
首先你必须删除第8行。它在添加文本之前输出图像。然后正确设置字体的路径。
这里描述:
http://php.net/manual/en/function.imagettftext.php
根据PHP使用的GD库版本,当fontfile不以前导/开头时,然后.ttf将附加到文件名,库将尝试沿库定义的字体路径搜索该文件名
<?php
header("Content-Type: image/png");
$string = $_GET['days'];
$im = imagecreatefrompng("twainpen.png");
imagealphablending($im, false);
imagesavealpha($im,true);
$transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
$black = imagecolorallocate($im, 0, 0, 0);
$font = './daniel.ttf';
$initialX = '153';
$initialY = '35';
$increaseY = '23';
$fontSize = '16';
$fontRotation = '0';
$firstline = 'What do you mean';
$secondline = 'there are ' . $string . ' days until';
$thirdline = 'Christmas?! What am';
$fourthline = 'I supposed to do until';
$fifthline = 'then? WAIT?! I am';
$sixthline = 'not a patient man!';
//imagestring($im, $font, $px, 20, $string, $black);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY, $black, $font, $firstline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+$increaseY, $black, $font, $secondline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*2), $black, $font, $thirdline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*3), $black, $font, $fourthline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*4), $black, $font, $fifthline);
imagettftext($im, $fontSize, $fontRotation, $initialX, $initialY+($increaseY*5), $black, $font, $sixthline);
imagepng($im);
imagedestroy($im);
?>
答案 1 :(得分:0)
Payam Mohammadi用简单的解决方案解决了我的问题:“在标题之前添加:error_reporting(0);”
imagepng($ im);也必须取出线。