图片裁切函数

 * 图片裁切或者同比例缩放
 * @author shaobaojie by 26416@163.com
 * @param $url 图片地址
 * @param $maxwidth 最大宽度
 * @param $maxheight 最大高度
 * @param $crop 是否按照最大尺寸比例裁切 默认为否
 */
function picture_sf($url,$maxwidth='700',$maxheight='700',$crop=false){
	$suffix = substr(strrchr($url,'.'),1); //strrchr返回最后的.jpg 或者.jpeg或png或gif
	$style = strtolower($suffix);
	if($style=='jpg' || $style=='jpeg'){
		$originail = imagecreatefromjpeg($url); //jpg
	}elseif($style=='png'){
		$originail = imagecreatefrompng($url); //png
	}elseif($style=='gif'){
		$originail = imagecreatefromgif($url); //gif
	}
	$dims = getimagesize($url); //获取图片大小;
	$imgInfo='';$imgInfo['width']=$dims['0'];$imgInfo['height']=$dims['1'];
	if($crop){
		$size=getNewMaxSize($maxwidth, $maxheight, $imgInfo);
		//如果图片的比例大小低于指定图片大小则不裁切,同比例缩小并且复制图片
		$where = $size['width'] <= $maxwidth && $size['height']<=$maxheight;
		}else{
	    $size=getNewSize($maxwidth, $maxheight, $imgInfo);
		$where = $size['width']==$imgInfo['width']&&$size['height']==$imgInfo['height'];
		}  
	if($where){   
		$newim = imagecreatetruecolor($size['width'],$size['height']);
		imagecopyresampled($newim,$originail ,0,0,0,0,$size['width'],$size['height'],$dims['0'],$dims['1']);  
		 if($style=='jpg' || $style=='jpeg'){
					  imagejpeg($newim,$url,100);
				  }elseif($style=='png'){
					  imagepng($newim,$url,9);
				  }elseif($style=='gif'){
					  imagegif($newim,$url,100);
				  } 
		imagedestroy($newim);  
	}else {
		//创建空白图像
		$newim=imagecreatetruecolor($size['width'],$size['height']);
		imagecopyresampled($newim,$originail ,0,0,0,0,$size['width'],$size['height'],$dims['0'],$dims['1']); 
		if($crop){
			//创建空白图像
			  $newim2 = imagecreatetruecolor($maxwidth,$maxheight);
			  //重采样拷贝部分图像并调整大小
			  if($size['width']>$maxwidth){
				$widthstart=$size['width']/2-$maxwidth/2;
				imagecopyresampled($newim2,$newim,0,0,$widthstart,0,$maxwidth,$maxheight,$maxwidth,$maxheight);
			   }
			  if($size['height']>$maxheight){
				$heightstart=$size['height']/2-$maxheight/2;
				imagecopyresampled($newim2,$newim,0,0,0,$maxheight,$maxwidth,$maxheight,$maxwidth,$maxheight);
			  }
			}
		if($crop){
			if($style=='jpg' || $style=='jpeg'){
			    imagejpeg($newim2,$url,100);
			}elseif($style=='png'){
				imagepng($newim2,$url,9);
			}elseif($style=='gif'){
				imagegif($newim2,$url,100);
			} 
			}else{ 	
			  if($style=='jpg' || $style=='jpeg'){
				  imagejpeg($newim,$url,100);
			  }elseif($style=='png'){
				  imagepng($newim,$url,9);
			  }elseif($style=='gif'){
				  imagegif($newim,$url,100);
			  } 
		}
		//ImageJpeg($newim,$to_File,100);
		/* 资源回收 */
		if($crop){
			imagedestroy($newim2);
			}
		imagedestroy($originail);
		imagedestroy($newim);
	} 
}
//同比例缩放按照最小比例 返回最小比例缩放尺寸
function getNewSize($width, $height,$imgInfo){
	if($width/$imgInfo['width'] < $height/$imgInfo['height']){
		$b=$width/$imgInfo['width'];
	}else{
		$b=$height/$imgInfo['height'];
	}
	$size['width']=$b*$imgInfo['width'];
	$size['height']=$b*$imgInfo['height'];
	return $size; 
}
//按照同比例缩放按照最大比例 返回最大比例尺寸 并且当真实图片大小超过所裁切范围的时候才进行裁切
function getNewMaxSize($width, $height,$imgInfo){
	if($width<$imgInfo['width']||$height<$imgInfo['height']){
		$rw=false;
		$rh=false;
		$ws=0;
		$rh=0;
		if($width<$imgInfo['width']){
			$ws=$width/$imgInfo['width'];
			$rw=true;
		}	
		if($height<$imgInfo['height']){
			$hs=$height/$imgInfo['height'];
			$rh=true;
		}  
		@$b=$ws>$hs?$ws:$hs; 
		$size['width']=$b*$imgInfo['width'];
		$size['height']=$b*$imgInfo['height'];
	}else{
		$size['width']=$imgInfo['width'];
		$size['height']=$imgInfo['height'];
	} 
	return $size;
}



 
 
 
 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值