//裁剪
function get_thumb_img($old_src){
$new_src = "******/".time().rand(0,5000).rand(5000,10000).'_n.jpg';
$old_info = getimagesize($old_src);
switch($old_info[2]){
case 1:$im = imagecreatefromgif($old_src);break;
case 2:$im = imagecreatefromjpeg($old_src);break;
case 3:$im = imagecreatefrompng($old_src);break;
case 4:$im = imagecreatefromjpeg("/img/swf.jpg");break;
case 6:return false;
}
$old_width = imagesx($im);
$old_height = imagesy($im);
//创建源图的实例
$src = imagecreatefromstring(file_get_contents($old_src));
//裁剪开区域左上角的点的坐标
$x = 0;
$y = 0;
//裁剪区域的宽和高
$height = $old_height;
$width = $height*1.32;
//最终保存成图片的宽和高,和源要等比例,否则会变形
$final_width = $width;
$final_height = round($final_width * $height / $width);
//将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升
$new_image = imagecreatetruecolor($final_width, $final_height);
imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height);
//输出图片
//imagejpeg($new_image);
imagejpeg($new_image,$new_src,100);//对图像进行截图
imagedestroy($src);
imagedestroy($new_image);
$url = str_replace('/opt****2/baby','http://w******om',$new_src);
return $url;
}
//缩略图
function weekend_cut_photo($old_src,$new_src,$new_width,$new_height,$rate=100){
//参数:源文件地址,缩略图地址,缩略图宽,缩略图高,缩略图质量:0~100
//成功返回1,格式不符合返回2,生成图片失败返回3
//长宽不高于给定参数
$old_info = getimagesize($old_src);
switch($old_info[2]){
case 1:$im = imagecreatefromgif($old_src);break;
case 2:$im = imagecreatefromjpeg($old_src);break;
case 3:$im = imagecreatefrompng($old_src);break;
case 4:$im = imagecreatefromjpeg("/img/swf.jpg");break;
case 6:return false;
}
if(!$im) return 2;
$old_width = imagesx($im);
$old_height = imagesy($im);
if($old_width<$new_width && $old_height<$new_height){
imagejpeg($im,$new_src,$rate);
imagedestroy($im);
return 1;
}
$x_rate = $old_width/$new_width;
$y_rate = $old_height/$new_height;
if($x_rate<$y_rate){
$dst_x = ceil($old_width/$y_rate);
$dst_y = $new_height-1;
$new_start_x = 0;
$new_start_y = 0;
}else{
$dst_x = $new_width;
$y_rate = $old_height/$new_height;
if($x_rate<$y_rate){
$dst_x = ceil($old_width/$y_rate);
$dst_y = $new_height-1;
$new_start_x = 0;
$new_start_y = 0;
}else{
$dst_x = $new_width;
$dst_y = ceil($old_height/$x_rate);
$new_start_x = 0;
$new_start_y = 0;
}
$newim = imagecreatetruecolor($dst_x,$dst_y);//先压缩
$bg = imagecolorallocate($newim,255,255,255);
imagefilledrectangle($newim,0,0,$dst_x,$dst_y,$bg); //画个大小一致矩形充当背景
imagecopyresampled($newim,$im,0,0,0,0,$dst_x,$dst_y,$old_width,$old_height);
$cutim = imagecreatetruecolor($dst_x,$dst_y);//对图像进行截图
imagecopyresampled($cutim,$newim,0,0,$new_start_x,$new_start_y,$new_width,$new_height,$new_width,$new_height);
imagejpeg($cutim,$new_src,$rate);//对图像进行截图
imagedestroy($im);
imagedestroy($newim);
$a = imagedestroy($cutim);
if($a){
return 1;
}else{
return 3;
}
}
}