/**
* 将文字打印在图片上并输出
* @param string $data 显示在图片上的文字
* @return string
*/
function imgaddtext($data = '测试文字')
{
//底层模板路径
$filename = './static/common/images/muban.jpg';
//字体路径(字体在服务器上的绝对路径)
$font = realpath('./static/common/font/mnjt.ttf');
//获取图片的属性,第一个宽度,第二个高度,类型1=>gif,2=>jpeg,3=>png
list($width, $height, $type) = getimagesize($filename);
//可以处理的图片类型
$types = array(1 => "gif", 2 => "jpeg", 3 => "png",);
//通过图片类型去组合,可以创建对应图片格式的,创建图片资源的GD库函数
$createfrom = "imagecreatefrom" . $types[$type];
//通过“变量函数”去打对应的函数去创建图片的资源
$image = $createfrom($filename);
//设置字体的颜色(rgb格式)
$textcolor = imagecolorallocate($image, 48, 42, 40);
//x轴居中计算(需要自行测试字体宽度和字体大小的关系,例如下面50号字对应计算宽度为67)
$x = ($width - 67 * mb_strlen($data, 'utf8')) / 2;//(图片总宽-每个字宽度*字数)/2得出这行字在x轴居中位置
//参数分别为:模板地址,文字大小、旋转角度(以第一个字左边为中心旋转)、x轴位置,y轴位置,文字颜色,文字字体,文字内容
imagefttext($image, 50, 0, $x, 650, $textcolor, $font, $data);
//通过图片类型去组合保存对应格式的图片函数
$output = "image" . $types[$type];
//设置保存路径
$save_path = './uploads/cert/' . date('Ymd') . '/';
//设置保存时的图片名称
$new_name = time() . rand(1000, 9999) . '.' . $types[$type];
//当保存路径不存在时自动创建
if (!is_dir($save_path)) {
mkdir($save_path, 0777, true);
}
//通过变量函数去保存对应格式的图片到指定路径
$output($image, $save_path . $new_name);
//去掉左边第一个点,防止显示时相对路径错误
$save_path = ltrim($save_path, '.');
//释放图片资源
imagedestroy($image);
//return $save_path . $new_name;
return "";
} 如果帮助到你,请赏杯奶茶喝~
- 本文链接: https://www.shx1024.top//index/article/details/article_id/101.shtml
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。