可以使用PHP中的动态生成图像吗? [英] Does html can be use with dynamic generated images in php?

查看:116
本文介绍了可以使用PHP中的动态生成图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码创建图片

 <?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文件:


  1. 一个用于图片本身

  2. 另一个用于PHP显示的文件。

让我们来看看你是这样做的:


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


    为什么你必须这样做?因为要生成图像,您不能将其与其他HTML输出混合使用。更不用说一个空间返回,因为这样会导致图片损坏。



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

  3. 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:

  1. one for the image itself
  2. another one for PHP to display it.

Let's take a look at to you do it:

  1. You create image.php that accept parameter, like: image ID or file name. For security reason, you HAVE to filter whatever parameter it get.

    Why you have to do this? because, to generate image, you can't mix it with another HTML output. Let alone a single space or return as this will render the image broken.

  2. You do the HTML thing on another PHP, say test92.php. To the HTML logic here, like:

    1. get image data
    2. loop the data
    3. display image => <img src="image.php?imageID=12" alt="" />

这篇关于可以使用PHP中的动态生成图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