缩略图Thumbnail.class.php 'png',2='jpeg',3='gif');list($src_width,$src_height,$type) = getimagesize($fileName);self::$src_width = $src_width;self::$src_height = $src_height;self::$type = $imageType[$type];return self::$error = in_array(s
'png',2=>'jpeg',3=>'gif');
list($src_width,$src_height,$type) = getimagesize($fileName);
self::$src_width = $src_width;
self::$src_height = $src_height;
self::$type = $imageType[$type];
return self::$error = in_array(self::$type, $imageType) ? true:"不支持该格式文件,支持的格式为:'png','jpeg','gif'";
}
/**
* [variableMethod 返回可变方法的方法名]
* @param [string] $name [可变方法名的拼接部分]
* @param string $funcPrefix [可变方法名的不变部分(前缀)]
* @return [string] [可变方法的方法名字符串]
*/
public static function variableMethod($funcPrefix='imagecreatefrom',$name){
return $funcPrefix.$name;
}
/**
* 生成图片的随机文件名
* @param string $fileName 传入的文件名,带后缀名
* @param int $length=9 默认值为9,生成的随机字符串的长度
* @return $newFilwName 返回生成的新文件名
*/
public static function createNewFileName($type,$length=9){
//获取当前时间
$newFilwName = date("YmdHis");
//生成随机名的一部分
for($i=0;$i<$length;$i++){
$newFilwName .= mt_rand(1,9);
}
$newFilwName .= ".".$type;
return $newFilwName;
}
public static function thumbnailCreateSave($filePath,$width=100,$height=40){
$param = func_get_args();
$counts = count($param);
//判断合法性
$result = self::fileExists($filePath)?(self::fileLegal($filePath)?:false):false;
if(!$result)return false;
//打开文件资源
$funcMethod = self::variableMethod('imagecreatefrom',self::$type);
$srcImage = $funcMethod($filePath);
if(!$srcImage)return false;
//创建缩略图资源
$srcTHumb = imagecreatetruecolor($width,$height);
//采样数据
$result &= imagecopyresampled($srcTHumb,$srcImage,0,0,0,0,$width,$height,self::$src_width,self::$src_height);
if(!$result)return false;
//保存输出
$thumbnailName = self::createNewFileName(self::$type);
$funcMethod = self::variableMethod('image',self::$type);
if($counts==4){
$newpath = $param[3];
$newpath = isset($newpath) ? $newpath .= $thumbnailName :false;
$result = $funcMethod($srcTHumb,$newpath);
}else{
// 设置头信息,告诉浏览器以图片格式输出
header("content-type:image/png");
// 清屏,为图片输出创在输出环境
ob_clean();
$result = $funcMethod($srcTHumb);
}
if(!$result)return false;
//销毁资源
imagedestroy($srcTHumb);
imagedestroy($srcImage);
return $newpath;
}
}
//Thumbnail::createTnumbnail($filePath = './42933-106.jpg',400,100);
