【问题标题】:converting html to image not working [closed]将html转换为图像不起作用[关闭]
【发布时间】:2013-10-23 06:11:31
【问题描述】:

我使用这个脚本通过 php-gd 库将 html 代码制作成图像:

<?php
    //put your html code here 
    $html_code = ' 
    <html> 
        <body> 
            Image Out Put sadadasdasdasdasdasd
        </body> 
    </html>
    '; 

    $img = imagecreate("300", "600"); 
    imagecolorallocate($img,0,0,0); 
    $c2 = imagecolorallocate($img,70,70,70); 
    imageline($img,0,0,300,600,$c2); 
    imageline($img,300,0,0,600,$c2); 

    $white = imagecolorallocate($img, 255, 255, 255); 
    imagettftext($img, 9, 0, 1, 1, $white, "arial.ttf", $html_code); 

    header('Content-type: image/jpeg'); 
    imagejpeg($img); 

?>

网站网址:

http://almogas.com/temp/phpimage.php?id=3

我得到了很多未知字符。如何解决此问题?

///////////////////////////////////////

编辑

现在删除空格后可以正常工作

但是有办法添加背景图片吗? 谢谢

【问题讨论】:

  • unknown char 是什么意思?您希望它如何工作?
  • Content-Type 标头未正确发送。文档的 Content-Type 为 text/html
  • OP 正在尝试创建一个不正确的图像Content-Type@Le_Morri
  • 这就是我要说的。他给定的代码是正确的。但是服务器没有这样做。
  • @putvande 这行得通。你检查输出了吗?

标签: php html image php-gd


【解决方案1】:
  • 确保您的服务器上启用了 GD 库。
  • 您应该使用 png 而不是 jpg,因为 png 可以产生更好的文本质量
  • 你应该在 imagejpeg 或 imagepng 之后使用 imagedestroy 来清理内存

【讨论】:

    【解决方案2】:

    请确保您的文件开头没有空格或多余的字符。 重要的是它以

    开头
    <?
    

    你可以避免关闭

    ?>
    

    这样您就可以确保不会向客户端发送任何其他不需要的输出。

    【讨论】:

    • 谢谢大家。现在有办法添加bg图像吗?谢谢
    【解决方案3】:

    &lt;? 之前有不需要的字符。删除&lt;?之前的所有空白行和空格,否则您的图像是正确的。

    &lt;? 必须是脚本第一行的第一个字符。你可以避免?&gt;如果你使用它,它必须是最后一行最后一个字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 2015-05-05
      • 2019-12-16
      相关资源
      最近更新 更多