19

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

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

7 Answers 7

59

I can see 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);
$mimeType = $finfo->buffer($rawImage);
Sign up to request clarification or add additional context in comments.

3 Comments

This solution is the best, if you are using PHP > 5.3
This should indeed be upvoted above mine with the current state of PHP, 5.3 is getting old.
You get something similar to application/pdf; charset=binary Which, in turn, can be extracted this way: list($mime, $raw_charset) = explode(';', $finfo->buffer($rawImage)); and then (if you need the charset): $charset = explode('=', trim($raw_charset))[1];
29

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.

4 Comments

Could you give an example of how you would check these against some binary data using php? thanks
@DonutReply - just for the people searching it: gist.github.com/CodeBrauer/5fe9ad14fa9786b3d1f6
Do you have any information about the last bits to find out the end of an image?
@Daniel Did find out end bytes.Am also searching for same thing.
5

Here's an implementation of the function as described by 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;
}

Comments

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.

Comments

1

Use the fileinfo PHP extension:

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:

$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.

Comments

0

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

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

1 Comment

See function docs, it is written there not to use it for "is image" checks.
-2

Why not just check the file entension? :)

An Alternative

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

2 Comments

So most of your GIF files are not actually GIFs? There is really NO reason this wouldn't work unless you are dealing with user uploads, and even then you likely have nothing to worry about.
In 2012, some users believe they can convert images just by renaming the file... So, it's not reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.