php图片自动裁剪图片,php裁剪图片(支持定点裁剪)

/**

* 图片裁剪函数,支持指定定点裁剪和方位裁剪两种裁剪模式

* @param $src_file 原图片路径

* @param $new_width 裁剪后图片宽度(当宽度超过原图片宽度时,去原图片宽度)

* @param $new_height 裁剪后图片高度(当宽度超过原图片宽度时,去原图片高度)

* @param $type 裁剪方式,1-方位模式裁剪;0-定点模式裁剪。

* @param $pos 方位模式裁剪时的起始方位(当选定点模式裁剪时,此参数不起作用)

* 1为顶端居左,2为顶端居中,3为顶端居右;

* 4为中部居左,5为中部居中,6为中部居右;

* 7为底端居左,8为底端居中,9为底端居右;

* @param $start_x 起始位置X (当选定方位模式裁剪时,此参数不起作用)

* @param $start_y 起始位置Y(当选定方位模式裁剪时,此参数不起作用)

* @return 裁剪图片存储路径*/

function thumb($src_file, $new_width, $new_height, $type = 1, $pos = 5, $start_x = 0, $start_y = 0) {$pathinfo = pathinfo($src_file);$dst_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] .'_'. $new_width . 'x' . $new_height . '.' . $pathinfo['extension'];if (!file_exists($dst_file)) {if ($new_width < 1 || $new_height < 1) {echo "params width or height error !";return;//exit();

}if (!file_exists($src_file)) {echo $src_file . " is not exists !";return;//exit();

}$img_type=pathinfo($src_file,PATHINFO_EXTENSION);$img_type=strtolower($img_type);/*载入图像*/

switch ($img_type) {case 'jpg':

if(@!($src_img= imagecreatefromjpeg($src_file))){if(@!($src_img = imagecreatefrompng($src_file))){$src_img = imagecreatefromgif($src_file);

}

}break;case 'png':

if(@!($src_img= imagecreatefrompng($src_file))){if(@!($src_img = imagecreatefromjpeg($src_file))){$src_img = imagecreatefromgif($src_file);

}

}break;case 'gif':

if(@!($src_img= imagecreatefromgif($src_file))){if(@!($src_img = imagecreatefrompng($src_file))){$src_img = imagecreatefromjpeg($src_file);

}

}break;default:

echo "载入图像错误!";return;//exit();

}/*获取源图片的宽度和高度*/

$src_width = imagesx($src_img);$src_height = imagesy($src_img);/*计算剪切图片的宽度和高度*/

$mid_width = ($src_width < $new_width) ? $src_width : $new_width;$mid_height = ($src_height < $new_height) ? $src_height : $new_height;/*初始化源图片剪切裁剪的起始位置坐标*/

switch ($pos * $type) {case 1://1为顶端居左

$start_x = 0;$start_y = 0;break;case 2://2为顶端居中

$start_x = ($src_width - $mid_width) / 2;$start_y = 0;break;case 3://3为顶端居右

$start_x = $src_width - $mid_width;$start_y = 0;break;case 4://4为中部居左

$start_x = 0;$start_y = ($src_height - $mid_height) / 2;break;case 5://5为中部居中

$start_x = ($src_width - $mid_width) / 2;$start_y = ($src_height - $mid_height) / 2;break;case 6://6为中部居右

$start_x = $src_width - $mid_width;$start_y = ($src_height - $mid_height) / 2;break;case 7://7为底端居左

$start_x = 0;$start_y = $src_height - $mid_height;break;case 8://8为底端居中

$start_x = ($src_width - $mid_width) / 2;$start_y = $src_height - $mid_height;break;case 9://9为底端居右

$start_x = $src_width - $mid_width;$start_y = $src_height - $mid_height;break;default://随机

break;

}//为剪切图像创建背景画板

$mid_img = imagecreatetruecolor($mid_width, $mid_height);//拷贝剪切的图像数据到画板,生成剪切图像

imagecopy($mid_img, $src_img, 0, 0, $start_x, $start_y, $mid_width, $mid_height);//为裁剪图像创建背景画板

$new_img = imagecreatetruecolor($new_width, $new_height);//拷贝剪切图像到背景画板,并按比例裁剪

imagecopyresampled($new_img, $mid_img, 0, 0, 0, 0, $new_width, $new_height, $mid_width, $mid_height);/*按格式保存为图片*/

switch ($img_type) {case 'jpg':imagejpeg($new_img, $src_file, 100);break;case 'png':imagepng($new_img, $src_file, 9);break;case 'gif':imagegif($new_img, $src_file, 100);break;default:

break;

}

}return ltrim($src_file, '.');

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值