php裁剪图片,并给图片加上水印

本次以裁剪四个图片为例,图片如下

代码如下

bash 复制代码
public function cutImg($imgUrl){
        try{
            // 读取原始图片
            $src_img = imagecreatefromjpeg($imgUrl);

            // 获取原始图片的宽度和高度
            $src_width = imagesx($src_img);
            $src_height = imagesy($src_img);

            // 计算每个部分的宽度和高度
            $part_width = $src_width / 2;
            $part_height = $src_height / 2;

            // 创建4个新的图片
            $part1_img = imagecreatetruecolor($part_width, $part_height);
            $part2_img = imagecreatetruecolor($part_width, $part_height);
            $part3_img = imagecreatetruecolor($part_width, $part_height);
            $part4_img = imagecreatetruecolor($part_width, $part_height);

            // 将原始图片的指定部分复制到新的图片中
            imagecopyresampled($part1_img, $src_img, 0, 0, 0, 0, $part_width, $part_height, $src_width / 2, $src_height / 2);
            imagecopyresampled($part2_img, $src_img, 0, 0, $src_width / 2, 0, $part_width, $part_height, $src_width / 2, $src_height / 2);
            imagecopyresampled($part3_img, $src_img, 0, 0, 0, $src_height / 2, $part_width, $part_height, $src_width / 2, $src_height / 2);
            imagecopyresampled($part4_img, $src_img, 0, 0, $src_width / 2, $src_height / 2, $part_width, $part_height, $src_width / 2, $src_height / 2);

            //添加水印
            $text = '用积分可下载原图';
            $textAi = 'AI生图';
            $font = __DIR__ . '/../../../public/static/font/FangZhengShuSongJianTi-1.ttf';  // 字体文件的路径
            $font_size = 20;  // 字体大小
            $font_color = imagecolorallocatealpha($part1_img, 255, 255, 255, 0);  // 字体颜色,这里设置为白色
            // 设置水印文字的位置和边距
//            $margin_left = ($thumbnail_width - mb_strlen($text)) / 2;  // 水印距离右边界的距离
            $textBoundingBox = imagettfbbox($font_size, 0, $font, $text);
            $textWidth = $textBoundingBox[2] - $textBoundingBox[0];
            $margin_left = ($part_width - $textWidth) / 2;  // 水印距离右边界的距离
            $margin_bottom = $part_height / 2;  // 水印距离底部边界的距离

            // 设置文字倾斜角度(以度为单位,逆时针方向为正)
            $text_angle = 0;

            //添加中间水印
            imagettftext($part1_img, $font_size, $text_angle, $margin_left, $margin_bottom, $font_color, $font, $text);
            imagettftext($part2_img, $font_size, $text_angle, $margin_left, $margin_bottom, $font_color, $font, $text);
            imagettftext($part3_img, $font_size, $text_angle, $margin_left, $margin_bottom, $font_color, $font, $text);
            imagettftext($part4_img, $font_size, $text_angle, $margin_left, $margin_bottom, $font_color, $font, $text);

            // 保存4张新生成的图片
            imagejpeg($part1_img, str_replace('.jpg','_sy_1.jpg',$imgUrl),70);
            imagejpeg($part2_img, str_replace('.jpg','_sy_2.jpg',$imgUrl),70);
            imagejpeg($part3_img, str_replace('.jpg','_sy_3.jpg',$imgUrl),70);
            imagejpeg($part4_img, str_replace('.jpg','_sy_4.jpg',$imgUrl),70);

            // 释放内存
            imagedestroy($src_img);
            imagedestroy($part1_img);
            imagedestroy($part2_img);
            imagedestroy($part3_img);
            imagedestroy($part4_img);


            return 1;
        } catch (\ErrorException $e){
            return -1;
        }
    }

裁剪后图片如下

相关推荐
程高兴几秒前
单相交直交变频电路设计——matlab仿真+4500字word报告
开发语言·matlab
我真的不会C43 分钟前
QT中的事件及其属性
开发语言·qt
Chrome深度玩家1 小时前
谷歌翻译安卓版拍照翻译精准度与语音识别评测【轻松交流】
android·人工智能·语音识别
长点点1 小时前
从架构角度了解安卓APP(1):安卓核心组件的设计逻辑与演进
android·架构·app
用户71887350336801 小时前
Android适配最新SplashScreen方案
android·android jetpack
2501_906314322 小时前
优化无头浏览器流量:使用Puppeteer进行高效数据抓取的成本降低策略
开发语言·数据结构·数据仓库
EQ-雪梨蛋花汤2 小时前
【Part 2安卓原生360°VR播放器开发实战】第二节|基于等距圆柱投影方式实现全景视频渲染
android·音视频·vr
让我们一起加油好吗2 小时前
【C++】类和对象(上)
开发语言·c++·visualstudio·面向对象
Railshiqian2 小时前
Framework.jar里的类无法通过Class.forName反射某个类的问题排查
android·反射·framework.jar