php实现图片裁剪

function tailoringImg($file_path,$save_width,$start_spot_x,$start_spot_y,$width,$height,$display=1)
{
    if(file_exists($file_path) && is_readable($file_path))
    {
        //从字符串中的图像流新建一图像
        $src = imagecreatefromstring(file_get_contents($file_path));

        //保存图片的高
        $save_height = round($save_width*$height/$width);

        //根据要保存的宽和高创建图片
        $new_image = imagecreatetruecolor($save_width, $save_height);

        //生成最后的图片
        imagecopyresampled($new_image, $src, 0, 0, $start_spot_x, $start_spot_y, $save_width, $save_height, $width, $height);

        //直接展示
        if($display)
        {
            header('Content-Type: image/jpeg');
            imagejpeg($new_image);
            imagedestroy($src);
            imagedestroy($new_image);
        }

    }
    else
    {
        echo '文件查看失败';//记录日志
    }
}

//将图片进行裁剪
$file_path = './cs.jpg';
$save_width = 200;
$start_spot_x = 10;
$start_spot_y = 15;
$width = 100;
$height = 100;

tailoringImg($file_path,$save_width,$start_spot_x,$start_spot_y,$width,$height,1);

用到的图片处理函数

php图像处理函数imagecopyresampled
语法

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
参数
dst_image 目标图象连接资源。
src_image 源图象连接资源。
dst_x 目标 X 坐标点。
dst_y 目标 Y 坐标点。
src_x 源的 X 坐标点。
src_y 源的 Y 坐标点。
dst_w 目标宽度。
dst_h 目标高度。
src_w 源图象的宽度。
src_h 源图象的高度。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mr.杰瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值