简体   繁体   English

html可以与php中的动态生成图像一起使用吗?

[英]Does html can be use with dynamic generated images in php?

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. 如果我对行号“A”和“B”进行注释,则代码工作会很精细,并且它会在浏览器上生成图像,并在其上写入测试。 But i want the image to be in a div. 但我希望图像在div中。 so i uncomment the line (A) and (B) but it is not giving right output. 所以我取消注释行(A)和(B)但它没有给出正确的输出。 The generated html is also strange generated html is 生成的html也是奇怪生成的html

<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: 基本上,要在HTML中创建动态图像,您将需要2个PHP文件:

  1. one for the image itself 一个用于图像本身
  2. another one for PHP to display it. 另一个用于PHP显示它。

Let's take a look how to do it: 我们来看看如何做到这一点:

  1. You create image.php that accept parameter, like: image ID or file name. 您创建接受参数的image.php ,如:image ID或文件名。 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. 因为,要生成图像,您不能将其与另一个HTML输出混合。 Let alone a single space or return as this will render the image broken. 更不用说单个spacereturn因为这会使图像破碎。

  2. You do the HTML thing on another PHP, say test92.php . 你在另一个PHP上执行HTML操作,比如test92.php To the HTML logic here, like: 这里的HTML逻辑,如:

    1. get image data 获取图像数据
    2. loop the data 循环数据
    3. display image => <img src="image.php?imageID=12" alt="" /> display image => <img src="image.php?imageID=12" alt="" />

If you want a div around your image you have to do that in the html, you can't do that in the image generation code 如果你想在你的图像周围使用div,你必须在html中这样做,你不能在图像生成代码中这样做

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

If you are getting errors regarding the image, try browsing the image url http://localhost/php/test92.php and see what it looks like. 如果您收到有关图像的错误,请尝试浏览图片网址http://localhost/php/test92.php并查看其外观。

Does it show an image like you are expecting? 它是否显示出您期望的图像?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM