/**
* # 缩放图片大小
* @param $path
* @param $max_width
* @param $max_height
* @param $img_name
* @param $img_path
* @param $img_type
* @return void
*/
function ResizeImage($path,$max_width,$max_height,$img_name,$img_path,$img_type)
{
# 获取图片信息
$img = imagecreatefromstring(file_get_contents($path));
# 获取当前图片信息
list($pic_width, $pic_height) = getimagesize($path);
# 获取长倍数
$widthratio = $max_width/$pic_width;
# 获取宽倍数
$heightratio = $max_height/$pic_height;
# 获取确定的缩放倍数
$ratio = $widthratio > $heightratio ? $widthratio : $heightratio;
# 获取新长宽
$new_width = $pic_width * $ratio;
$new_height = $pic_height * $ratio;
if(function_exists("imagecopyresampled")){
$new_img = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_img,$img,0,0,0,0,$new_width,$new_height,$pic_width,$pic_height);
}else{
$new_img = imagecreate($new_width,$new_height);
imagecopyresized($new_img,$img,0,0,0,0,$new_width,$new_height,$pic_width,$pic_height);
}
# 获取放大后的图片地址
$path = "{$img_path}/{$img_name}.{$img_type}";
# 判断图片类型调用不同图片方法
switch($img_type){
case 'png':
imagepng($new_img,$path);
break;
case 'jpeg':
imagejpeg($new_img,$path);
break;
}
# 销毁新图
imagedestroy($new_img);
}
PHP 图片缩放
最新推荐文章于 2024-06-14 15:13:53 发布