2014-10-07 13 views
0

我使用GD库,我试图添加一个HTML图像使用PHP回声。gd库 - 图像上的文本作为回声与html

<?php 
$image = imagecreatefrompng("grafika.png"); 
$black = imageColorAllocate($image, 255, 255, 255); 
$font = 'arial.ttf'; 
$text = 4; 
if($text == 4) { 
     echo '<div style="background-color: yellow; width: 100px; height: 100px;">some text here</div>'; 
    } 
imagettftext($image, 16, 0, 50, 100, $black, $font, $text); 
header("Content-type: image/png"); 
imagepng($image); 
?> 

我在做什么错? 有没有不同的方式来做到这一点?

回答

1

你有两个问题。您将html转储到输出流中,然后使用.png图像的原始二进制内容跟随该html。

想必你已经有了

<img src="yourscript.php"> 

,这意味着你要发送HTML到只有二值图像所允许的范围内。并以某种方式期待浏览器知道您发送HTML 图像,并以某种方式能够将它们分开。

由于您正在执行该回声,因此html会成为图像的一部分,并且生成的图像将被几乎所有浏览器视为已损坏。他们期待一个PNG,它具有非常特定的标题格式。 HTML将不符合该格式,所以显然图像已损坏。

0

如果你被允许嵌入这里一些文字到图像比你可以使用imagefilledrectangle功能:

<?php 
$image = imagecreatefrompng("grafika.png"); 
$black = imageColorAllocate($image, 255, 255, 255); 
$font = 'arial.ttf'; 
$text = 4; 
if($text == 4) { 
     imagefilledrectangle($image, 16, 0, 0, 50, 100, rgbcolorallocate($image, 255, 255, 0)); 
     imagettftext($image, 16, 0, 0, 0, $black, $font, 'some text here'); 
    } 
imagettftext($image, 16, 0, 50, 100, $black, $font, $text); 
header("Content-type: image/png"); 
imagepng($image); 
?> 

您也可以使用imagettfbbox函数来计算文本的边框。