php 如何在php中获取图像类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7925962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the image type in php
提问by user892134
I am trying to get the image type in the database but the below isn't working. How do I detect if the image is png, jpeg or gif?
我试图在数据库中获取图像类型,但下面的不起作用。如何检测图像是 png、jpeg 还是 gif?
if(isset($_POST['submit'])) {
$fileType = $_FILES['image']['type'];
$tmpname = $_FILES['image']['tmp_name'];
$fp = fopen($tmpname,'r');
$data = fread($fp,filesize($tmpname));
$data = addslashes($data);
fclose($fp);
$update = mysql_query("UPDATE avatar SET image1='$data',type='$fileType' WHERE username='$user'",$this->connect);
} else {
echo "<form enctype='multipart/form-data' action='http://www.example.com/cp/avatar' method='post'>
<div id='afield1' >Upload</div><div id='afield2'><input type='hidden' name='MAX_FILE_SIZE' value='102400' /><input type='file' size='25' name='image' /></div>
<div id='asubmit'><input type='submit' name='submit' class='button' value='Save Changes' /></div>
</form>";
}
回答by Peter
use getimagesize()or exif_imagetype()
使用getimagesize()或exif_imagetype()
// integer - for example: IMAGETYPE_GIF, IMAGETYPE_JPEG etc.
$type = exif_imagetype($_FILES['image']['tmp_name']);
and
和
$info = getimagesize($_FILES['image']['tmp_name']);
$mime = $info['mime']; // mime-type as string for ex. "image/jpeg" etc.
$width = $info[0]; // width as integer for ex. 512
$height = $info[1]; // height as integer for ex. 384
$type = $info[2]; // same as exif_imagetype
Mind that exif_imagetype
is much faster than getimagesize
. Check documentation for more info.
心意exif_imagetype
比 快得多getimagesize
。查看文档以获取更多信息。