PHP:二进制图像数据,检查图像类型

时间:2021-03-06 16:34:10

I have some images in bin, I want to check the header to check the format (jpg, png, etc)

我在bin中有一些图像,我想检查标题以检查格式(jpg,png等)

I don't want to use temp files! I have a solution using TEMP FILES.

我不想使用临时文件!我有一个使用TEMP FILES的解决方案。

7 个解决方案

#1


21  

The bits start with:

这些比特开始于:

$JPEG = "\xFF\xD8\xFF"
$GIF  = "GIF"
$PNG  = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
$BMP  = "BM"
$PSD  = "8BPS"
$SWF  = "FWS"

The other ones I wouldn't know right now, but the big 3 (jpeg,gif,png) usually cover 99%. So, compare the first bytes to those string, and you have your answer.

其他我现在不知道的,但是大3(jpeg,gif,png)通常覆盖99%。因此,将第一个字节与这些字符串进行比较,您就得到了答案。

#2


27  

I can se that most of you didn't understand the question :) ( question was how to validate binary data in buffer, not a file on disk).

我可以说大多数人都不明白这个问题:)(问题是如何验证缓冲区中的二进制数据,而不是磁盘上的文件)。

I had same problem, and resolved it with:

我有同样的问题,并解决了它:

$finfo = new finfo(FILEINFO_MIME_TYPE);
$finfo->buffer($rawImage);

#3


6  

Here's an implementation of the function as described by Wrikken

这是Wrikken描述的函数的实现

function getImgType($filename) {
    $handle = @fopen($filename, 'r');
    if (!$handle)
        throw new Exception('File Open Error');

    $types = array('jpeg' => "\xFF\xD8\xFF", 'gif' => 'GIF', 'png' => "\x89\x50\x4e\x47\x0d\x0a", 'bmp' => 'BM', 'psd' => '8BPS', 'swf' => 'FWS');
    $bytes = fgets($handle, 8);
    $found = 'other';

    foreach ($types as $type => $header) {
        if (strpos($bytes, $header) === 0) {
            $found = $type;
            break;
        }
    }
    fclose($handle);
    return $found;
}

#4


1  

Are the files being uploaded or are they already on the file system?

文件是上传还是已经在文件系统上?

Try using mime_content_type() to get the file's MIME format.

尝试使用mime_content_type()来获取文件的MIME格式。

#5


1  

http://php.net/manual/en/function.getimagesize.php

http://php.net/manual/en/function.getimagesize.php

"Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image."

“索引2是指示图像类型的IMAGETYPE_XXX常量之一。”

#6


0  

Why not just check the file entension? :)

为什么不检查文件的扩展? :)

An Alternative

替代

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

#7


0  

Use the fileinfo PHP extension:

使用fileinfo PHP扩展:

http://de.php.net/manual/en/function.finfo-file.php

http://de.php.net/manual/en/function.finfo-file.php

Its using the "file" *nix command to reliably determine the mime-type of a given file:

它使用“file”* nix命令可靠地确定给定文件的mime类型:

$finfo = finfo_open(FILEINFO_MIME_TYPE); 
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);

This extension is shipped with PHP 5.3 or can be installed from pecl (pecl install fileinfo) for earlier versions.

此扩展随PHP 5.3一起提供,或者可以从pecl(pecl install fileinfo)安装以用于早期版本。

#1


21  

The bits start with:

这些比特开始于:

$JPEG = "\xFF\xD8\xFF"
$GIF  = "GIF"
$PNG  = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
$BMP  = "BM"
$PSD  = "8BPS"
$SWF  = "FWS"

The other ones I wouldn't know right now, but the big 3 (jpeg,gif,png) usually cover 99%. So, compare the first bytes to those string, and you have your answer.

其他我现在不知道的,但是大3(jpeg,gif,png)通常覆盖99%。因此,将第一个字节与这些字符串进行比较,您就得到了答案。

#2


27  

I can se that most of you didn't understand the question :) ( question was how to validate binary data in buffer, not a file on disk).

我可以说大多数人都不明白这个问题:)(问题是如何验证缓冲区中的二进制数据,而不是磁盘上的文件)。

I had same problem, and resolved it with:

我有同样的问题,并解决了它:

$finfo = new finfo(FILEINFO_MIME_TYPE);
$finfo->buffer($rawImage);

#3


6  

Here's an implementation of the function as described by Wrikken

这是Wrikken描述的函数的实现

function getImgType($filename) {
    $handle = @fopen($filename, 'r');
    if (!$handle)
        throw new Exception('File Open Error');

    $types = array('jpeg' => "\xFF\xD8\xFF", 'gif' => 'GIF', 'png' => "\x89\x50\x4e\x47\x0d\x0a", 'bmp' => 'BM', 'psd' => '8BPS', 'swf' => 'FWS');
    $bytes = fgets($handle, 8);
    $found = 'other';

    foreach ($types as $type => $header) {
        if (strpos($bytes, $header) === 0) {
            $found = $type;
            break;
        }
    }
    fclose($handle);
    return $found;
}

#4


1  

Are the files being uploaded or are they already on the file system?

文件是上传还是已经在文件系统上?

Try using mime_content_type() to get the file's MIME format.

尝试使用mime_content_type()来获取文件的MIME格式。

#5


1  

http://php.net/manual/en/function.getimagesize.php

http://php.net/manual/en/function.getimagesize.php

"Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image."

“索引2是指示图像类型的IMAGETYPE_XXX常量之一。”

#6


0  

Why not just check the file entension? :)

为什么不检查文件的扩展? :)

An Alternative

替代

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

#7


0  

Use the fileinfo PHP extension:

使用fileinfo PHP扩展:

http://de.php.net/manual/en/function.finfo-file.php

http://de.php.net/manual/en/function.finfo-file.php

Its using the "file" *nix command to reliably determine the mime-type of a given file:

它使用“file”* nix命令可靠地确定给定文件的mime类型:

$finfo = finfo_open(FILEINFO_MIME_TYPE); 
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);

This extension is shipped with PHP 5.3 or can be installed from pecl (pecl install fileinfo) for earlier versions.

此扩展随PHP 5.3一起提供,或者可以从pecl(pecl install fileinfo)安装以用于早期版本。