简体   繁体   English

PHP getimagesize():IMAGETYPE还是MIME类型?

[英]PHP getimagesize(): IMAGETYPE or MIME-type?

I have a simple images resizing script that works great for JPEGs, but not for GIFs or PNGs. 我有一个简单的图像大小调整脚本,适用于JPEG,但不适用于GIF或PNG。 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? 我的问题是:似乎getimagesize()返回IMAGETYPE和MIME类型......那么我应该用哪个来确定想象是JPEG,PNG还是GIF?

It seems strange that PHP gives you two ways of doing this, so I presume they each have their designated uses? 看起来很奇怪,PHP为你提供了两种方法,所以我认为它们每个都有它们的指定用途?

This is largely for convenience, though there are some cases where multiple IMAGETYPEs correspond to the same MIME type. 这主要是为了方便,但在某些情况下,多个IMAGETYPE对应于相同的MIME类型。 For example, IMAGETYPE_JPC , IMAGETYPE_JPX and IMAGETYPE_JB2 all have the MIME type application/octet-stream . 例如, IMAGETYPE_JPCIMAGETYPE_JPXIMAGETYPE_JB2都具有MIME类型application/octet-stream

To determine if an imagine is JPEG, PNG, or GIF you can use either, though I would generally go with IMAGETYPE. 要确定想象是JPEG,PNG还是GIF,您可以使用其中任何一个,但我通常会使用IMAGETYPE。

Documentation says that: 文档说:

mime is the correspondant MIME type of the image. mime是图像的对应MIME类型。 This information can be used to deliver images with the correct HTTP Content-type header 此信息可用于使用正确的HTTP Content-type标头提供图像

I believe IMAGETYPE is valid choice. 我相信IMAGETYPE是有效的选择。

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? 我不确定你的问题是否足够清楚,但为什么不能使用简单的IF语句来检查图像类型呢?

For example, I can check for 3 basic mime types and the image size in one line ie: 例如,我可以在一行中检查3种基本的mime类型和图像大小,即:

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 这是mime类型列表: http//www.php.net/manual/en/function.mime-content-type.php#87856

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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