QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

PHPCMS

phpcms自动生成标题图片

 shitouadmin  2023-06-26 16:45:23
利用文章标题生成一个纯色背景的图片,效果如图:



看着是不是很整齐,舒服,实现方法:

1、在phpcms/libs/functions/global.func.php中添加添加函数:
  1. function thumbTitle($id,$catid,$width=400,$height=300) { 
  2.     global $image; 
  3.     $upload_url = pc_base::load_config('system','upload_url'); 
  4.     $upload_path = pc_base::load_config('system','upload_path'); 
  5.     $path = $upload_url.'auto/'.$catid.'/'.$id.'.png'
  6.     $filename =  $upload_path.'auto/'.$catid.'/'.$id.'.png'; //图片会自动生成到uploadfile/auto/下
  7.     $dir = $upload_path.'auto/'.$catid; 
  8.     if(!file_exists($filename)){ 
  9.         mkdirs($dir, 0777, true); 
  10.     }else
  11.         return $path; 
  12.     } 
  13.      
  14.     $db = pc_base::load_model('content_model'); 
  15.     $siteid = get_siteid(); 
  16.     $CATEGORYS = getcache('category_content_'.$siteid,'commons'); 
  17.     $modelid = $CATEGORYS[$catid]['modelid']; 
  18.     $db->set_model($modelid); 
  19.     $r = $db->get_one(array('id'=>$id)); 
  20.     if(!$r) return ""
  21.  
  22.     $font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'Alibaba-PuHuiTi-Bold.ttf'
  23.     $font2 = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'alibabamedium.ttf'
  24.     $fontsize = 20; 
  25.     $title = mb_substr(new_html_special_chars($r['title']),0,120); 
  26.     //创建图片 
  27.     if(function_exists('imagecreatetruecolor')){ 
  28.         $thumbimg = imagecreatetruecolor($width, $height);  
  29.     }else
  30.         $thumbimg = imagecreate($width, $height);  
  31.     } 
  32.     imageantialias($thumbimg, true);//开启抗锯齿 
  33.     $rgb = randRGB(); 
  34.     $c = Imagecolorallocatealpha($thumbimg, $rgb[0] , $rgb[1] , $rgb[2] , 0);//拾取一个完全透明的颜色 
  35.     $textColor = Imagecolorallocatealpha($thumbimg, 255 , 255 , 255 , 0);//字体颜色 
  36.     // Imagealphablending($thumbimg, false);//关闭混合模式,以便透明颜色能覆盖原画布(开启会造成字体锯齿) 
  37.     ImageFill($thumbimg, 0 , 0 , $c);//填充 
  38.     Imagesavealpha($thumbimg, true); 
  39.     $fontWidth = Imagefontwidth($fontsize); 
  40.     $fontHeight = Imagefontheight($fontsize); 
  41.     $imgHeight = imagesy($thumbimg);//生成图片高 
  42.     $imgWidth = imagesx($thumbimg);//生成图片宽 
  43.     $x = ($width-$imgWidth+20); 
  44.     $y = ($height-$fontHeight)/2 -10; 
  45.     $widthText = $width - 60; 
  46.     $title = wrapText($fontsize, 0, $font, $title, $widthText,2);//处理换行 
  47.     $fontBox = imagettfbbox($fontsize, 0, $font, $title);//获取文字所需的尺寸大小  
  48.     //Imagefttext($thumbimg, $fontsize, 0, $x, $y,$textColor, $font, $title);//生成文字(第一个参数为字体大小,0是倾斜度) 
  49.     //4.写入文字 (图片资源,字体大小,旋转角度,坐标x,坐标y,颜色,字体文件,内容) 
  50.     Imagettftext($thumbimg, $fontsize, 0, ceil(($width - $fontBox[2]) / 2), ceil(($height - $fontBox[1] - $fontBox[7]) / 2), $textColor, $font, $title); 
  51.  
  52.     $author = '--腾石科技'
  53.     $fontsize2 = 12; 
  54.     $textColor2 = Imagecolorallocatealpha($thumbimg, 255 , 255 , 255 , 60);//字体颜色 
  55.     $fontWidth2 = Imagefontwidth($fontsize); 
  56.     $x2 = ($width-$fontWidth2*strlen($author)+ 10); 
  57.     $y2 = $y + 90; 
  58.  
  59.     Imagefttext($thumbimg, $fontsize2, 0, $x2, $y2,$textColor2, $font2, $author);//生成文字(第一个参数为字体大小,0是倾斜度) 
  60.     imagepng($thumbimg,$filename); 
  61.     Imagedestroy($thumbimg); 
  62.     return $path; 
  63.  
  64. function randRGB(){ 
  65.     // $r = rand(0,255); 
  66.     // $g = rand(0,255); 
  67.     // $b = rand(0,255); 
  68.     // return array($r,$g,$b); 
  69.     $colorList = array( 
  70.         array(255, 0, 0), 
  71.         array(255, 153, 0), 
  72.         array(255,86,33), 
  73.         array(0,150,138), 
  74.         array(46,145,238), 
  75.         array(255,122,0), 
  76.         array(33,189,150), 
  77.         array(108,188,251), 
  78.         array(50,158,254), 
  79.         array(243,17,60), 
  80.         array(255,144,105), 
  81.         array(255,200,64), 
  82.         array(255,132,21), 
  83.         array(203,153,238), 
  84.         array(160,155,225), 
  85.         array(113,215,202), 
  86.         array(255,103,103), 
  87.         array(108,48,255), 
  88.         array(220,188,53), 
  89.         array(0,185,141), 
  90.         array(33,82,176), 
  91.         array(0,157,158), 
  92.         array(43,150,60), 
  93.         array(95,184,120), 
  94.         array(47,64,86), 
  95.         array(255,87,34), 
  96.     ); 
  97.     $num = rand(0,count($colorList) -1); 
  98.     return $colorList[$num]; 
  99.  
  100. //换行 
  101. function wrapText($fontsize, $angle, $fontface, $string, $width, $max_line = null){ 
  102.     // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度 
  103.     $content = ""
  104.     // 将字符串拆分成一个个单字 保存到数组 letter 中 
  105.     $letter = []; 
  106.     for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) { 
  107.         $letter[] = mb_substr($string, $i, 1, 'UTF-8'); 
  108.     } 
  109.     $line_count = 0; 
  110.     foreach ($letter as $l) { 
  111.         $testbox = imagettfbbox($fontsize, $angle, $fontface, $content . ' ' . $l); 
  112.         // 判断拼接后的字符串是否超过预设的宽度 
  113.         if (($testbox[2] > $width) && ($content !== "")) { 
  114.             $line_count++; 
  115.             if ($max_line && $line_count >= $max_line) { 
  116.                 $content = mb_substr($content, 0, -1, 'UTF-8') . "..."
  117.                 break
  118.             } 
  119.             $content .= "\n"
  120.         } 
  121.         $content .= $l; 
  122.     } 
  123.     return $content; 
  1. function mkdirs($path){ 
  2.     if(is_dir($path)) return TRUE; 
  3.     $ftp_enable = 0; 
  4.     $path = dir_path($path); 
  5.     $temp = explode('/', $path); 
  6.     $cur_dir = ''
  7.     $max = count($temp) - 1; 
  8.     for($i=0; $i<$max; $i++) { 
  9.         $cur_dir .= $temp[$i].'/'
  10.         if (@is_dir($cur_dir)) continue
  11.         @mkdir($cur_dir, 0777,true); 
  12.         @chmod($cur_dir, 0777); 
  13.     } 
  14.     return is_dir($path); 

(2)下载阿里普惠体字体放到phpcms/libs/data/font下,代码的22行,23行 改成自己的字体名称,

(3)调用方式

  1. <img src="{if $r[thumb]}{thumb($r[thumb],400,300)}{else}{thumbTitle($r['id'],$r['catid'])}{/if}" alt="{$r[title]}"
有缩略图的时候调用缩略图,没有的时候调用自动生成的图片


注意:
(1)这种方式是生成实际图片的,防止后期访问量太大,实时生成图片卡顿,所以直接存储到本地
(2)调用之前会先去看图片是否存在,如果存在则直接读取
(3)function randRGB()表示随机获取一个指定的背景色,因为随机背景色可能生成比较刺眼或者比较浅的背景色,所以默认了这么多比较实用的几个背景色
(4)$author可以改成你的文章作者,时间,来源之类的参数


¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《phpcms自动生成标题图片》发布于石头博客文章,作者:shitouadmin,如若转载,请注明出处:https://www.pweb123.com/cms/phpcms/959.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换

添加微信好友

添加微信好友

微信小程序

百度小程序