首页 > PHP > PHP 生成海报带二维码和原型头像,文字定义功能

PHP 生成海报带二维码和原型头像,文字定义功能

2025-03-08 14:20:54
<?php
require 'vendor/autoload.php';

use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeEnlarge;

// 高清画布设置(放大2倍后缩小)
$scale = 2; // 先放大处理最后缩小输出
$width = 750 * $scale;
$height = 1334 * $scale;

// 1. 创建高清画布(真彩色+抗锯齿)
$poster = imagecreatetruecolor($width, $height);
imageantialias($poster, true); // 启用抗锯齿
$white = imagecolorallocatealpha($poster, 255, 255, 255, 0);
imagefill($poster, 0, 0, $white);

// 2. 高质量背景图处理
$bg = imagecreatefromjpeg('background.jpg');
// 使用高质量缩放算法
imagecopyresampled($poster, $bg, 
    0, 0, 0, 0, 
    $width, $height, 
    imagesx($bg), imagesy($bg));

// 3. 优化圆形头像(带抗锯齿)
function create_avatar($path, $size) {
    $src = imagecreatefromstring(file_get_contents($path));
    $src_width = imagesx($src);
    $src_height = imagesy($src);
    
    // 创建透明画布
    $avatar = imagecreatetruecolor($size, $size);
    imageantialias($avatar, true);
    imagesavealpha($avatar, true);
    $transparent = imagecolorallocatealpha($avatar, 0, 0, 0, 127);
    imagefill($avatar, 0, 0, $transparent);
    
    // 创建圆形蒙版
    $mask = imagecreatetruecolor($size, $size);
    imageantialias($mask, true);
    $black = imagecolorallocate($mask, 0, 0, 0);
    $white = imagecolorallocate($mask, 255, 255, 255);
    imagefill($mask, 0, 0, $black);
    imagefilledellipse($mask, $size/2, $size/2, $size, $size, $white);
    
    // 高质量缩放+圆形裁剪
    imagecopyresampled($avatar, $src, 0, 0, 0, 0, $size, $size, $src_width, $src_height);
    imagecopy($avatar, $mask, 0, 0, 0, 0, $size, $size);
    imagedestroy($mask);
    
    // 添加抗锯齿边缘
    imagefilter($avatar, IMG_FILTER_SMOOTH, 7);
    return $avatar;
}

$avatar = create_avatar('avatar.jpg', 150 * $scale);
imagecopy($poster, $avatar, 50 * $scale, 50 * $scale, 0, 0, 150 * $scale, 150 * $scale);

// 4. 优化文字渲染
function add_text($image, $text, $fontSize, $x, $y, $color, $maxWidth, $fontFile, $scale=1) {
    $fontSize *= $scale;
    $x *= $scale;
    $y *= $scale;
    $maxWidth *= $scale;
    
    // 使用更精确的换行计算
    $lines = [];
    $currentLine = '';
    $words = preg_split('/(?<!^)(?!$)/u', $text); // 按字符分割
    
    foreach ($words as $word) {
        $testLine = $currentLine . $word;
        $box = imagettfbbox($fontSize, 0, $fontFile, $testLine);
        $textWidth = $box[2] - $box[0];
        
        if ($textWidth <= $maxWidth) {
            $currentLine = $testLine;
        } else {
            $lines[] = $currentLine;
            $currentLine = $word;
        }
    }
    $lines[] = $currentLine;
    
    // 带阴影的文字渲染
    foreach ($lines as $key => $line) {
        $textY = $y + ($key * ($fontSize * 1.2));
        $shadowColor = imagecolorallocatealpha($image, 0, 0, 0, 50);
        imagettftext($image, $fontSize, 0, $x+2, $textY+2, $shadowColor, $fontFile, $line);
        imagettftext($image, $fontSize, 0, $x, $textY, $color, $fontFile, $line);
    }
}

$fontColor = imagecolorallocate($poster, 51, 51, 51);
add_text($poster, "欢迎参加PHP开发者大会\n2024年度技术峰会", 28, 50, 250, $fontColor, 600, 'msyh.ttf', $scale);

// 5. 高清二维码生成
$qrCode = new QrCode('https://example.com/invite');
$qrCode->setRoundBlockSizeMode(new RoundBlockSizeModeEnlarge());
$qrCode->setSize(800); // 提高基础尺寸
$qrCode->setMargin(20);

$writer = new PngWriter();
$qrResult = $writer->write($qrCode);

// 使用GD库直接处理代替临时文件
$qrImage = imagecreatefromstring($qrResult->getString());
$qr_width = imagesx($qrImage);
$qr_height = imagesy($qrImage);

// 高质量缩放
imagecopyresampled(
    $poster, 
    $qrImage, 
    500 * $scale, 1000 * $scale, 
    0, 0, 
    200 * $scale, 200 * $scale, 

主要优化点说明:

  1. 高清画布处理

  • 使用2倍尺寸画布进行绘制,最后缩小输出

  • 启用imageantialias()全局抗锯齿

  • 使用imagecreatetruecolor()代替普通画布

  1. 图像缩放优化

  • imagecopyresized替换为imagecopyresampled

  • 对头像和二维码使用高质量缩放算法

  1. 圆形头像优化

  • 使用透明通道代替蒙版合并

  • 添加imagefilter(IMG_FILTER_SMOOTH)

  • 优化边缘抗锯齿处理

  1. 文字渲染优化

  • 增加文字阴影提升清晰度

  • 改进换行算法使用字符级分割

  • 支持高清缩放字体渲染

  1. 二维码增强

  • 设置更大的基础尺寸(800px)

  • 使用RoundBlockSizeModeEnlarge模式

  • 增加边距(margin)设置

  1. 输出优化

  • 最终使用imagecopyresampled缩小输出

  • 设置imagepng()质量为9(最高)

  • 移除临时文件依赖

 

 

使用 Ctrl+D 可将网站添加到书签
收藏网站
扫描二维码
关注早实习微信公众号
官方公众号
Top