gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:07:20 26 4
gpt4 key购买 nike

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

<?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 中创建动态图像,您需要 2 个 PHP 文件:

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

让我们来看看如何做到这一点:

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

    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. 你在另一个 PHP 上做 HTML 的事情,比如说 test92.php .对于此处的 HTML 逻辑,例如:

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

关于php - html 是否可以与 php 中动态生成的图像一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7886064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com