php 检查图像是否为 JPEG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1141227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if the image is a JPEG
提问by MitMaro
I need to check whether a given image is a JPEG.
我需要检查给定的图像是否是 JPEG。
if ($_FILES["fname"]["error"] > 0) {
$imgData = "hyperlink/holder.jpg";
} else {
$imgData ="hyperlink/" . $_FILES["fname"]["name"];
}
// Only accept jpg images
// pjpeg is for Internet Explorer should be jpeg
if (!($_FILES["fname"]["type"] == "image/pjpeg") ) {
print "I only accept jpg files!";
exit(0);
}
When it goes to first statement in the first if statement it always gives I only accept jpg files!
当它转到第一个 if 语句中的第一个语句时,它总是给出我只接受 jpg 文件!
How can I fix it?
我该如何解决?
回答by MitMaro
Try the exif_imagetypeimage function.
试试exif_imagetype图像函数。
Example:
例子:
if(exif_imagetype($filepath) != IMAGETYPE_JPEG){
echo 'Not a JPEG image';
}
回答by Scott Evernden
PHP has such good image-type support, i wonder why you are restricting your app. In just a couple lines of code you can deal with any input format and convert to jpeg, if that is a requirement...
PHP 有这么好的图像类型支持,我想知道你为什么限制你的应用程序。只需几行代码,您就可以处理任何输入格式并转换为 jpeg,如果需要的话...
$im = imagecreatefrompng(input_filename)
imagejpeg($im, output_filename);
回答by CMP
I believe the following works:
我相信以下工作:
Also note that:
另请注意:
(exif_imagetype($ImagePathAndName) == IMAGETYPE_JPEG)
(exif_imagetype($ImagePathAndName) == IMAGETYPE_JPEG)
only reads the first few bytes looking for an image header so isn't really good enough to confirm if an image is corrupt.
只读取寻找图像标头的前几个字节,因此不足以确认图像是否已损坏。
Below I have it in a logical “and” statement i.e. both of these tests must be passed in order for the image to qualify as being valid and non-corrupt etc:
下面我将它放在一个逻辑“和”语句中,即必须通过这两个测试才能使图像符合有效且未损坏等条件:
if ((exif_imagetype($ImagePathAndName) == IMAGETYPE_JPEG) && (imagecreatefromjpeg( $ImagePathAndName ) !== false ))
{
echo 'The picture is a valid jpg<br>';
}
Note: You need to place this line of code at the top of the php code in order to avoid seeing the warning messages from imagecreatefromjpeg( $ImagePathAndName ) when it encounters a fake/corrupt image file.
注意:您需要将此行代码放在 php 代码的顶部,以避免在遇到伪造/损坏的图像文件时看到来自 imagecreatefromjpeg( $ImagePathAndName ) 的警告消息。
ini_set(‘gd.jpeg_ignore_warning', 1);
回答by Muhammad Ashikuzzaman
Check the mime(Multipurpose Internet Mail Extensions) type of file with this code. And verify your desired type. You can also detect png,gif with this code.
使用此代码检查文件的 MIME(多用途 Internet 邮件扩展)类型。并验证您想要的类型。您还可以使用此代码检测 png、gif。
if($_FILES["fname"]["type"] == "image/jpeg")
{
echo "File type is JPEG";
}
回答by Pascal MARTIN
When using $_FILES, you are relying on informations sent by the client, which is not the best thing to do (you've seen it's not always the same, and, if I remember correctly, $_FILES['...']['type']can be faked).
使用时$_FILES,您依赖于客户端发送的信息,这不是最好的做法(您已经看到它并不总是相同的,如果我没记错的话,$_FILES['...']['type']可以伪造)。
If you are using PHP >= 5.3 (or can install PECL packages), maybe you can give a look to the extension Fileinfo. If you are using an older version, what about mime_content_type?
如果您使用的是 PHP >= 5.3(或者可以安装 PECL 包),也许您可以查看扩展名Fileinfo。如果您使用的是旧版本,那该怎么mime_content_type办?
And, as said by Scott, why allow only jpeg?
而且,正如 Scott 所说,为什么只允许 jpeg?
Looking about the code better : when you are in the first case (error > 0), you are assigning a default file to $imgData? Why the spaces around "hyperlink"?
And why do you always use to check the content-type, even if there was an error a couple of lines before?
更好地查看代码:当您处于第一种情况 ( error > 0) 时,您将默认文件分配给$imgData? 为什么“超链接”周围有空格?为什么你总是用来检查content-type,即使之前有几行错误?
To finish, did you have a look at the manual (Handling file uploads)?
最后,您是否查看了手册(处理文件上传)?
回答by user140125
Why don't you try creating an array of exceptions (the files you want the user to be able to upload).
为什么不尝试创建一系列异常(您希望用户能够上传的文件)。
// Hyperlink for your website
$hyperlink = "http://www.yourwebsitehere.com";
if($_FILES['fname']['error'] > 0)
{
$image= $hyperlink . "/holder.jpg";
}
else
{
$image = $hyperlink . "/" . $_FILES['fname']['name'];
}
// Only accept files of jpeg format
$exceptions = array("image/jpg", "image/jpeg", "image/pjpeg");
foreach($exceptions as $value)
{
if($_FILES['fname']['type'] != $value)
{
echo "I only accept jpeg images!";
break; // Or exit();
}
}

