PHP 对图片的裁剪,缩略图 方法(后台方法)

//裁剪
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;
        }
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值