确定文件是否是PHP中的图像的最佳方法是什么?

时间:2022-01-06 04:57:33

I have a sever which people can upload files to. The problem is that some of the filenames are mangled (dont have any extension) and so I cannot immediately determine file type. This question is two part: for the files which do have filenames what is the best way to determine whether or not it is an image? (Just a big long if/else if list?) Secondly, for the files which dont have extensions, how can I determine if they are images?

我有一个人们可以上传文件的服务器。问题是某些文件名被破坏(没有任何扩展名),所以我无法立即确定文件类型。这个问题是两部分:对于有文件名的文件,确定它是否是图像的最佳方法是什么? (只是一个很长的if / else if列表?)其次,对于没有扩展名的文件,我如何确定它们是否是图像?

12 个解决方案

#1


5  

You can use exif_imagetype()

你可以使用exif_imagetype()

<?php    $type =exif_imagetype($image);

where $type is a value

其中$ type是一个值

  1. IMAGETYPE_GIF
  2. IMAGETYPE_GIF
  3. IMAGETYPE_JPEG
  4. IMAGETYPE_JPEG
  5. IMAGETYPE_PNG
  6. IMAGETYPE_PNG
  7. IMAGETYPE_SWF
  8. IMAGETYPE_SWF
  9. IMAGETYPE_PSD
  10. IMAGETYPE_PSD
  11. IMAGETYPE_BMP
  12. IMAGETYPE_BMP
  13. IMAGETYPE_TIFF_II (intel byte order)
  14. IMAGETYPE_TIFF_II(英特尔字节顺序)
  15. IMAGETYPE_TIFF_MM (motorola byte order)
  16. IMAGETYPE_TIFF_MM(摩托罗拉字节顺序)
  17. IMAGETYPE_JPC
  18. IMAGETYPE_JPC
  19. IMAGETYPE_JP2
  20. IMAGETYPE_JP2
  21. IMAGETYPE_JPX
  22. IMAGETYPE_JPX
  23. IMAGETYPE_JB2
  24. IMAGETYPE_JB2
  25. IMAGETYPE_SWC
  26. IMAGETYPE_SWC
  27. IMAGETYPE_IFF
  28. IMAGETYPE_IFF
  29. IMAGETYPE_WBMP
  30. IMAGETYPE_WBMP
  31. IMAGETYPE_XBM
  32. IMAGETYPE_XBM
  33. IMAGETYPE_ICO
  34. IMAGETYPE_ICO

From the manual:

从手册:

When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE. The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster.

找到正确的签名后,将返回适当的常量值,否则返回值为FALSE。返回值与getimagesize()在索引2中返回的值相同,但exif_imagetype()的速度要快得多。

#2


4  

You can use getimagesizeIt does not require the GD image library and it returns same information about image type.http://it2.php.net/manual/en/function.getimagesize.php

您可以使用getimagesizeIt不需要GD图像库,它返回有关图像类型的相同信息.http://it2.php.net/manual/en/function.getimagesize.php

#3


2  

If you have the GD2 extension enabled, you could just use that to load the file as an image, then if it returns invalid you can catch the error and return FALSE, otherwise return TRUE.

如果你启用了GD2扩展,你可以使用它来加载文件作为图像,然后如果它返回无效你可以捕获错误并返回FALSE,否则返回TRUE。

#4


2  

You have two options here, one's simple and pre-built with some shortfalls, the other is complex and requires math.

你有两个选择,一个是简单的,预先构建有一些缺点,另一个是复杂的,需要数学。

PHP's fileinfo can be used to detect file types based on the file's actual header information. For instance, I just grabbed your gravitar:

PHP的fileinfo可用于根据文件的实际标头信息检测文件类型。例如,我抓住了你的重力:

确定文件是否是PHP中的图像的最佳方法是什么?

But the actual code is this:

但实际代码是这样的:

‰PNGIHDR  szzôIDATX…­—OL\UÆZÀhëT)¡    c•1T:1‘Š‘.Ú(]4†A“ÒEY˜à.................................

So, even without the file name I could detect it quite obviously. This is what the PHP Fileinfo extension will do. Most PNG and JPG files tend to have this header in them, but this is not so for every single file type.

所以,即使没有文件名,我也能很清楚地发现它。这是PHP Fileinfo扩展将执行的操作。大多数PNG和JPG文件往往都有这个标题,但对于每种文件类型都不是这样。

That being said, fileinfo is dead simple to use, from the manual:

话虽这么说,fileinfo很容易使用,从手册:

$fi = new finfo(FILEINFO_MIME,'/usr/share/file/magic');$mime_type = $fi->buffer(file_get_contents($file));

Your other option is more complex and it depends on your own personal ambitions, you could generate a histogram and profile files based on their content.

您的其他选择更复杂,这取决于您自己的个人抱负,您可以根据其内容生成直方图和配置文件。

Something like this looks like a GIF file:

像这样的东西看起来像一个GIF文件:

确定文件是否是PHP中的图像的最佳方法是什么?

And something like this looks like a TIFF file:

这样的东西看起来像一个TIFF文件:

确定文件是否是PHP中的图像的最佳方法是什么?

From there you'd need to generate a model over multiple types of files for what the histogram of each type should be, and then use that to guess. This is a good method to use for files that don't really have those "magic headers" that can be read easily. Keep in mind, you'll need to learn some math and how to model an average histogram function and match them against files.

从那里你需要为多种类型的文件生成一个模型,用于每种类型的直方图,然后用它来猜测。对于那些没有真正具有可以轻松阅读的“魔术标题”的文件,这是一种很好的方法。请记住,您需要学习一些数学以及如何建模平均直方图函数并将它们与文件进行匹配。

#5


1  

You can try to load the image into PHP's GD library, and see if it works.

您可以尝试将图像加载到PHP的GD库中,看看它是否有效。

$file = file_get_contents('file');$img = imagecreatefromstring($file);if($img === FALSE){  // file is NOT an image}else{  // file IS an image}

#6


1  

Look at image magic identify. http://www.imagemagick.org/script/identify.php

看看图像魔术识别。 http://www.imagemagick.org/script/identify.php

The php wrapper is here: http://www.php.net/manual/en/function.imagick-identifyimage.php

php包装器在这里:http://www.php.net/manual/en/function.imagick-identifyimage.php

Or if you just want to validate that it's an image (and don't care about the meta data): http://www.php.net/manual/en/function.imagick-valid.php

或者,如果您只想验证它是图像(并且不关心元数据):http://www.php.net/manual/en/function.imagick-valid.php

#7


1  

exif_imagetype() might work

exif_imagetype()可能有效

make sure you have exif enabled.

确保你已启用exif。

#8


0  

Try looking at exif_imagetype

试试看exif_imagetype

#9


0  

If you need a fast solution, use imagesx() and imagesy(). There is also a fast way to check large image file dimensions, by reading just a small amount of data from the file header. Explained in more detail in the following url:

如果您需要快速解决方案,请使用imagesx()和imagesy()。通过从文件头中读取少量数据,还可以快速检查大图像文件的尺寸。在以下网址中详细说明:

http://hungred.com/useful-information/php-fastest-image-width-height/

http://hungred.com/useful-information/php-fastest-image-width-height/

#10


0  

You can use the Fileinfo extension:http://www.php.net/manual/en/function.finfo-file.php

您可以使用Fileinfo扩展名:http://www.php.net/manual/en/function.finfo-file.php

finfo_file() uses magic bytes and does not have to load the whole image into memory. The result is a string with the corresponding MIME type, e.g.:

finfo_file()使用魔术字节,不必将整个图像加载到内存中。结果是一个具有相应MIME类型的字符串,例如:

  • text/html
  • text / html的
  • image/gif
  • 图像/ GIF
  • application/vnd.ms-excel
  • 应用/ vnd.ms-Excel中

