欢迎来到代码驿站!

当前位置:首页 >

php实现图片裁剪

时间:2019-11-18 23:32:38|栏目:|点击:

我们经常要用到图片的裁剪和压缩,例如在生成小程序朋友圈分享图的时候,需要裁剪原缩略图,本文举例说明php如何实现图片 裁剪 ,可以直接使用。

用到的图片处理函数

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 源图象的高度。
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);

上一篇:js如何准确获取当前页面url网址信息

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

本文标题:php实现图片裁剪

本文地址:http://www.codeinn.net/misctech/2154.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有