内容
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://xhto.cn/archives/282.html
友情提示:添加友联和友联失效404的请联系博主,并确保自己网站已经添加博主为友联!
黑色的背后是黎明... + 赞赏博主吧!已经穷的开不起站了~
真好呢
怎么收藏这篇文章?
怎么收藏这篇文章?