PHP根据文章标题生成缩略图,并居中展示

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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是宽宽呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值