PHP再图上添加多段不同位置的文字再返回图片base64格式数据

落花生 2023-07-26 0 条评论 352 个足迹 PHP语言

首页 / PHP语言 / 正文
星环博客

内容

PHP再图上添加多段不同位置的文字再返回图片base64格式数据

代码

/**
    * @name invitation_generate_image() 智能生成邀请函-图片
    * @author ux
    * @mtime 2023-07-25
    * @request post
    * @param username      嘉宾名称
    * @param time          日期时间
    * @param contact_name1 联系姓名1
    * @param contact_mobile1 联系手机号1
    * @param contact_name2 联系姓名2
    * @param contact_mobile2 联系手机号2
    * @param bgimg         背景图片
    * @param font          字体文件
    * @return page
    */
public function invitation_generate_image()
{
    # 获取数据
    $param = request()->all();
    $param_chars = array_map('htmlspecialchars', $param);
    
    # 验证数据
    if (empty($param_chars['username'])) {
        return array('code'=>400, 'msg'=>'嘉宾名称不可为空!', 'data'=>[]);
    }
    if (empty($param_chars['time']) && empty($param_chars['time2'])) {
        return array('code'=>400, 'msg'=>'日期时间不可为空!', 'data'=>[]);
    }
    if ( (!empty($param_chars['contact_name1']) && !empty($param_chars['contact_mobile1'])) || (!empty($param_chars['contact_name2']) && !empty($param_chars['contact_mobile2'])) ) {
    }else{
        return array('code'=>400, 'msg'=>'联系信息至少为一组不可为空!', 'data'=>[]);
    }
    if ( !empty($param_chars['contact_mobile1']) ) {
        $contact_mobile1_number = preg_replace('/[^0-9]/', '', $param_chars['contact_mobile1']);
        if (strlen($contact_mobile1_number) == 11 && in_array(substr($contact_mobile1_number, 0, 2), ['13', '14', '15', '16', '17', '18', '19'])) {
        }else{
            return array('code'=>400, 'msg'=>'手机号格式不正确!', 'data'=>[]);
        }
    }
    if ( !empty($param_chars['contact_mobile2']) ) {
        $contact_mobile2_number = preg_replace('/[^0-9]/', '', $param_chars['contact_mobile2']);
        if (strlen($contact_mobile2_number) == 11 && in_array(substr($contact_mobile2_number, 0, 2), ['13', '14', '15', '16', '17', '18', '19'])) {
        }else{
            return array('code'=>400, 'msg'=>'手机号格式不正确!', 'data'=>[]);
        }
    }
    
    
    # 默认数据
    $page_id = '750';
    $page_data = DB::table('cms_cat')->select('id','bannerBg','topfile')->where('id', $page_id)->get();
    $page_data_json = json_decode($page_data, true);
    
    $generate_img = ".".$page_data_json[0]['bannerBg'];
    $generate_font = ".".$page_data_json[0]['topfile'];
    
    # 时间数据
    $param_time1 = $param_chars['time'];
    $param_time2 = $param_chars['time2'];
    $param_time = date("Y-m-d H:i:s", strtotime("$param_time1 $param_time2"));
    $param_chars['time'] = $param_time;
    
    # 默认数据替换
    if (!empty($param_chars['bgimg'])) {
        $param_bgimg = $_FILES['bgimg'];
        if ($param_bgimg['error'] !== UPLOAD_ERR_OK) {
            return array('code'=>400, 'msg'=>'图片似乎有点问题,请重新上传图片后在尝试!', 'data'=>[]);
        }
        $cavent_img = $param_bgimg['tmp_name'];
    }else{
        $cavent_img = $generate_img;
    }
    
    if (!empty($param_chars['font'])) {
        $param_font = $_FILES['font'];
        if ($param_font['error'] !== UPLOAD_ERR_OK) {
            return array('code'=>400, 'msg'=>'字体包似乎有点问题,请重新上传后在尝试!', 'data'=>[]);
        }
        $cavent_font = $param_font['tmp_name'];
    }else{
        $cavent_font = $generate_font;
    }
    
    # 获取图片宽高
    $cavent_img_info = getimagesize($cavent_img);
    if (!$cavent_img_info) {
        return array('code'=>400, 'msg'=>'图片信息未获取成功,请联系管理员!', 'data'=>[]);
    }
    
    # 判断图片类型
    if ($cavent_img_info['mime'] === 'image/png') {
        # 制作图片-创建图像
        $image = imagecreatefrompng($cavent_img);
    } elseif ($cavent_img_info['mime'] === 'image/jpeg') {
        # 制作图片-创建图像
        $image = imagecreatefromjpeg($cavent_img);
    } else {
        return array('code'=>400, 'msg'=>'图片格式不正确,请上传PNG或者JPG格式的图片!', 'data'=>[]);
    }
    
    # 设置图像背景颜色和文字颜色;
    $text_color = imagecolorallocate($image, 58, 55, 55);
    
    # 填充背景颜色
    # imagefill($image, 0, 0, $background_color);
    
    # 设置字体文件路径和字体大小
    $font_file = $cavent_font;
    
    # 文字处理
    $handle_param = $param_chars;
    $handle_param['time'] = strtotime($handle_param['time']);
    
    # 添加文字
    $word_name = "尊敬的".$handle_param['username']."嘉宾:";
    $word_contact_name1 = $handle_param['contact_name1'];
    $word_contact_mobile1 = $handle_param['contact_mobile1'];
    $word_contact_name2 = $handle_param['contact_name2'];
    $word_contact_mobile2 = $handle_param['contact_mobile2'];
    
    # 文字在图片上定位
    # 标题
    if (true) {
        $word_x = 125;
        $word_y = 328;
        $for_word = preg_replace('/(?<!^)(?!$)(.)/u', '||$1', $word_name);
        $for_word_arr = explode("||",$for_word);
        for ($i = 0; $i < mb_strlen($word_name, 'UTF-8'); $i++) {
            $character = $for_word_arr[$i];
            imagettftext($image, 31, 0, $word_x, $word_y, $text_color, $font_file, $character);
            # 调整此值来设置间距
            $word_x += 50;
        }
    }else{
        imagettftext($image, 31, 0, 125, 328, $text_color, $font_file, $word_name);
    }
    
    # 内容
    if (true) {
        # 数据
        $handle_word_time = date("m", $handle_param['time'])."月";
        $handle_word_time .= " ".date("d", $handle_param['time'])."日";
        if (true) {
            $handle_word_time_h = date("H:s", $handle_param['time']);
            $handle_word_time_h_word = "";
        }else{
            $handle_word_time_h = date("H", $handle_param['time']);
            if($handle_word_time_h > 12) {
                $handle_word_time_h_word = "下午";
            }else{
                $handle_word_time_h_word = "上午";
            }
        }
        $handle_word_time .= " ".$handle_word_time_h_word." ".$handle_word_time_h;
        # 处理图片
        $word_time = $handle_word_time;
        $word_x = 128;
        $word_y = 437;
        $for_word = preg_replace('/(?<!^)(?!$)(.)/u', '||$1', $word_time);
        $for_word_arr = explode("||",$for_word);
        for ($i = 0; $i < mb_strlen($word_time, 'UTF-8'); $i++) {
            $character = $for_word_arr[$i];
            imagettftext($image, 22.5, 0, $word_x, $word_y, $text_color, $font_file, $character);
            # 调整此值来设置间距
            $word_x += 24;
        }
    }else{
        # 数据
        $handle_word_time = date("m", $handle_param['time'])."月";
        $handle_word_time .= date("d", $handle_param['time'])."日";
        if (true) {
            $handle_word_time_h = date("H:s", $handle_param['time']);
            $handle_word_time_h_word = "";
        }else{
            $handle_word_time_h = date("H", $handle_param['time']);
            if($handle_word_time_h > 12) {
                $handle_word_time_h_word = "下午";
            }else{
                $handle_word_time_h_word = "上午";
            }
        }
        $handle_word_time .= $handle_word_time_h_word.$handle_word_time_h;
        # 处理图片
        if (true) {
            $word_time = $handle_word_time;
        }else{
            $word_time = preg_replace('/(?<!^)(?!$)(.)/u', '  $1', $handle_word_time);
        }
        imagettftext($image, 22, 0, 128, 437, $text_color, $font_file, $word_time);
    }
    
    # 其它数据
    imagettftext($image, 21, 0, 260, 1772, $text_color, $font_file, $word_contact_name1);
    imagettftext($image, 21, 0, 390, 1772, $text_color, $font_file, $word_contact_mobile1);
    imagettftext($image, 21, 0, 260, 1810, $text_color, $font_file, $word_contact_name2);
    imagettftext($image, 21, 0, 390, 1810, $text_color, $font_file, $word_contact_mobile2);
    
    # 将图像输出为数据流
    ob_start();
    if ($cavent_img_info['mime'] === 'image/png') {
        imagepng($image);
    }else{
        imagejpeg($image);
    }
    $image_data = ob_get_clean();
    
    # 销毁图像资源
    imagedestroy($image);
    
    # 发送图像数据流
    if ($cavent_img_info['mime'] === 'image/png') {
        header('Content-Type: image/png');
        $image_ext = "png";
    }else{
        header('Content-Type: image/jpeg');
        $image_ext = "jpeg";
    }
    
    # 将图像数据转换为Base64编码
    $image_data_base = base64_encode($image_data);
    $image_data_base_header = "data:image/png;base64,";
    $image_return = $image_data_base_header.$image_data_base;
    
    # 图片名称
    $image_title = $handle_param['username']."_".date("YmdHs").".".$image_ext;
    
    # 输出数据
    return array('code'=>0, 'msg'=>'生成成功!', 'data'=>$image_return, 'imagetitle'=>$image_title);

版权声明:本文为原创文章,版权归 星环博客 所有,转载请注明出处!

本文链接: https://www.xhto.cn/archives/282.html

友情提示:添加友联和友联失效404的请联系博主,并确保自己网站已经添加博主为友联!

2023年07月26日 17:25:35 © 本文允许各作者规范转载

黑色的背后是黎明... + 赞赏博主吧!已经穷的开不起站了~

上一篇 下一篇
奇思幻想
表情
签到
赞一下
踩一下
  • PNG静图表情包
  • GIF动图表情包

评论事项 ( 请使用真实邮箱地址,以便接收评论回复信息 )

 这颗星球好像还没有被发现~
这里是B612星球,小王子外出寻找她的绵羊了,留言等她吧...
赞赏作者

支付宝

微信

支付宝红包码

[ 赞赏码 ]

打开对应软件 - 扫码赞赏

赞赏请备注好昵称和邮箱,方便公布排行榜

博客信息

文章数目79 篇
评论数目112 条
运行天数3年253天
友链特数0 条

广而告之

当前文章标签

https://www.xhto.cn/usr/themes/only/x-data/url.php
当前网站已经离线 - 星环博客
星环博客+https://www.xhto.cn/
https://www.xhto.cn/usr/themes/only/x-user/xeq_zfb.jpg
https://www.xhto.cn/usr/themes/only/x-user/xeq_wx.jpg
https://www.xhto.cn/usr/themes/only/x-user/xeq_hbm.jpg

即将离开 星环博客

您即将离开星环博客,请注意您的帐号和财产安全。


继续访问

星环博客 的运行时间:

花有重开日,人无再少年。

站点创建时间:2020-08-17 +  版权归属 星环博客 所有 +  © 2024 Copyright +  备案号: 粤ICP备2023084320号 +  不理白驹过隙,不信生死有命 +  页面加载速度:332ms
https://www.xhto.cn/usr/themes/only/x-img/xloading.gif