If it possible to check if the type of image if it was renamed from .png
to .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)
Every .png starts with these bytes:
89 50 4E 47 0D 0A 1A 0A
They are the PNG signature. If a .jpg starts with those bytes, it is not a jpg.
getimagesize will contain info about image type:
$info = getimagesize('file.png');
if($info[2] == IMAGETYPE_JPEG){
}
You can always read the image type to check an image using 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...
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. PNG files start with a different signature: \\211 PNG \\r \\n \\032 \\n (in hexadecimal: 89 50 4E 47 0D 0A 1A 0A).
See this website for more information: http://en.wikipedia.org/wiki/Magic_number_%28programming%29
Try the file
command.
When unknownimage
is a JPG image:
$ file unknownimage
unknownimage: JPEG image data, baseline, precision 8, 400x400, frames 3
When unknownimage
file is a PNG image:
$ file unknownimage
unknownimage: PNG image data, 779 x 701, 8-bit/color RGBA, non-interlaced
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.