Re: Re: how to detect type of image

From: Date: Sat, 28 Apr 2007 08:39:33 +0000
Subject: Re: Re: how to detect type of image
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-254049@lists.php.net to get a copy of this message
On 4/28/07, Tim <downloadsoft@gmx.net> wrote:
On 21.04.2007 12:45, Alain Roger wrote: Hi, In my web application, end user is able to load images (png, jpeg, gif,..) into database. I would like to know how can i detect automatically the type of image (pnd, jpeg,...) ? i do not want to check the extension because this is easily faked... just by renaming it. Does it exist a technique for that ? thanks a lot, Hi, unfortunately mime_content_type() does not work for me. This functions does always return false, although the "magic.mime" is valid. Here is a function I wrote to determine the correct extension of an image file: function get_image_extension($filename) {
    if (function_exists('exif_imagetype'))
    {
        switch (exif_imagetype($filename))
        {
            case 1:
                return 'gif';
            case 2:
                return 'jpg';
            case 3:
                return 'png';
            case 4:
                return 'swf';
            case 5:
                return 'psd';
            case 6:
                return 'bmp';
            case 7:
                return 'tiff';
            case 8:
                return 'tiff';
            case 9:
                return 'jpc';
            case 10:
                return 'jp2';
            case 11:
                return 'jpx';
            case 12:
                return 'jb2';
            case 13:
                return 'swc';
            case 14:
                return 'iff';
            case 15:
                return 'wbmp';
            case 16:
                return 'xbm';
            default:
                return false;
        }
    }
    else
        return false;
} Best regards, Tim
First of all, i don't see any reason why this works better then other functions, as it also relies on the magic bytes. Second, for your switch, you should use the constants (IMAGETYPE_GIF,IMAGETYPE_JPEG,...) instead of just using decimal values. Tijnema

« previous php.general (#254049) next »