PHP 图片缩略图处理类
2018-02-27 本文已影响0人
破晓丨
/**
* 生成缩略图
* @param string $file_name 源图片
* @param string $file_folder
* @param number $quality 缩略图的生成品质(范围:1-100),值越大缩略图的品质越好(缩略图的文件大小跟品质成正比)
* @return multitype:number string
*/
function create_image_small($file_name, $file_folder, $quality = 50 , $file_folder1 = '') {
$imgPathInfo = path_info($file_name);
$imgFile = $file_folder . $file_name;
$result = array();
if (!file_exists($imgFile)) {
$result['status'] = 404;
$result['message'] = "源图片获取失败";
$result['src_file_name'] = $imgFile;
return $result;
}
$imgInfo = getimagesize($imgFile);
if ($imgInfo) {
$imgWidth = $imgInfo[0];
$imgHeight = $imgInfo[1];
$imgType = $imgInfo[2];
$srcImg = null;
switch ($imgType) {
case IMAGETYPE_GIF:
$srcImg = imagecreatefromgif($imgFile);
break;
case IMAGETYPE_JPEG:
$srcImg = imagecreatefromjpeg($imgFile);
break;
case IMAGETYPE_PNG:
$srcImg = imagecreatefrompng($imgFile);
break;
case IMAGETYPE_WBMP:
$srcImg = imagecreatefromwbmp($imgFile);
break;
default:
break;
}
$smallImg = imagecreatetruecolor($imgWidth, $imgHeight);
imagecopyresampled($smallImg, $srcImg, 0, 0, 0, 0, $imgWidth, $imgHeight, $imgWidth, $imgHeight);
if(empty($file_folder1)){
$file_folder1 = $file_folder;
}
$smallFileName = $file_folder1 . $imgPathInfo['filename'] . "_small." . $imgPathInfo['extension'];
imagejpeg($smallImg, $smallFileName, $quality);
imagedestroy($srcImg);
imagedestroy($smallImg);
$result['status'] = 0;
$result['message'] = "缩略图生成成功";
$result['src_file'] = $file_name;
$result['small_file'] = $imgPathInfo['filename'] . "_small." . $imgPathInfo['extension'];
return $result;
}
$result['status'] = 500;
$result['message'] = "源图片大小读取失败";
$result['src_file_name'] = $imgFile;
return $result;
}