问题描述
我使用此代码创建图片
<?php
//设置内容类型
头('Content-Type:image / png');
//创建图片
$ im = imagecreatetruecolor(400,30);
//创建一些颜色
$ white = imagecolorallocate($ im,255,255,255);
$ gray = imagecolorallocate($ im,128,128,128);
$ black = imagecolorallocate($ im,0,0,0);
imagefilledrectangle($ im,0,0,399,29,$ white);
//要绘制的文本
$ text ='测试...';
//用你自己的字体路径替换路径
$ font ='arial.ttf';
//为文本添加一些阴影
imagettftext($ im,20,0,11,21,$ gray,$ font,$ text);
//添加文本
imagettftext($ im,20,0,10,20,$ black,$ font,$ text);
//与imagejpeg()相比,使用imagepng()可以得到更清晰的文本()
(A)print('< div class =test>');
imagepng($ im);
print('< / div>');
(B)imagedestroy($ im);
?>
如果我注释行号A和B并且它生成浏览器上带有测试的图像。但我想要图像在div中。所以我取消注释行(A)和(B),但它没有给出正确的输出。生成的html也是奇怪的生成的html是
< img src =http://localhost/php/test92.php alt =图片http://localhost/php/test92.php无法显示,因为它包含错误。>
基本上,要在HTML中创建动态图像,需要2个PHP文件:
- 一个用于图片本身
- 另一个用于PHP显示的文件。
让我们来看看你是这样做的:
-
创建接受参数的
image.php
,如:图像ID或文件名。出于安全原因,你必须过滤它得到的任何参数。 -
你在另一个PHP上做HTML事情,比如
test92.php
。这里的HTML逻辑如下: >
- display image =>
< img src =image.php?imageID = 12alt =/>
I am using this code to create an image
<?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);
?>
The code work fines if i comment the line number 'A' and 'B' and it generates the image on the browser with testing written on it. But i want the image to be in a div. so i uncomment the line (A) and (B) but it is not giving right output. The generated html is also strange generated html is
<img src="http://localhost/php/test92.php" alt="The image "http://localhost/php/test92.php" cannot be displayed, because it contains errors.">
Basically, to create dynamic image in HTML, you will need 2 PHP files:
- one for the image itself
- another one for PHP to display it.
Let's take a look at to you do it:
You create
image.php
that accept parameter, like: image ID or file name. For security reason, you HAVE to filter whatever parameter it get.You do the HTML thing on another PHP, say
test92.php
. To the HTML logic here, like:- get image data
- loop the data
- display image =>
<img src="image.php?imageID=12" alt="" />
这篇关于可以使用PHP中的动态生成图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!