以内容标题文字,渲染在随机的背景图片内,生成为一张全新的图片进行展示
步骤一
在根目录的api文件夹内建立一个pic.php文件,放入下面的代码:
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
if(isset($_SERVER['HTTP_REFERER'])){
if(strpos($_SERVER['HTTP_REFERER'],APP_PATH)==0){
pic();
}
else{
echo "Welcome to Picture Generation library system";
}
}
elseif(checkrobot()){
pic();
}else{
echo "Welcome to Picture Generation library system";
}
function pic(){
$db = pc_base::load_model('content_model');
$id = intval($_GET['id']);
$catid = intval($_GET['catid']);
if(!$catid || !$id) showmessage(L('参数不正确'),'blank');
$siteids = getcache('category_content','commons');
$siteid = $siteids[$catid];
$CATEGORYS = getcache('category_content_'.$siteid,'commons');
if(!isset($CATEGORYS[$catid]) || $CATEGORYS[$catid]['type']!=0){
$title = '栏目不存在';
}else{
$category = $CAT = $CATEGORYS[$catid];
$category_setting = $CAT['setting'] = string2array($category['setting']);
$siteid = $GLOBALS['siteid'] = $CAT['siteid'];
$MODEL = getcache('model','commons');
$modelid = $CAT['modelid'];
$tablename = $db->table_name = $db->db_tablepre.$MODEL[$modelid]['tablename'];
$r = $db->get_one(array('id'=>$id));
$db->table_name = $tablename.'_data';
$r2 = $db->get_one(array('id'=>$id));
$rs = $r2 ? array_merge($r,$r2) : $r;
if(!$rs){
$title = '内容不存在';
}else{
$title = $rs['title'];
}
}
$dst_path = IMG_PATH.'background/'.rand(1,20).'.jpg';//图片背景(背景图片名字格式为:.jpg,命名规则:数字.jpg,数字范围就是前面rand()函数里的数字范围)
$font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'Alibaba-PuHuiTi-Bold.ttf';
$fontsize = 20;
$dst_nr = mb_substr(new_html_special_chars($title),0,120);//文字内容(默认截取120个汉字,靠左对齐)
$dst = imagecreatefromstring(file_get_contents($dst_path));//创建图片的实例
$color= imagecolorallocatealpha($dst,0,255,0,0);//配置颜色参数,透明度(前三个数字为颜色代码,最后一个是透明度)
$imgHeight = imagesy($dst);//生成图片高
$imgWidth = imagesx($dst);//生成图片宽
$fontWidth = imagefontwidth($fontsize);
$fontHeight = imagefontheight($fontsize);
$x = ($imgWidth-$imgWidth+20);
$y = ($imgHeight-$fontHeight)/2;
$widths = ($imgWidth-20);
$dst_nr = autowrap($fontsize, 0, $font, $dst_nr, $widths);//处理换行
imagefttext($dst, $fontsize, 0, $x, $y, $color, $font, $dst_nr);//生成文字(第一个参数为字体大小,0是倾斜度)
header('Content-Type: image/png');
imagepng($dst);
imagedestroy($dst);
}
function autowrap($fontsize, $angle, $fontface, $string, $width) {
$content = "";
for ($i=0;$i<mb_strlen($string);$i++) {
$letter[] = mb_substr($string, $i, 1);
}
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
return $content;
}
//判断是不是蜘蛛
function checkrobot($useragent=''){
static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');
$useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
if(strpos($useragent, 'http') === false && dstrpos($useragent, $kw_browsers)) return false;
if(dstrpos($useragent, $kw_spiders)) return true;
return false;
}
function dstrpos($string, $arr, $returnvalue = false) {
if(empty($string)) return false;
foreach((array)$arr as $v) {
if(strpos($string, $v) !== false) {
$return = $returnvalue ? $v : true;
return $return;
}
}
return false;
}
?>
步骤二
在statics/images目录里建立一个background文件夹,里面以数字形式放入jpg背景图片,如1.jpg,2.jpg,随机为20个,可增加更多,在20行的rand里扩大第二参数的20
下载字体文件,放到/phpcms/libs/data/font文件夹内:
百度网盘地址:https://pan.baidu.com/s/1IBBY75UXGoHoGI69GnyMBg
提取码:mri9
步骤三
伪静态规则:
Apache伪静态:
RewriteRule ^pic\/([0-9]+)-([0-9]+)\.png$ api.php?op=pic&catid=$1&id=$2
Nginx伪静态:
rewrite ^(.*)/pic/([0-9]+)-([0-9]+).png$ $1/api.php?op=pic&catid=$2&id=$3 last;
使用方法
以内容页为例
<img src="/pic/{$catid}-{$id}.png">
调用方法理论上支持任意位置使用,只需要能获取到栏目id和内容id即可,比如首页,频道页,栏目页,内容
评论前必须登录!
注册