gd: imagecreatefrom how to check

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • avlee

    #1

    gd: imagecreatefrom how to check

    Hello

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

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

    How can i do it ?

    Thanx
  • Toby A Inkster

    #2
    Re: gd: imagecreatefrom how to check

    avlee wrote:
    i need to check if the file is png or jpeg.
    How can i do 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 ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    Working...