如何返回图像
我想用 php 脚本显示图像。这是我当前的代码:
<?php
if (isset ($_GET['id'])) $id = $_GET['id'];
$t=getimagesize ($id) or die('Unknown type of image');
switch ($t[2])
{
case 1:
$type='GIF';
$img=imagecreatefromgif($path);
break;
case 2:
$type='JPEG';
$img=imagecreatefromjpeg($path);
break;
case 3:
$type='PNG';
$img=imagecreatefrompng($path);
break;
}
header("Content-type: image/".$type);
echo $img;
?>
但它不显示图像。什么是代替 echo $img
的正确方法?
I want to show image with php script. Here is my current code:
<?php
if (isset ($_GET['id'])) $id = $_GET['id'];
$t=getimagesize ($id) or die('Unknown type of image');
switch ($t[2])
{
case 1:
$type='GIF';
$img=imagecreatefromgif($path);
break;
case 2:
$type='JPEG';
$img=imagecreatefromjpeg($path);
break;
case 3:
$type='PNG';
$img=imagecreatefrompng($path);
break;
}
header("Content-type: image/".$type);
echo $img;
?>
But it doesn't show the image. What is the right way instead of echo $img
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每种格式都有对应的函数。
在您的情况下,您可以添加:
There are functions for each format.
In your case you could add:
我之前使用过 echo() 来实现此目的并且它有效。但请尝试使用
imagejpeg()
函数代替。另外,请确保脚本中的图像之前或之后没有输出任何其他内容。一个常见的问题是由于
和
?>
标记之前或之后的空格和换行导致输出空格和换行。您需要删除所有这些。并检查通过include()
或requre()
加载的任何其他 PHP 代码是否有相同的情况。I've used
echo()
for this before and it's worked. But try usingimagejpeg()
function instead.Also, make sure you don't have any other content being output either before or after the image in your script. A common problem is blank spaces and line feeds being output caused by space and line feeds before or after the
<?php
and?>
tags. You need to remove all of these. And check any other PHP code loaded viainclude()
orrequre()
for the same thing.就像这样:
just like this :