phpcms自动生成标题图片
发布时间:2021-02-10 作者:洛阳网站建设 点击:
利用文章标题生成一个纯色背景的图片,效果如图:
看着是不是很整齐,舒服,实现方法:
1、在phpcms/libs/functions/global.func.php中添加添加函数:
- function thumbTitle($id,$catid,$width=400,$height=300) {
- global $image;
- $upload_url = pc_base::load_config('system','upload_url');
- $upload_path = pc_base::load_config('system','upload_path');
- $path = $upload_url.'auto/'.$catid.'/'.$id.'.png';
- $filename = $upload_path.'auto/'.$catid.'/'.$id.'.png'; //图片会自动生成到uploadfile/auto/下
- $dir = $upload_path.'auto/'.$catid;
- if(!file_exists($filename)){
- mkdirs($dir, 0777, true);
- }else{
- return $path;
- }
- $db = pc_base::load_model('content_model');
- $siteid = get_siteid();
- $CATEGORYS = getcache('category_content_'.$siteid,'commons');
- $modelid = $CATEGORYS[$catid]['modelid'];
- $db->set_model($modelid);
- $r = $db->get_one(array('id'=>$id));
- if(!$r) return "";
- $font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'Alibaba-PuHuiTi-Bold.ttf';
- $font2 = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'alibabamedium.ttf';
- $fontsize = 20;
- $title = mb_substr(new_html_special_chars($r['title']),0,120);
- //创建图片
- if(function_exists('imagecreatetruecolor')){
- $thumbimg = imagecreatetruecolor($width, $height);
- }else{
- $thumbimg = imagecreate($width, $height);
- }
- imageantialias($thumbimg, true);//开启抗锯齿
- $rgb = randRGB();
- $c = Imagecolorallocatealpha($thumbimg, $rgb[0] , $rgb[1] , $rgb[2] , 0);//拾取一个完全透明的颜色
- $textColor = Imagecolorallocatealpha($thumbimg, 255 , 255 , 255 , 0);//字体颜色
- // Imagealphablending($thumbimg, false);//关闭混合模式,以便透明颜色能覆盖原画布(开启会造成字体锯齿)
- ImageFill($thumbimg, 0 , 0 , $c);//填充
- Imagesavealpha($thumbimg, true);
- $fontWidth = Imagefontwidth($fontsize);
- $fontHeight = Imagefontheight($fontsize);
- $imgHeight = imagesy($thumbimg);//生成图片高
- $imgWidth = imagesx($thumbimg);//生成图片宽
- $x = ($width-$imgWidth+20);
- $y = ($height-$fontHeight)/2 -10;
- $widthText = $width - 60;
- $title = wrapText($fontsize, 0, $font, $title, $widthText,2);//处理换行
- $fontBox = imagettfbbox($fontsize, 0, $font, $title);//获取文字所需的尺寸大小
- //Imagefttext($thumbimg, $fontsize, 0, $x, $y,$textColor, $font, $title);//生成文字(第一个参数为字体大小,0是倾斜度)
- //4.写入文字 (图片资源,字体大小,旋转角度,坐标x,坐标y,颜色,字体文件,内容)
- Imagettftext($thumbimg, $fontsize, 0, ceil(($width - $fontBox[2]) / 2), ceil(($height - $fontBox[1] - $fontBox[7]) / 2), $textColor, $font, $title);
- $author = '--腾石科技';
- $fontsize2 = 12;
- $textColor2 = Imagecolorallocatealpha($thumbimg, 255 , 255 , 255 , 60);//字体颜色
- $fontWidth2 = Imagefontwidth($fontsize);
- $x2 = ($width-$fontWidth2*strlen($author)+ 10);
- $y2 = $y + 90;
- Imagefttext($thumbimg, $fontsize2, 0, $x2, $y2,$textColor2, $font2, $author);//生成文字(第一个参数为字体大小,0是倾斜度)
- imagepng($thumbimg,$filename);
- Imagedestroy($thumbimg);
- return $path;
- }
- function randRGB(){
- // $r = rand(0,255);
- // $g = rand(0,255);
- // $b = rand(0,255);
- // return array($r,$g,$b);
- $colorList = array(
- array(255, 0, 0),
- array(255, 153, 0),
- array(255,86,33),
- array(0,150,138),
- array(46,145,238),
- array(255,122,0),
- array(33,189,150),
- array(108,188,251),
- array(50,158,254),
- array(243,17,60),
- array(255,144,105),
- array(255,200,64),
- array(255,132,21),
- array(203,153,238),
- array(160,155,225),
- array(113,215,202),
- array(255,103,103),
- array(108,48,255),
- array(220,188,53),
- array(0,185,141),
- array(33,82,176),
- array(0,157,158),
- array(43,150,60),
- array(95,184,120),
- array(47,64,86),
- array(255,87,34),
- );
- $num = rand(0,count($colorList) -1);
- return $colorList[$num];
- }
- //换行
- function wrapText($fontsize, $angle, $fontface, $string, $width, $max_line = null){
- // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
- $content = "";
- // 将字符串拆分成一个个单字 保存到数组 letter 中
- $letter = [];
- for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
- $letter[] = mb_substr($string, $i, 1, 'UTF-8');
- }
- $line_count = 0;
- foreach ($letter as $l) {
- $testbox = imagettfbbox($fontsize, $angle, $fontface, $content . ' ' . $l);
- // 判断拼接后的字符串是否超过预设的宽度
- if (($testbox[2] > $width) && ($content !== "")) {
- $line_count++;
- if ($max_line && $line_count >= $max_line) {
- $content = mb_substr($content, 0, -1, 'UTF-8') . "...";
- break;
- }
- $content .= "\n";
- }
- $content .= $l;
- }
- return $content;
- }
- function mkdirs($path){
- if(is_dir($path)) return TRUE;
- $ftp_enable = 0;
- $path = dir_path($path);
- $temp = explode('/', $path);
- $cur_dir = '';
- $max = count($temp) - 1;
- for($i=0; $i<$max; $i++) {
- $cur_dir .= $temp[$i].'/';
- if (@is_dir($cur_dir)) continue;
- @mkdir($cur_dir, 0777,true);
- @chmod($cur_dir, 0777);
- }
- return is_dir($path);
- }
(2)下载阿里普惠体字体放到phpcms/libs/data/font下,代码的22行,23行 改成自己的字体名称,
(3)调用方式
有缩略图的时候调用缩略图,没有的时候调用自动生成的图片
注意:
(1)这种方式是生成实际图片的,防止后期访问量太大,实时生成图片卡顿,所以直接存储到本地
(2)调用之前会先去看图片是否存在,如果存在则直接读取
(3)function randRGB()表示随机获取一个指定的背景色,因为随机背景色可能生成比较刺眼或者比较浅的背景色,所以默认了这么多比较实用的几个背景色
(4)$author可以改成你的文章作者,时间,来源之类的参数
网站关键词:
phpcms