简体   繁体   中英

PHP getimagesize(): IMAGETYPE or MIME-type?

I have a simple images resizing script that works great for JPEGs, but not for GIFs or PNGs. The first step is getting the correct image type so I can process it accordingly.

My question is: It seems that getimagesize() returns both IMAGETYPE and MIME-type... So which should I use to determine if an imagine is JPEG, PNG, or GIF?

It seems strange that PHP gives you two ways of doing this, so I presume they each have their designated uses?

This is largely for convenience, though there are some cases where multiple IMAGETYPEs correspond to the same MIME type. For example, IMAGETYPE_JPC , IMAGETYPE_JPX and IMAGETYPE_JB2 all have the MIME type application/octet-stream .

To determine if an imagine is JPEG, PNG, or GIF you can use either, though I would generally go with IMAGETYPE.

Documentation says that:

mime is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type header

I believe IMAGETYPE is valid choice.

I am not sure if your question is clear enough but why can't you use simple IF statement to check for the image type first?

For example, I can check for 3 basic mime types and the image size in one line ie:

if ( (($_FILES["file"]["type"] == "image/gif") || 
     ($_FILES["file"]["type"] == "image/jpeg") || 
     ($_FILES["file"]["type"] == "image/png")) && 
     ($_FILES["file"]["size"] < 1000000) )
{    
// your re-size script    
} else {     
echo "Wrong image mime type!";   
}

Here is the mime-type list: http://www.php.net/manual/en/function.mime-content-type.php#87856

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