#11


0  

The type of the image is typically going to be able to be inferenced from the header information of the file.

通常可以从文件的头信息推断图像的类型。

#12


-1  

For the first question is extension is known you could use the PHP function in_array() Documentation

对于第一个问题,扩展已知您可以使用PHP函数in_array()文档

#1


5  

You can use exif_imagetype()

你可以使用exif_imagetype()

<?php    $type =exif_imagetype($image);

where $type is a value

其中$ type是一个值

  1. IMAGETYPE_GIF
  2. IMAGETYPE_GIF
  3. IMAGETYPE_JPEG
  4. IMAGETYPE_JPEG
  5. IMAGETYPE_PNG
  6. IMAGETYPE_PNG
  7. IMAGETYPE_SWF
  8. IMAGETYPE_SWF
  9. IMAGETYPE_PSD
  10. IMAGETYPE_PSD
  11. IMAGETYPE_BMP
  12. IMAGETYPE_BMP
  13. IMAGETYPE_TIFF_II (intel byte order)
  14. IMAGETYPE_TIFF_II(英特尔字节顺序)
  15. IMAGETYPE_TIFF_MM (motorola byte order)
  16. IMAGETYPE_TIFF_MM(摩托罗拉字节顺序)
  17. IMAGETYPE_JPC
  18. IMAGETYPE_JPC
  19. IMAGETYPE_JP2
  20. IMAGETYPE_JP2
  21. IMAGETYPE_JPX
  22. IMAGETYPE_JPX
  23. IMAGETYPE_JB2
  24. IMAGETYPE_JB2
  25. IMAGETYPE_SWC
  26. IMAGETYPE_SWC
  27. IMAGETYPE_IFF
  28. IMAGETYPE_IFF
  29. IMAGETYPE_WBMP
  30. IMAGETYPE_WBMP
  31. IMAGETYPE_XBM
  32. IMAGETYPE_XBM
  33. IMAGETYPE_ICO
  34. IMAGETYPE_ICO

From the manual:

从手册:

When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE. The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster.

找到正确的签名后,将返回适当的常量值,否则返回值为FALSE。返回值与getimagesize()在索引2中返回的值相同,但exif_imagetype()的速度要快得多。

#2


4  

You can use getimagesizeIt does not require the GD image library and it returns same information about image type.http://it2.php.net/manual/en/function.getimagesize.php

您可以使用getimagesizeIt不需要GD图像库,它返回有关图像类型的相同信息.http://it2.php.net/manual/en/function.getimagesize.php

#3


2  

If you have the GD2 extension enabled, you could just use that to load the file as an image, then if it returns invalid you can catch the error and return FALSE, otherwise return TRUE.

如果你启用了GD2扩展,你可以使用它来加载文件作为图像,然后如果它返回无效你可以捕获错误并返回FALSE,否则返回TRUE。

#4


2  

You have two options here, one's simple and pre-built with some shortfalls, the other is complex and requires math.

你有两个选择,一个是简单的,预先构建有一些缺点,另一个是复杂的,需要数学。

PHP's fileinfo can be used to detect file types based on the file's actual header information. For instance, I just grabbed your gravitar:

PHP的fileinfo可用于根据文件的实际标头信息检测文件类型。例如,我抓住了你的重力:

确定文件是否是PHP中的图像的最佳方法是什么?

But the actual code is this:

但实际代码是这样的:

‰PNGIHDR  szzôIDATX…­—OL\UÆZÀhëT)¡    c•1T:1‘Š‘.Ú(]4†A“ÒEY˜à.................................

So, even without the file name I could detect it quite obviously. This is what the PHP Fileinfo extension will do. Most PNG and JPG files tend to have this header in them, but this is not so for every single file type.

