检查图像在 PHP 中是否有效的最佳和最快方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7393419/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:36:13  来源:igfitidea点击:

What is the best and fastest way to check if the image is valid in PHP?

phpimage

提问by chubbyk

What is the best and fastest way to check if the image is valid in PHP ? I need it to be able to check GIF, JPG as well as PNG images.

检查图像在 PHP 中是否有效的最佳和最快方法是什么?我需要它能够检查 GIF、JPG 以及 PNG 图像。

回答by Felix Eve

exif_imagetypeis a better solution.

exif_imagetype是一个更好的解决方案。

This method is faster than using getimagesize. To quote php.net"The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster."

此方法比使用 getimagesize 更快。引用php.net“返回值与 getimagesize() 在索引 2 中返回的值相同,但 exif_imagetype() 快得多。”

if(exif_imagetype('path/to/image.jpg')) {
    // your image is valid
}

Update:

更新:

After reading that getimagesize can be unreliable I tried to find some more info on what file types can give false positives but could not find any more info so performed a brief test (using exif_imagetype):

在阅读了 getimagesize 可能不可靠之后,我试图找到更多关于哪些文件类型会导致误报的信息,但找不到更多信息,因此进行了一个简短的测试(使用exif_imagetype):

PowerPoint-survey-results.pptx - N
LRMonoPhase4.wav               - N
TestWordDoc.doc                - N
drop.avi                       - N
test.dll                       - N
raw_data_sample.sav            - N
text.txt                       - N
Excel-survey-results.xlsx      - N
pdf-test.pdf                   - N
simplepie-1.5.zip              - N
Word-survey-results.docx       - N
centaur_1.mpg                  - N
Test.svg                       - N
multipage_tif_example.tif      - Y
200.gif                        - Y
Test.png                       - Y
test.jpg                       - Y

I realise this is not exhaustive however at least show that with common file types the results are as expected.

我意识到这并不详尽,但至少表明使用常见文件类型的结果符合预期。

回答by Marcel Kohls

As recommended by the PHP documentation:

正如PHP 文档所推荐的那样:

"Do not use getimagesize() to check that a given file is a valid image.Use a purpose-built solution such as the Fileinfo extension instead."

“不要使用 getimagesize() 来检查给定的文件是否是有效的图像。请改用专门构建的解决方案,例如 Fileinfo 扩展。”

Here is an example:

下面是一个例子:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, "test.jpg");

if (isset($type) && in_array($type, array("image/png", "image/jpeg", "image/gif"))) {
    echo 'This is an image file';
} else {
    echo 'Not an image :(';
}

回答by Blagovest Buyukliev

I guess getimagesize:

我猜getimagesize

list($width, $height, $type, $attr) = getimagesize("path/to/image.jpg");

if (isset($type) && in_array($type, array(
    IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) {
  ...
}

回答by Max

exif_imagetype is much faster than getimagesize and doesn't use gd-Lib (leaving a leaner mem footprint)

exif_imagetype 比 getimagesize 快得多并且不使用 gd-Lib(留下更精简的内存占用)

function isImage($pathToFile)
{
  if( false === exif_imagetype($pathToFile) )
   return FALSE;

   return TRUE;
}

回答by Hoàng V? Tgtt

I use this:

我用这个:

function is_image($path)
{
    $a = getimagesize($path);
    $image_type = $a[2];

    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
    {
        return true;
    }
    return false;
}

回答by kasper Taeymans

I use this function... it checks urls too

我使用这个功能...它也检查网址

function isImage($url){
   $params = array('http' => array(
                'method' => 'HEAD'
             ));
   $ctx = stream_context_create($params);
   $fp = @fopen($url, 'rb', false, $ctx);
   if (!$fp) 
      return false;  // Problem with url

  $meta = stream_get_meta_data($fp);
  if ($meta === false){
      fclose($fp);
      return false;  // Problem reading data from url
  }
 }