gpt4 book ai didi

PHP imagejpeg() 显示奇怪的字符

转载 作者:行者123 更新时间:2023-12-04 20:49:58 25 4
gpt4 key购买 nike

问题:

我找到了这个解释如何使用 imagejpeg() 的简单脚本:

<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>

当我将它复制/粘贴到一个空的 php 文件中时,它工作正常。我显示图像。

现在,当我将这段代码插入到我现有的 php 文件中时,格式如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

***code above***

</body>
</html>

它不返回任何图像。只是几行奇怪的字符。

我认为发生了什么:

我阅读了其他帖子,我认为问题出在我的主标题“Content-Type”上。但我不明白为什么嵌入代码中的“Content-Type: image/jpeg”不会取代主标题中的任何内容?它应该告诉 imagejpeg() 显示图像,而不是文本...

有人知道怎么解决吗?谢谢。

最佳答案

你也可以这样做。

<?php

$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);

ob_start ();

imagejpeg($im);
imagedestroy($im);

$data = ob_get_contents ();

ob_end_clean ();

$image = "<img src='data:image/jpeg;base64,".base64_encode ($data)."'>";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<?php echo $image; ?>

</body>
</html>

对我有用。

关于PHP imagejpeg() 显示奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19486445/

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