检查图像是否是JPEG

时间:2021-12-28 00:49:17

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?

我怎样才能修好它呢?

6 个解决方案

#1


21  

Try the exif_imagetype image function.

尝试一下exif_imagetype图像函数。

Example:

例子:

if(exif_imagetype($filepath) != IMAGETYPE_JPEG){
    echo 'Not a JPEG image';
}

#2


3  

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);

#3


1  

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";
  }

#4


1  

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);

#5


0  

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();
    }
}

#6


0  

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['…)…”]['类型']可以伪造)。

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包),也许您可以查看扩展文件信息。如果您使用的是旧版本,那么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?

更好地查看代码:在第一种情况下(错误> 0),您正在为$imgData分配一个默认文件?为什么“超链接”周围的空格?为什么你总是要检查内容类型,即使之前有几行错误呢?

To finish, did you have a look at the manual (Handling file uploads)?

最后,您看了手册(处理文件上传)吗?

#1


21  

Try the exif_imagetype image function.

尝试一下exif_imagetype图像函数。

Example:

例子:

if(exif_imagetype($filepath) != IMAGETYPE_JPEG){
    echo 'Not a JPEG image';
}

#2


3  

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);

#3


1  

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";
  }

#4


1  

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);

#5


0  

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();
    }
}

#6


0  

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['…)…”]['类型']可以伪造)。

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包),也许您可以查看扩展文件信息。如果您使用的是旧版本,那么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?

更好地查看代码:在第一种情况下(错误> 0),您正在为$imgData分配一个默认文件?为什么“超链接”周围的空格?为什么你总是要检查内容类型,即使之前有几行错误呢?

To finish, did you have a look at the manual (Handling file uploads)?

最后,您看了手册(处理文件上传)吗?