Re: how to detect type of image

From: Date: Sat, 28 Apr 2007 00:10:19 +0000
Subject: Re: how to detect type of image
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-254044@lists.php.net to get a copy of this message
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

« previous php.general (#254044) next »