2011-10-25 85 views
0

我使用这个代码,如果我评论的行号“A”和“B”来创建图像
是否可以在PHP中使用动态生成的图像?

<?php 

// Set the content-type 
header('Content-Type: image/png'); 

// Create the image 
$im = imagecreatetruecolor(400, 30); 

// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 399, 29, $white); 

// The text to draw 
$text = 'Testing...'; 
// Replace path by your own font path 
$font = 'arial.ttf'; 

// Add some shadow to the text 
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 

    // Add the text 
    imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

    // Using imagepng() results in clearer text compared with imagejpeg() 
    (A)print ('<div class="test">'); 
    imagepng($im); 
    print ('</div>'); 
    (B)imagedestroy($im); 
    ?> 

的代码工作的罚款和它产生在写有测试浏览器的图像。但我想要图像在div中。所以我取消注释行(A)和(B),但它没有给出正确的输出。生成的HTML也怪生成的html是

<img src="http://localhost/php/test92.php" alt="The image “http://localhost/php/test92.php” cannot be displayed, because it contains errors."> 
+2

你必须学习HTML是什么,它是如何工作。没有冒犯,只是一个友好的建议。 –

+0

@ Col.Shrapnel感谢您的建议。 –

回答

1

基本上,创建HTML动态图像,则需要2个PHP文件:

  1. 一个图像本身
  2. 另一个用于PHP来显示它。

让我们来看看你做到这一点:

  1. 创建image.php接受参数,如:图像ID或文件名。出于安全原因,你必须过滤它得到的任何参数。

    为什么你必须这样做?因为要生成图像,您不能将其与其他HTML输出混合使用。更不用说单一的spacereturn,因为这会使图像断裂。

  2. 你在另一个PHP上做HTML的事情,比如test92.php。到HTML的逻辑在这里,如:

    1. 获取图像数据
    2. 循环中的数据
    3. 显示图像=><img src="image.php?imageID=12" alt="" />
+0

非常感谢您的帮助。我正在找那个。 –

1

如果你想在你的形象一个div你必须做的是,在HTML中,你不能这样做,在图像生成代码

<div> 
<img src="http://localhost/php/test92.php"> 
</div> 

如果您收到有关图片的错误,请尝试浏览图片url http://localhost/php/test92.php,看看它是什么样子。

它显示出像你期待的图像吗?

相关问题