使用 PHP GD 库来调整大小和保存图像是地狱
我正在编写一个脚本,该脚本将从用户输入上传文件,将其大小调整为缩略图并将两个新文件名添加到数据库中。
然而,我一生都无法弄清楚如何让 PHP 检测图像的 MIME 类型,然后将其提供给标头。这是代码,我已经添加了注释,试图使其尽可能清晰:
$picture = $_FILES['picture']['name'];
/*original file location*/
$file = 'picture/'.$picture.'';
/*save thumbnail location*/
$save = 'thumb/tn-'.$picture.'';
/*append thumbnail filename with tn-*/
$thumb = 'tn-'.$picture.'';
/*get original file size*/
list($width, $height) = getimagesize($file);
/*get image MIME type*/
$size = getimagesize($file);
$fp = fopen($file, "r");
if ($size && $fp)
{
header("Content-type:".$size['mime']);
fpassthru($fp);
exit;
}
else
{
echo 'Error getting filetype.';
}
/*define thumbnail dimensions*/
$modwidth = 300;
$modheight = 200;
/*check to see if original file is not too small*/
if (($width > 301) || ($height > 201))
{
/*create shell for the thumbnail*/
$tn = imagecreatetruecolor($modwidth, $modheight);
/*HERE IS WHERE I HAVE TROUBLE*/
/*if MIME type is PNG*/
if ($size['mime'] == "image/png")
{
/*try to create PNG thumbnail*/
$image = imagecreatefrompng($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagepng($tn, $save, 100);
}
/*if MIME type is JPEG*/
if ($size['mime'] == "image/jpeg")
{
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 100);
}
/*if MIME type is GIF*/
if ($size['mime'] == "image/gif")
{
$image = imagecreatefromgif($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagegif($tn, $save, 100);
}
}
else { echo 'Your file is too small.'; }
所以这是我不理解的部分:当我上传 .jpeg 时,代码工作正常,但如果它是 PNG 或一个 GIF,它会弹出一个页面,显示“图像‘127.0.0.1:8888’无法显示,因为它包含错误。”我认为它一定没有正确的标头 MIME 类型,但它可能是其他类型吗?
当我选择 .jpeg 时,它会很好地上传到我的图像文件夹,并按照我想要的方式在我的缩略图文件夹中生成缩略图。
但当我尝试使用 PNG 和 GIF 时,它就失败了。我一定错过了一些明显的东西?
这段代码对我想要完成的任务有什么好处吗?
提前致谢,
I'm writing a script that will upload a file from user input, resize it to a thumbnail and add the two new filenames to a database.
However, I cannot for the life of me figure out how to get PHP to detect the image's MIME type and then give it to the header. Here is the code, I've put comments to try and make it as clear as possible:
$picture = $_FILES['picture']['name'];
/*original file location*/
$file = 'picture/'.$picture.'';
/*save thumbnail location*/
$save = 'thumb/tn-'.$picture.'';
/*append thumbnail filename with tn-*/
$thumb = 'tn-'.$picture.'';
/*get original file size*/
list($width, $height) = getimagesize($file);
/*get image MIME type*/
$size = getimagesize($file);
$fp = fopen($file, "r");
if ($size && $fp)
{
header("Content-type:".$size['mime']);
fpassthru($fp);
exit;
}
else
{
echo 'Error getting filetype.';
}
/*define thumbnail dimensions*/
$modwidth = 300;
$modheight = 200;
/*check to see if original file is not too small*/
if (($width > 301) || ($height > 201))
{
/*create shell for the thumbnail*/
$tn = imagecreatetruecolor($modwidth, $modheight);
/*HERE IS WHERE I HAVE TROUBLE*/
/*if MIME type is PNG*/
if ($size['mime'] == "image/png")
{
/*try to create PNG thumbnail*/
$image = imagecreatefrompng($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagepng($tn, $save, 100);
}
/*if MIME type is JPEG*/
if ($size['mime'] == "image/jpeg")
{
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 100);
}
/*if MIME type is GIF*/
if ($size['mime'] == "image/gif")
{
$image = imagecreatefromgif($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagegif($tn, $save, 100);
}
}
else { echo 'Your file is too small.'; }
So here is the part that I do not understand: the code works fine for when I upload a .jpeg, but if it is a PNG or a GIF, it brings up a page that says, 'The image '127.0.0.1:8888' cannot be displayed because it contains errors.' I'm supposing it must not have the correct Header MIME type, but could it be something else?
When I select a .jpeg, it uploads just fine to my image folder and it generates a thumbnail to my Thumbnail folder like I want it to.
But as soon as I try it with PNG and GIF, it fails. I must be missing something obvious?
Is this code any good for what I'm trying to accomplish?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 PHP 的 GD 扩展确实很痛苦。就我个人而言,我建议采用像 WideImage 这样的抽象库。这将大大简化您的代码:
是的,上面的所有代码都减少到四个通道。
几点提示:
$_FILES[]['mime']
。这是由用户代理发送的,不可信。$_FILES[]['name']
。此外,您的原始代码不起作用,因为您从未
move_uploaded_file()
。Using PHP's GD extension can truly be a pain. Personally, I would recommend adopting a abstraction library like WideImage. This would simplify your code greatly:
Yes, all that code above reduced to four lanes.
A few pointers:
$_FILES[]['mime']
. That's sent by the user-agent and it cannot be trusted.$_FILES[]['name']
.Also, your original code doesn't work because you never
move_uploaded_file()
.得到了更好的解决方案,实际上使用 Thumbnailer< 处理图像可能很有趣/a> 类。
Got better solution, hadling images can be fun actually using the Thumbnailer class.