/**
* * 按照指定的尺寸压缩图片
* * @param $source_path 原图路径
* * @param $imgWidth 目标宽度
* * @param $imgHeight 目标高度
* * @return bool|string
*/
function resize_image($source_path, $imgWidth, $imgHeight)
{
$source_info = getimagesize($source_path);
$source_mime = $source_info['mime'];
switch ($source_mime) {
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
break;
default:
return false;
break;
}
$target_image = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图
imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $imgWidth, $imgHeight, $source_info[0], $source_info[1]);
if(!imagejpeg($target_image, $source_path)){
$source_path = '';
}
imagedestroy($target_image);
return $source_path;
}