* 图片裁切或者同比例缩放
* @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;
}