php 使用 GD 库生成海报图

GD 库中的一个函数,用于对图像进行重采样和大小调整。要使用这个函数,你需要确保你的 PHP 环境已经安装并启用了 GD 库







 /**
     * 创建海报图
     *
     *
     */
    public function Createposter($user=[],$wxcode=""){

        // 背景图片路径
        $backgroundImagePath = ROOT_PATH.'背景图地址';

        // 添加头像
        $avatarImagePath =  ROOT_PATH.'头像地址';
    

        $avatarImagePath = file_get_contents($avatarImagePath);
        $avatarImage = imagecreatefromstring($avatarImagePath);
        // 二维码图片路径
        $qrCodeImagePath = ROOT_PATH."二维码图片路径";
        // 输出图片路径
        $outputImagePath = ROOT_PATH.'存放图片路径';
        $haibaourl = '存放图片路径'.png';

        // 打开背景图片
        $backgroundImage = imagecreatefrompng($backgroundImagePath);
        // 获取背景图片的宽度和高度
        $backgroundWidth = imagesx($backgroundImage);
        $backgroundHeight = imagesy($backgroundImage);
        // 打开微信二维码图片

        $imageData = file_get_contents($qrCodeImagePath);

        $qrCodeImage = imagecreatefromstring($imageData);
        // 获取微信二维码图片的宽度和高度
        $qrCodeWidth = imagesx($qrCodeImage);
        $qrCodeHeight = imagesy($qrCodeImage);
        // 定义微信二维码在背景图中的位置
        $qrCodeX = ($backgroundWidth - $qrCodeWidth) / 2; // 水平居中
        $qrCodeY = ($backgroundHeight - $qrCodeHeight) / 2; // 垂直居中
        // 将微信二维码合并到背景图中
        imagecopy($backgroundImage, $qrCodeImage, $qrCodeX, $qrCodeY, 0, 0, $qrCodeWidth, $qrCodeHeight);

        // 设置文本颜色
        $textColor = imagecolorallocate($backgroundImage, 0, 0, 0); // 黑色文本
        // 设置字体路径
        $fontPath = ROOT_PATH.'/public/SimHei.ttf'; // 字体文件路径





         $avatarX = 67; // 头像位置 X 坐标
         $avatarY = 110; // 头像位置 Y 坐标
         // 头像的目标宽度和高度
         $targetAvatarWidth = 80;
         $targetAvatarHeight = 80;

         //获取园图
        $createRoundImage = self::createRoundImage($avatarImage,$targetAvatarWidth,$targetAvatarHeight);

         // 获取头像的原始宽度和高度
         $avatarWidth = imagesx($createRoundImage);
         $avatarHeight = imagesy($createRoundImage);


         // 计算头像的缩放比例
         $scale = min($targetAvatarWidth / $avatarWidth, $targetAvatarHeight / $avatarHeight);

         // 计算缩放后的目标宽度和高度
         $newAvatarWidth = $avatarWidth * $scale;
         $newAvatarHeight = $avatarHeight * $scale;

         // 将头像缩放并居中到背景图
         imagecopyresampled($backgroundImage, $createRoundImage, $avatarX, $avatarY, 0, 0, $newAvatarWidth, $newAvatarHeight, $avatarWidth, $avatarHeight);


        // 添加姓名和 ID 文本
        $name = '姓名';
        $id = "ID;
        $fontSize = 20;
        $textBox = imagettfbbox($fontSize, 0, $fontPath, $name . "\n" . $id);
        $textWidth = $textBox[2] - $textBox[0];
        $textHeight = $textBox[1] - $textBox[7];
//        $textX = ($backgroundWidth - $textWidth) / 2; // 水平居中
//        $textX = $backgroundWidth - $textWidth-330;
        $textX = 160;
        $textY = $avatarY + $newAvatarHeight -40; // 头像下方
        imagettftext($backgroundImage, $fontSize, 0, $textX, $textY, $textColor, $fontPath, $name . "\n" . $id);


        // 保存输出图片
        imagepng($backgroundImage, $outputImagePath);
        // 释放内存
        imagedestroy($backgroundImage);
        imagedestroy($qrCodeImage);
        imagedestroy($avatarImage);


       return $haibaourl;

    }







  // 生成圆图
    public function createRoundImage($srcImgOld,$width,$height)
    {
        $srcImg = imagecreatetruecolor(imagesx($srcImgOld), imagesy($srcImgOld));

        // 为源图像设置透明背景
        imagesavealpha($srcImg, true);
        $transColor = imagecolorallocatealpha($srcImg, 0, 0, 0, 127); // 透明色
        imagefill($srcImg, 0, 0, $transColor);

        imagecopy($srcImg, $srcImgOld, 0, 0, 0, 0, imagesx($srcImgOld), imagesy($srcImgOld));

        // 获取图像尺寸
        $imgWidth = imagesx($srcImg);
        $imgHeight = imagesy($srcImg);
        $minSize = min($imgWidth, $imgHeight);

        // 对图像进行中心裁剪,生成正方形
        $srcImg = imagecrop($srcImg, ['x' => ($imgWidth - $minSize) / 2, 'y' => ($imgHeight - $minSize) / 2, 'width' => $minSize, 'height' => $minSize]);

        // 创建新的图像,并启用透明背景
        $newImg = imagecreatetruecolor($minSize, $minSize);
        imagesavealpha($newImg, true);
        $transColour = imagecolorallocatealpha($newImg, 0, 0, 0, 127); // 透明色
        imagefill($newImg, 0, 0, $transColour);

        $r = $minSize / 2; // 圆半径

        // 使用圆形裁剪图像
        for ($x = 0; $x < $minSize; $x++) {
            for ($y = 0; $y < $minSize; $y++) {
                $srcX = round($x * $minSize / $minSize);
                $srcY = round($y * $minSize / $minSize);
                // 判断是否在圆形范围内
                if ((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r)) {
                    $rgbColor = imagecolorat($srcImg, $srcX, $srcY);
                    imagesetpixel($newImg, $x, $y, $rgbColor);
                }
            }
        }

        // 创建缩略图
        $imageResized = imagecreatetruecolor($width, $height);

        // 对于PNG,确保透明度不丢失
        imagealphablending($imageResized, false);
        imagesavealpha($imageResized, true);

        // 复制并调整图片大小,同时保留透明度
        imagecopyresampled($imageResized, $newImg, 0, 0, 0, 0, $width, $height, $minSize, $minSize);
        // 返回处理后的图像
        return $imageResized;

    }

类似这个样式代码生成以后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值