gd: imagecreatefrom how to check

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have some dynamically generated images in files.
I need to open them using:

i need to check if the file is png or jpeg.

How can i do it ?


Re: gd: imagecreatefrom how to check

avlee wrote:

Quoted text here. Click to load it

Open the file using fopen() and read in the first 10 bytes. Now close the
file. Match the first ten bytes using the following:

    $is_image_png  = preg_match('/^.PNG/', $ten_bytes);
    $is_image_jpeg = preg_match('/(Exif|JFIF)$/', $ten_bytes);

Note, it's also fairly easy to check for a few other common file types:

    $is_image_gif  = preg_match('/^GIF8[79]/', $ten_bytes);
    $is_audio_ogg  = preg_match('/^Ogg/', $ten_bytes);
    $is_script     = preg_match('/^#!\//', $ten_bytes);

Of course, it's not a rigid test that the file really is of that type, but
it's a nice quick check, and should help you differentiate between JPEG
and PNG images fairly easily.

Toby A Inkster BSc (Hons) ARCS
Contact Me ~
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Site Timeline