简体   繁体   中英

Checking image file type

I'm attempting to add a function to my site where users can set their profile picture as an image from an external url, instead of saving it to their pc, then uploading it to my server.

This is what I've come up with so far:

            $filename = $inputs['image_url'];

            if(getimagesize($filename)){
                // Get new dimensions
                list($width, $height) = getimagesize($filename);
                $new_width = 100;
                $new_height = 100;

                // Resample
                $image_p = imagecreatetruecolor($new_width, $new_height);                                           

                //a check needs to be done here $image = imagecreatefromjpeg($filename);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

                $filename = "profile_pictures/". md5( uniqid(rand(),true) ).".jpg";

                imagejpeg($image_p,$filename,100);
            }else{
                echo 'This is not an image';
            }

The code can download .jpg images fine, but I want to support other filetypes too.

I need to somehow check the file type of the image so I can save other images with different filetypes...

                if($extension=="jpg" || $extension=="jpeg"){
                    $image = imagecreatefromjpeg($filename);
                }else if($extension=="png"){
                    image = imagecreatefrompng($filename);
                }else{
                    $image = imagecreatefromgif($filename);
                }

Does anyone know of a function that would allow me to do this?

Thanks in advance for any replies.

Most straight forward and for GD compatiblity checking you can just create it from string:

$image = imagecreatefromstring(file_get_contents($filename));

If this fails to load, you know that you can not process that image.

But see as well getimagesize , it inspects the filetype as well, not only width and height:

list($width, $height, $type) = getimagesize($filename);

The third element, $type , then is any of these IMAGETYPE_XXX constants:

Value   Constant
1     IMAGETYPE_GIF
2     IMAGETYPE_JPEG
3     IMAGETYPE_PNG
4     IMAGETYPE_SWF
5     IMAGETYPE_PSD
6     IMAGETYPE_BMP
7     IMAGETYPE_TIFF_II (intel byte order)
8     IMAGETYPE_TIFF_MM (motorola byte order)
9     IMAGETYPE_JPC
10    IMAGETYPE_JP2
11    IMAGETYPE_JPX
12    IMAGETYPE_JB2
13    IMAGETYPE_SWC
14    IMAGETYPE_IFF
15    IMAGETYPE_WBMP
16    IMAGETYPE_XBM
17    IMAGETYPE_ICO

From exif_imagetype . If you combine both, you could first check if the type is okay to load for you and you then check if you can load it.

Splfileinfo::getextension.php might be just what you need.

Edit : I misread, you only want to check the extension of a string. There isn't built-in function for that in PHP, you can simply use a regexp :

$ext = substr(strrchr($fileName,'.'),1);

You can try of of this

$fileExtention = pathinfo ( $filename, PATHINFO_EXTENSION );

Or

$fileExtention = image_type_to_extension(exif_imagetype($filename));

Finally i would advice you use something like http://phpthumb.sourceforge.net/ is has more than what you need to manipulate images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM