mbtdr3ev - 4天前注册会员;
3n44fZgo - 24天前注册会员;
73n5lsCR - 38天前注册会员;
NaZ2pUfG - 47天前注册会员;
bwpKIZwD - 96天前注册会员;
加入我们(62 )

php根据图片背景+标题生成图片封面

php根据图片背景+标题生成图片封面

随时生成图,颜色背景不一样的图,seo优化

已经封装成函数

注意:图片只能是jpg,并且需要字体

<?php
function bgCover($url, $title) {
    // 获取背景图片
    $bg_image = imagecreatefromjpeg($url);
    $bg_width = imagesx($bg_image);
    $bg_height = imagesy($bg_image);

    // 创建一个新图像,大小为背景图片的大小
    $image = imagecreatetruecolor($bg_width, $bg_height);

    // 将背景图片复制到新图像中
    imagecopy($image, $bg_image, 0, 0, 0, 0, $bg_width, $bg_height);

    // 生成随机颜色作为描边色,不能为白色
    do {
        $stroke_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    } while ($stroke_color == imagecolorallocate($image, 255, 255, 255));

    // 生成字体颜色为白色
    $text_color = imagecolorallocate($image, 255, 255, 255);

    // 定义字体路径
    $font = 'SourceHanSansOLD-Bold-2.otf';

    // 设置字体大小
    $font_size = 80;

    // 计算标题文字的宽度和高度
    $text_box = imagettfbbox($font_size, 0, $font, $title);
    $text_width = abs($text_box[2] - $text_box[0]);
    $text_height = abs($text_box[5] - $text_box[3]);

    // 计算标题文字的位置
    $x = ($bg_width - $text_width) / 2;
    $y = ($bg_height - $text_height) / 2 + $text_height;

    // 给标题文字添加描边
    for ($i = 1; $i <= 4; $i++) {
        imagettftext($image, $font_size, 0, $x-$i, $y-$i, $stroke_color, $font, $title);
        imagettftext($image, $font_size, 0, $x+$i, $y-$i, $stroke_color, $font, $title);
        imagettftext($image, $font_size, 0, $x-$i, $y+$i, $stroke_color, $font, $title);
        imagettftext($image, $font_size, 0, $x+$i, $y+$i, $stroke_color, $font, $title);
    }

    // 添加光影效果
    $shadow_color = imagecolorallocatealpha($image, 255, 255, 255, 50);
    for ($i = 1; $i <= 4; $i++) {
        imagefilledellipse($image, $x+$text_width/2, $y+$i*2, $i4, $i4, $shadow_color);
    }
    // 写入标题文字
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $title);

    // 输出图像并销毁图像资源
    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);
}

bgCover('https://image.uisdc.com/wp-content/uploads/2023/02/ysbanner-202300214-2.jpg', '强哥好帅!!!');

改进版:生成图片保存在服务器


<?php
function bgCover($url, $title,$fileUrl="/bg/img") {
    // 获取背景图片
    $bg_image = imagecreatefromjpeg($url);
    $bg_width = imagesx($bg_image);
    $bg_height = imagesy($bg_image);

    // 创建一个新图像,大小为背景图片的大小
    $image = imagecreatetruecolor($bg_width, $bg_height);

    // 将背景图片复制到新图像中
    imagecopy($image, $bg_image, 0, 0, 0, 0, $bg_width, $bg_height);

    // 生成随机颜色作为描边色,不能为白色
    do {
        $stroke_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    } while ($stroke_color == imagecolorallocate($image, 255, 255, 255));

    // 生成字体颜色为白色
    $text_color = imagecolorallocate($image, 255, 255, 255);

    // 定义字体路径
    $font = 'SourceHanSansOLD-Bold-2.otf';

    // 设置字体大小
    $font_size = 80;

    // 计算标题文字的宽度和高度
    $text_box = imagettfbbox($font_size, 0, $font, $title);
    $text_width = abs($text_box[2] - $text_box[0]);
    $text_height = abs($text_box[5] - $text_box[3]);

    // 计算标题文字的位置
    $x = ($bg_width - $text_width) / 2;
    $y = ($bg_height - $text_height) / 2 + $text_height;

    // 给标题文字添加描边
    for ($i = 1; $i <= 4; $i++) {
        imagettftext($image, $font_size, 0, $x-$i, $y-$i, $stroke_color, $font, $title);
        imagettftext($image, $font_size, 0, $x+$i, $y-$i, $stroke_color, $font, $title);
        imagettftext($image, $font_size, 0, $x-$i, $y+$i, $stroke_color, $font, $title);
        imagettftext($image, $font_size, 0, $x+$i, $y+$i, $stroke_color, $font, $title);
    }

    // 添加光影效果
    $shadow_color = imagecolorallocatealpha($image, 255, 255, 255, 50);
    for ($i = 1; $i <= 4; $i++) {
        imagefilledellipse($image, $x+$text_width/2, $y+$i*2, $i4, $i4, $shadow_color);
    }
    // 写入标题文字
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $title);

    // 生成文件名
    $fileName = $_SERVER['DOCUMENT_ROOT'] . $fileUrl . '/' . md5($url . $title) . '.png';
    // 如果图片已经存在,直接返回 URL
    if (file_exists($fileName)) {
        return $fileUrl . '/' . md5($url . $title) . '.png';
    }

    // 将最终的图像保存为 PNG 文件
    imagepng($image, $fileName);

    //释放资源
    imagedestroy($image);

    return $fileUrl . '/' . md5($url . $title) . '.png';

}

echo bgCover('https://image.uisdc.com/wp-content/uploads/2023/02/ysbanner-202300214-2.jpg', '强哥好帅!!!');

声明:本站原创文章文字版权归本站所有,转载务必注明作者和出处;本站转载文章仅仅代表原作者观点,不代表本站立场,图文版权归原作者所有。如有侵权,请联系我们删除。