沉浸式阅读
Beta
转载

php原生图片合成和文字生成图片

     订阅 PHP     2022-08-28     www.php.cn    网络转载    725    0    1    1    2022-08-28
学习要点:请用代码测试

之前有一个我写的帖子是可以拿到远程地址的图片保存本地的
前段时间搞的很火的朋友圈标签,可以使用下面仅仅三个自定义函数即可合成,具体参数石头已经给大家写出,有需要改进的地方请直接留言!

function image_copy_image($bg_image, $sub_image, $add_x, $add_y, $add_w, $add_h, $out_image) {
    if ($sub_image) {
        $bg_image_c = imagecreatefromstring(file_get_contents($bg_image));
        $sub_image_c = imagecreatefromstring(file_get_contents($sub_image));
        imagecopyresampled($bg_image_c, $sub_image_c, $add_x, $add_y, 0, 0, $add_w, $add_h, imagesx($sub_image_c), imagesy($sub_image_c));
        //保存到out_image
        imagejpeg($bg_image_c, $out_image, 80);
        imagedestroy($sub_image_c);
        imagedestroy($bg_image_c);
    }
}

function create_text($filename, $text, $font, $size, $width_f, $red, $grn, $blu) {
    $rot = 0; // 旋转角度
    $width = 0; //宽度
    $height = 0; //高度
    $offset_x = 0; //x偏移
    $offset_y = 0; //y偏移
    $bounds = array();
    $text = autowrap($size, 0, $font, $text, $width_f); // 自动换行处理
    
    // 确定边框高度.
    $bounds = ImageTTFBBox($size, $rot, $font, "W");
    if ($rot < 0) {
        $font_height = abs($bounds[7] - $bounds[1]);
    } else if ($rot > 0) {
        $font_height = abs($bounds[1] - $bounds[7]);
    } else {
        $font_height = abs($bounds[7] - $bounds[1]);
    }
    // 确定边框高度.
    $bounds = ImageTTFBBox($size, $rot, $font, $text);
    if ($rot < 0) {
        $width = abs($bounds[4] - $bounds[0]);
        $height = abs($bounds[3] - $bounds[7]);
        $offset_y = $font_height;
        $offset_x = 0;
    } else if ($rot > 0) {
        $width = abs($bounds[2] - $bounds[6]);
        $height = abs($bounds[1] - $bounds[5]);
        $offset_y = abs($bounds[7] - $bounds[5]) + $font_height;
        $offset_x = abs($bounds[0] - $bounds[6]);
    } else {
        $width = abs($bounds[4] - $bounds[6]);
        $height = abs($bounds[7] - $bounds[1]);
        $offset_y = $font_height;
        $offset_x = 0;
    }
    $bg = imagecreatetruecolor($width + 20, $height + 20); // 创建画布
    $color = imagecolorallocatealpha($bg, 0, 0, 0, 127); //拾取一个完全透明的颜色
    imagealphablending($bg, false); //关闭混合模式,以便透明颜色能覆盖原画布
    imagefill($bg, 0, 0, $color); //填充
    imagesavealpha($bg, true); //设置保存PNG时保留透明通道信息
    $textImg = imagecolorallocate($bg, $red, $grn, $blu); // 创建白色
    ImageTTFText($bg, $size, 0, 10, $size + 10, $textImg, $font, $text);
    imagepng($bg, $filename);
}

function autowrap($fontsize, $angle, $fontface, $string, $width) {
    $content = "";
    // 将字符串拆分成一个个单字 保存到数组 letter 中
    preg_match_all("/./u", $string, $arr);
    $letter = $arr[0];
    foreach($letter as $l) {
        $teststr = $content.
        " ".$l;
        $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
        // 判断拼接后的字符串是否超过预设的宽度
        if (($testbox[2] > $width) && ($content !== "")) {
            $content. = PHP_EOL;
        }
        $content. = $l;
    }
    return $content;
}


本文标题: php原生图片合成和文字生成图片

本文链接: https://www.mbkfw.com/course/1107.html (转载时请注明来源链接)

本文说明: 本文来源于网络转载,有问题请发送至: 邮箱/kf@dtmuban.com 进行修改或删除

特别鸣谢: 如果您觉得本文对您有帮助,请给我们一个小小的赞,收藏本文更利于反复学习哦!

 
本文标签: #海报 #生成图片
destoon程序前端开发标签生成器

下班PC阅读不方便?

手机也可以随时学习开发

微信关注公众号“商企云服”
"模板开发网前端开发教学"
每日干货技术分享
 
0

圈友点评

文明上网理性发言,请遵守网络评论服务协议

小编推荐



色彩
×

《客户实名在线注册登记》售后一直都在!

关注

微信
关注

微信扫一扫
不同的环境体验

幸运大转盘,好礼等您拿

模板开发网公众号

模板开发网微信小程序

代授权

程序
授权

黑小二

联系
客服

很高兴为您服务
尊敬的用户,欢迎您咨询,我们为新用户准备了优惠好礼。咨询客服

联系客服:

在线QQ: 290948585

客服电话: 18605917465

E_mail邮箱: kf@dtmuban.com

微信公众号: 商企云服

微信小程序: 模板开发

QQ客服 微信客服DT授权代办 在线交谈 智能小云

工作时间:

周一至周五: 09:00 - 18:00

APP下载

安卓
APK

模板开发网安卓版APP

反馈

我要
反馈