【问题标题】:Does html can be use with dynamic generated images in php?html 可以与 php 中的动态生成图像一起使用吗?
【发布时间】:2011-10-25 07:35:43
【问题描述】:

我正在使用此代码创建图像

<?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);
  ?>

如果我注释行号“A”和“B”,代码可以正常工作,它会在浏览器上生成带有测试的图像。但我希望图像位于 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.">

【问题讨论】:

  • 您必须了解 HTML 是什么以及它是如何工作的。无意冒犯,只是一个友好的建议。
  • @Col.Shrapnel 感谢您的建议。

标签: php html gd


【解决方案1】:

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

  1. 图像本身的一个
  2. 另一个供 PHP 显示。

我们来看看怎么做:

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

    为什么你必须这样做?因为,要生成图像,您不能将它与另一个 HTML 输出混合。更不用说单个spacereturn,因为这会使图像损坏。

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

    1. 获取图片数据
    2. 循环数据
    3. 显示图片 => &lt;img src="image.php?imageID=12" alt="" /&gt;

【讨论】:

    【解决方案2】:

    如果你想要一个 div 围绕你的图像,你必须在 html 中这样做,你不能在图像生成代码中这样做

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

    如果您收到有关图片的错误,请尝试浏览图片 url http://localhost/php/test92.php 并查看它的外观。

    它是否显示了您所期望的图像?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多