使用 Imagick 类需 php安装 imagemagick 扩展模块,宝塔可以直接下载安装。
Imagick 类参考文档:https://www.php.net/manual/zh/book.imagick.php
所需的文件均在 https://gitee.com/oackrevv/Imagick
代码:
<?php
// 创建一张白色背景图
$white_bg = './bg_white.png';
if(!file_exists($white_bg)){
$image = imagecreate(300,450);
$white = imagecolorallocate($image, 255, 255, 255);
imagepng($image, $white_bg);
imagedestroy($image);
}
$config = [
'logo' => [
'width' => 30,
'height' => 30,
'radius' => 100,
'x' => 15,
'y' => 30,
],
'logo_name' => [
'fontsize' => 16,
'fontweight' => 400,
'color' => '#080808',
'x' => 50,
'y' => 50,
],
'qr' => [
'width' => 200,
'height' => 200,
'radius' => 0,
'x' => 50,
'y' => 90,
],
'qr_tips' => [
'fontsize' => 12,
'fontweight' => 400,
'color' => '#080808',
'x' => 80,
'y' => 320,
],
'desc' => [
'fontsize' => 12,
'fontweight' => 400,
'color' => '#080808',
'x' => 60,
'y' => 370,
],
];
$font = './microsoftYaHei.ttf';
$logo_url = './logo.jpg';
$qr_code_url = './qr_code.jpg';
$imagick_obj = new Imagick($white_bg);
$composite_default = Imagick::COMPOSITE_DEFAULT;
// logo
$new_image = setImages($logo_url, $config['logo']);
$imagick_obj->compositeImage($new_image, $composite_default, $config['logo']['x'], $config['logo']['y']);// 把一张图合并到一张图
$word = setWord($font, $config['logo_name']);
$imagick_obj->annotateImage($word, $config['logo_name']['x'], $config['logo_name']['y'], 0, '无忧好图库');// 用文本注释图像
// 二维码
$new_image = setImages($qr_code_url, $config['qr']);
$imagick_obj->compositeImage($new_image, $composite_default, $config['qr']['x'], $config['qr']['y']);
$word = setWord($font, $config['qr_tips']);
$imagick_obj->annotateImage($word, $config['qr_tips']['x'], $config['qr_tips']['y'], 0, '微信扫码查看更多好看壁纸!');
// 描述
$word = setWord($font, $config['desc']);
$desc_text = '精选高清壁纸,已将设置,让你的手机瞬间焕然一新。';
$desc_text = wordAutoWrap($config['desc']['fontsize'], $font, $desc_text, 230);// 设置指定宽度,超出就换行显示
$imagick_obj->annotateImage($word, $config['desc']['x'], $config['desc']['y'], 0, $desc_text);
// 显示图片
header("Content-Type: image/{$imagick_obj->getImageFormat()}");
echo $imagick_obj->getImageBlob();
// 输出图片
$imagick_obj->writeImage('./output.jpg');
// 清理资源
$imagick_obj->clear();
$imagick_obj->destroy();
/**
* 设置图片
* @param [type] $url
* @param [type] $config
* @return void
*/
function setImages($url, $config){
extract($config);
$image = new Imagick($url);
$image->thumbnailImage($width, $height);
if($radius != 0){
// 创建遮罩图像
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('transparent'), 'png');
// 创建圆角矩形
$shape = new ImagickDraw();
$shape->setFillColor(new ImagickPixel('black'));
$shape->roundRectangle(0, 0, $width, $height, $radius, $radius);
// 绘制矩形
$mask->drawImage($shape);
// 合并到目标图片
$image->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0);
}
return $image;
}
/**
* 设置文字
* @param [type] $font
* @param [type] $config
* @return void
*/
function setWord($font, $config){
$draw = new ImagickDraw();
$draw->setFontSize($config['fontsize']);
$draw->setFont($font);
$draw->setFontWeight($config['fontweight']);
$draw->setFillColor($config['color']);
return $draw;
}
/**
* 根据预设宽度让文字自动换行
* @param $fontsize 字体大小
* @param $ttfpath 字体名称
* @param $str 字符串
* @param $width 预设宽度
* @param $fontangle 角度
* @param $charset 编码
* @return $_string 字符串
*/
function wordAutoWrap($fontsize, $ttfpath, $str, $width, $fontangle = 0, $charset = 'utf-8'){
$_string = "";
$_width = 0;
$temp = chararray($str);
foreach ($temp[0] as $v){
$w = charWidth($fontsize, $fontangle, $ttfpath,$v);
$_width += intval($w);
if (($_width > $width) && ($v !== "")){
$_string .= PHP_EOL;
$_width = 0;
}
$_string .= $v;
}
return $_string;
}
/**
* 返回一个字符串在图片中所占的宽度
* @param $fontsize 字体大小
* @param $fontangle 角度
* @param $ttfpath 字体文件
* @param $char 字符
* @return $width
*/
function charWidth($fontsize, $fontangle, $ttfpath, $char){
$box = imagettfbbox($fontsize, $fontangle, $ttfpath, $char);
$width = max($box[2], $box[4]) - min($box[0], $box[6]);
return $width;
}
/**
* 返回一个字符的数组
*
* @param $str 文字
* @param $charset 字符编码
* @return $match 返回一个字符的数组
*/
function charArray($str, $charset="utf-8"){
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
return $match;
}
最终输出效果图: