Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让PHP脚本作为img源代码工作?_Php_Html_Image Processing - Fatal编程技术网

如何让PHP脚本作为img源代码工作?

如何让PHP脚本作为img源代码工作?,php,html,image-processing,Php,Html,Image Processing,我想在电子邮件上跟踪一些使用情况统计数据,并考虑将我使用的图像放入跟踪脚本中,然后将其添加到电子邮件中 但是,我不能让它工作。我的守则如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> </head> <body> <img src="test.php"> </body&

我想在电子邮件上跟踪一些使用情况统计数据,并考虑将我使用的图像放入跟踪脚本中,然后将其添加到电子邮件中

但是,我不能让它工作。我的守则如下:

<!DOCTYPE html>
<html>
<head>
     <meta charset="utf-8"/>
    <title></title>
</head>
<body>
<img src="test.php">
</body>
</html>

PHP code test.PHP:

  <?php
 // The image to return
$image=imagecreatefrompng("images/dl-front.png");

// ** Output image to browser
header('Content-type: image/png');
imagejpeg($image);

// ** Destroys the image
imagedestroy($image);
?>

我得到的图像链接如下所示:


编辑:这个答案是在OP编辑之前给出的

我不确定,但您正在创建一个.png图像,然后将使用
标题(“Content-type:image/jpeg”)显示该图像
so作为.jpg

所以我认为如果你想显示一个.png图像,你必须使用

header('Content-type:image/png')

您可以使用以下PHP脚本来解决问题:

$image = 'images/dl-front.png';
if(!file_exists($image)){
    die("File does not exist");
}
header('Content-Type:image/png');
header('Content-Length: ' . filesize($image));
header('Content-disposition: filename="dl-front.png"');
readfile($image);

很难说不知道你是如何发送那封电子邮件的。img源应该包含一个HTTP地址。如果它是本地的,那么外部资源将无法触发它。错误报告显示了什么吗?我正要在这里发布一个答案,但我想我会在评论中这样说。这里使用的是
.html
文件。您是否指示服务器将其视为PHP?如果没有,它应该拥有一个
.php
扩展名。再试一次。或者,也许我错了。你有2条评论,下面是答案,请看。如果我不在问题中,请将我回复。@FunkFortyNiner控制台中没有错误。我首先在本地envir中尝试这一点。您应该首先调试test.php脚本(确保它工作),直接运行它()。然后尝试用img标签加载它。我们都可以继续猜测,但在某个时候,你必须亲自调查这一点。“我添加了适当的标签并进行了回应,然后图像出现了”-是什么?我之前问过错误,但你没有告诉我们上面的错误。你在哪里看到
标题('Content-type:image/jpeg')?很公平。我编辑了你的答案,只是为了让每个人都知道。为拼写错误道歉。确保图像文件存在。现在对我有用,没有兄弟。不为我工作。我已经添加了
标题('Content-disposition:filename=“dl-front.png”)
PHP
脚本中。它现在正在使用
HTML
tag
@ahmednumamaan进行工作,这两者都不应该是necessary@WalterTross我也有同样的想法,但如果我没有这个想法,它就不起作用了。