[英]Check image type if renamed from .png to .jpg
If it possible to check if the type of image if it was renamed from .png
to .jpg
? 如果可以检查图像的类型是否从
.png
重命名为.jpg
?
I need this because when I use a resize function my website stop working if the uploaded image was renamed from .png to .jpg (I made my code to accept only .jpg images) 我需要这个,因为当我使用调整大小功能时,如果上传的图像从.png重命名为.jpg(我的代码只接受.jpg图像),我的网站就会停止工作
Every .png starts with these bytes: 每个.png都以这些字节开头:
89 50 4E 47 0D 0A 1A 0A
They are the PNG signature. 它们是PNG签名。 If a .jpg starts with those bytes, it is not a jpg.
如果.jpg以这些字节开头,那么它不是jpg。
getimagesize will contain info about image type: getimagesize将包含有关图像类型的信息:
$info = getimagesize('file.png');
if($info[2] == IMAGETYPE_JPEG){
}
You can always read the image type to check an image using exif_imagetype() 您始终可以使用exif_imagetype()读取图像类型以检查图像
$image_type = exif_imagetype($filename);
Example: 例:
<?php
if (exif_imagetype('image.gif') != IMAGETYPE_GIF) {
echo 'The picture is not a gif';
}
?>
Yes you can use this 是的你可以使用它
if($_FILES["imagefile"]["type"] == "image/jpeg")
{
return true;
}
else if($_FILES["imagefile"]["type"] == "image/png")
{
return false;
}
Or you should use this mime_content_type is more reliable because the $_FILES["imagefile"]["type"]
can be faked from client side... 或者您应该使用此mime_content_type更可靠,因为
$_FILES["imagefile"]["type"]
可以从客户端伪造...
What you need to know is not the history of the file, but its real format. 你需要知道的不是文件的历史,而是它的真实格式。 You can do that by examining the file's content: JPG files should begin with the bytes FF D8 and end with FF D9.
您可以通过检查文件的内容来做到这一点:JPG文件应以FF D8字节开头,以FF D9结束。 PNG files start with a different signature: \\211 PNG \\r \\n \\032 \\n (in hexadecimal: 89 50 4E 47 0D 0A 1A 0A).
PNG文件以不同的签名开头:\\ 211 PNG \\ r \\ n \\ 032 \\ n(十六进制:89 50 4E 47 0D 0A 1A 0A)。
See this website for more information: http://en.wikipedia.org/wiki/Magic_number_%28programming%29 有关更多信息,请访问此网站: http : //en.wikipedia.org/wiki/Magic_number_%28programming%29
Try the file
command. 尝试使用
file
命令。
When unknownimage
is a JPG image: 当
unknownimage
是JPG图像时:
$ file unknownimage
unknownimage: JPEG image data, baseline, precision 8, 400x400, frames 3
When unknownimage
file is a PNG image: 当
unknownimage
文件是PNG图像时:
$ file unknownimage
unknownimage: PNG image data, 779 x 701, 8-bit/color RGBA, non-interlaced
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.