tags:

views:

393

answers:

3

I'm trying to find the mime type of an image. PHP has the function getimagesize but that only takes a filename, whereas I have an image "resource" instead - i.e. an image created from imagecreatefromstring.

I found the functions imagesx and imagesy which return the width/height from a resource but I can't find any function that tell me the mime type from a resource. Anyone know of a way to do this?

Note: Due to a weird server set up, we can't read/write files from the server normally, only through an FTP layer (which is where I read the image data from).

+3  A: 

An image created using imagecreatefromstring has no MIME type any more, it is decoded from its native format and stored in GD's internal format.

The same question was asked a while back with the same result.

The only way to go is to catch the image before it gets imagecreatefromstringed, and somehow catch the size information from it.

You say that you can't do file read/write operations on your system, so just writing out the file is out of the question.

The fact that getimagesize() can't read from variables is known and lamented: http://bugs.php.net/bug.php?id=44239

The guy there mentions a nifty workaround: Registering a new stream wrapper that allows file operations on variables.

Is this an option on your server setup?

Pekka
Care to explain the downvote?
Pekka
Sorry, like I said I can't read/write files to the server normally, so it's not possible to use `getimagesize` because there is no filename. EDIT: dude, don't be so impatient, I was writing an explanation... ;)
DisgruntledGoat
Can't be done with a GD resource only, sorry. But if you `imagecreatefromstring`, you have the original data at some point, right? Why not write it out to disk and do a `getimagesize`? I agree it's silly, but it'll do the job. EDIT: That's why I first comment and then vote ;) Second edit: Ah, you can't write to disk. And I take it you can't use the `ftp://` wrapper for imagesize()?
Pekka
@DisgruntledGoat, I added a second possibility.
Pekka
A: 

You could use the PHP fileinfo functions.

$image_buffer = SomeFunctionToGetStringBufferFromGD();

$fileinfo = finfo_open();

$type = finfo_buffer($fileinfo, $image_buffer);

It uses the magic numbers (same as the unix file command) to identify the file type.

matiasf
+2  A: 

If you've got access to the binary data of the image (as the use of imagecreatefromstring() suggests), you can detect the file-type "manually":


function image_file_type_from_binary($binary) {
    if (
        !preg_match(
            '/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/',
            $binary, $hits
        )
    ) {
        return 'application/octet-stream';
    }
    static $type = array (
        1 => 'image/jpeg',
        2 => 'image/gif',
        3 => 'image/png',
        4 => 'image/x-windows-bmp',
        5 => 'image/tiff',
        6 => 'image/x-ilbm',
    );
    return $type[count($hits) - 1];
}
fireweasel