所以,即使没有文件名,我也能很清楚地发现它。这是PHP Fileinfo扩展将执行的操作。大多数PNG和JPG文件往往都有这个标题,但对于每种文件类型都不是这样。

That being said, fileinfo is dead simple to use, from the manual:

话虽这么说,fileinfo很容易使用,从手册:

$fi = new finfo(FILEINFO_MIME,'/usr/share/file/magic');$mime_type = $fi->buffer(file_get_contents($file));

Your other option is more complex and it depends on your own personal ambitions, you could generate a histogram and profile files based on their content.

您的其他选择更复杂,这取决于您自己的个人抱负,您可以根据其内容生成直方图和配置文件。

Something like this looks like a GIF file:

像这样的东西看起来像一个GIF文件:

确定文件是否是PHP中的图像的最佳方法是什么?

And something like this looks like a TIFF file:

这样的东西看起来像一个TIFF文件:

确定文件是否是PHP中的图像的最佳方法是什么?

From there you'd need to generate a model over multiple types of files for what the histogram of each type should be, and then use that to guess. This is a good method to use for files that don't really have those "magic headers" that can be read easily. Keep in mind, you'll need to learn some math and how to model an average histogram function and match them against files.

从那里你需要为多种类型的文件生成一个模型,用于每种类型的直方图,然后用它来猜测。对于那些没有真正具有可以轻松阅读的“魔术标题”的文件,这是一种很好的方法。请记住,您需要学习一些数学以及如何建模平均直方图函数并将它们与文件进行匹配。

#5


1  

You can try to load the image into PHP's GD library, and see if it works.

您可以尝试将图像加载到PHP的GD库中,看看它是否有效。

$file = file_get_contents('file');$img = imagecreatefromstring($file);if($img === FALSE){  // file is NOT an image}else{  // file IS an image}

#6


1  

Look at image magic identify. http://www.imagemagick.org/script/identify.php

看看图像魔术识别。 http://www.imagemagick.org/script/identify.php

The php wrapper is here: http://www.php.net/manual/en/function.imagick-identifyimage.php

php包装器在这里:http://www.php.net/manual/en/function.imagick-identifyimage.php

Or if you just want to validate that it's an image (and don't care about the meta data): http://www.php.net/manual/en/function.imagick-valid.php

或者,如果您只想验证它是图像(并且不关心元数据):http://www.php.net/manual/en/function.imagick-valid.php

#7


1  

exif_imagetype() might work

exif_imagetype()可能有效

make sure you have exif enabled.

确保你已启用exif。

#8


0  

Try looking at exif_imagetype

试试看exif_imagetype

#9


0  

If you need a fast solution, use imagesx() and imagesy(). There is also a fast way to check large image file dimensions, by reading just a small amount of data from the file header. Explained in more detail in the following url:

如果您需要快速解决方案,请使用imagesx()和imagesy()。通过从文件头中读取少量数据,还可以快速检查大图像文件的尺寸。在以下网址中详细说明:

http://hungred.com/useful-information/php-fastest-image-width-height/

http://hungred.com/useful-information/php-fastest-image-width-height/

#10


0  

You can use the Fileinfo extension:http://www.php.net/manual/en/function.finfo-file.php

您可以使用Fileinfo扩展名:http://www.php.net/manual/en/function.finfo-file.php

finfo_file() uses magic bytes and does not have to load the whole image into memory. The result is a string with the corresponding MIME type, e.g.:

finfo_file()使用魔术字节,不必将整个图像加载到内存中。结果是一个具有相应MIME类型的字符串,例如:

  • text/html
  • text / html的
  • image/gif
  • 图像/ GIF
  • application/vnd.ms-excel
  • 应用/ vnd.ms-Excel中

#11


0  

The type of the image is typically going to be able to be inferenced from the header information of the file.

通常可以从文件的头信息推断图像的类型。

#12


-1  

For the first question is extension is known you could use the PHP function in_array() Documentation

对于第一个问题,扩展已知您可以使用PHP函数in_array()文档