智能cut php 切割图片

/**
	 * 智能裁剪 裁剪中间部门
	 * @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;
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值