/**
* 智能裁剪 裁剪中间部门
* @param int $width 要裁剪的宽度
* @param int $height 要裁剪的长度
* @param string $imgpath 来源图片地址
* @param string $topath 生成图片地址
* @param boolean $isPadding 是要填充模式吗
* @param string $paddingColor 填充颜色
* @return return_type
* @author yuminkang
* @date 2014-4-25下午02:04:00
* @version v1.0.0
*/
public static function autoCut( $width, $height, $imgpath, $topath, $isPadding =false , $paddingColor = 'FFFFFF' ){
list($src_w,$src_h)=getimagesize($imgpath);
$oriwidth = $src_w;
$oriheight = $src_h;
$x= $y = $tox = $toy = 0;
$source=imagecreatefromjpeg($imgpath);
$target = imagecreatetruecolor( $width, $height );//画布
if($isPadding == false){//是否是填充
if($src_w/$src_h > $width/$height){//原图较宽则水平截取
$oriheight = $src_h;
$oriwidth = $oriheight*$width/$height;
$x = ($src_w-$oriwidth)/2;
}else{
$oriwidth = $src_w;
$oriheight = $oriwidth*$height/$width;
$y = ($src_h-$oriheight)/2;
}
}else{
if(strlen($paddingColor)<6){
$paddingColor = 'FFFFFF';
}
$paddingColor = strtoupper($paddingColor);
$colorx = '0x'.substr($paddingColor,0,2);
$colory = '0x'.substr($paddingColor,2,4);
$colorz = '0x'.substr($paddingColor,4,6);
$color = imagecolorallocate($target, $colorx, $colory, $colorz);
imagefill($target, 0, 0, $color);
if($src_w/$src_h > $width/$height){//原图较宽则垂直方向填充 及是$toy 不是0
$oldheight = $height;
$height = $width*$src_h/$src_w;//得到高
$toy = ($oldheight-$height)/2;
}else{
$oldwidth = $width;
$width = $height*$src_w/$src_h;//得到宽
$tox = ($oldwidth-$width)/2;
}
}
imagecopyresampled($target,$source,$tox,$toy,$x,$y,$width,$height,$oriwidth,$oriheight);
imagejpeg($target,$topath);
imagedestroy($target);
if(!is_file($topath)){
return false;
}
return true;
}
智能cut php 切割图片
最新推荐文章于 2023-12-05 14:13:11 发布