我正在寻找一种方法,以采取用户上传的图像,目前是放在一个临时位置例如: /tmp/jkhjkh78,并从它创建一个php图像,自动检测格式。
有没有比使用imagefromjpeg、imagefrompng等的一堆try/catching更聪明的方法呢?
发布于 2008-10-09 22:13:59
这是getimagesize的功能之一。他们可能应该叫它"getimageinfo",但这是为你准备的PHP。
发布于 2014-05-15 23:06:52
//Image Processing
$cover = $_FILES['cover']['name'];
$cover_tmp_name = $_FILES['cover']['tmp_name'];
$cover_img_path = '/images/';
$type = exif_imagetype($cover_tmp_name);
if ($type == (IMAGETYPE_PNG || IMAGETYPE_JPEG || IMAGETYPE_GIF || IMAGETYPE_BMP)) {
$cover_pre_name = md5($cover); //Just to make a image name random and cool :D
/**
* @description : possible exif_imagetype() return values in $type
* 1 - gif image
* 2 - jpg image
* 3 - png image
* 6 - bmp image
*/
switch ($type) { #There are more type you can choose. Take a look in php manual -> http://www.php.net/manual/en/function.exif-imagetype.php
case '1' :
$cover_format = 'gif';
break;
case '2' :
$cover_format = 'jpg';
break;
case '3' :
$cover_format = 'png';
break;
case '6' :
$cover_format = 'bmp';
break;
default :
die('There is an error processing the image -> please try again with a new image');
break;
}
$cover_name = $cover_pre_name . '.' . $cover_format;
//Checks whether the uploaded file exist or not
if (file_exists($cover_img_path . $cover_name)) {
$extra = 1;
while (file_exists($cover_img_path . $cover_name)) {
$cover_name = md5($cover) . $extra . '.' . $cover_format;
$extra++;
}
}
//Image Processing Ends
这将使图像名称看起来很酷和独特
发布于 2010-02-28 22:39:14
使用exif_imagetype()
(如果可用) ..:
http://www.php.net/manual/en/function.exif-imagetype.php
我非常确定在安装php时,exif函数在默认情况下是可用的(也就是说,您必须明确地排除它们,而不是明确地包括它们
https://stackoverflow.com/questions/189391
复制相似问题