1、实现效果如下图
2、使用方法
<img src="/titleimg/引入你的标题或者文字内容.jpg">
3、添加伪静态规则
rewrite ^/titleimg/(.+)\.jpg$ /autoimg/img.php?txt=$1;
4、下载代码包,解压后放入项目根目录中
下载地址:https://download.csdn.net/download/YanWenGuang123/87959335
具体实现方法
<?php
function attach_text_logo($imgFilePath, $textLogo) {
$hh = 16;
if(mb_strlen($textLogo,'utf-8')>$hh){
$newtextLogo = mb_substr($textLogo,0,$hh,'utf-8') . "\n" . mb_substr($textLogo,$hh,$hh,'utf-8');
}else{
$newtextLogo = $textLogo;
}
//创建图片的实例
$im = imagecreatefromstring(file_get_contents($imgFilePath));
list($width, $height, $type, $attr) = getimagesize($imgFilePath);
// 水印字体颜色
$textColor = imagecolorallocate($im, 0x00, 0x00, 0x00);
// 水印字体大小
$textSize = 18;
//字体文件
$fontFile = './msyhbd.ttf';
$fontBox = imagettfbbox($textSize, 0, $fontFile, $newtextLogo);//获取文字所需的尺寸大小
// 居中算法
$fwidth = ceil(($width - $fontBox[2]) / 2);
$fheight = ceil(($height - $fontBox[1] - $fontBox[7]) / 2);
// 添加文字水印
imagefttext($im, $textSize, 0, $fwidth, $fheight, $textColor, $fontFile, $newtextLogo);
// imagefttext($im, $textSize, 0, 30, 110, $textColor, $fontFile, $newtextLogo);
switch($type) {
case 1:
imagegif($im);
break;
case 2:
imagejpeg($im);
break;
case 3:
imagepng($im);
break;
case 6:
imagewbmp($im);
break;
}
imagedestroy($im);
}
$txt = $_GET['txt'];
//$txt = iconv('gbk','utf-8',$txt);
if($txt){
header('Content-Type: image/jpg');
$bgimg = './bgimg/back.jpg';
attach_text_logo($bgimg, $txt);
}