PHP代码
<?php
/*
if(isset($_SERVER['HTTP_REFERER'])){
if(strpos($_SERVER['HTTP_REFERER'],"http://localhost")==0){
pic();
}
else{
echo "<title>404</title>";
}
}else if(find_spider()){
pic();
}else{
echo "<title>404</title>";
}
*/
pic();
function pic(){
$kw=$_GET["k"];
$kw = htmlentities($kw);
$kw = strip_tags($kw);
$dst_nr = mb_substr($kw,0,30,'utf-8');//文字内容(截取汉字个数,这里修改)
$img_array = glob("./image/*.{jpg,png}",GLOB_BRACE);
$str_length=mb_strlen($dst_nr,'utf-8');
if($str_length>=30){
$str_length=$str_length%10;
}
$dst_path = $img_array[$str_length];
$font = './Alibaba-PuHuiTi-Bold.ttf';
$fontsize = 20;
$dst = imagecreatefromstring(file_get_contents($dst_path));
$color= imagecolorallocatealpha($dst,255,255,0,0);
$imgWidth = imagesx($dst);
$imgHeight = imagesy($dst);
$fontWidth = imagefontwidth($fontsize);
$fontHeight = imagefontheight($fontsize);
$x = xy($imgWidth,$imgHeight,$fontsize,$dst_nr,2);
$y = ($imgHeight-$fontHeight)/1.8;
if(imagefontwidth($fontsize)*strlen($dst_nr)>$imgWidth-50){
$fontsize =18;
$font_num = mb_strlen($dst_nr,'utf-8');
$font_num=$font_num/2;
$str1 = mb_substr($dst_nr,0,$font_num,'utf-8');
$str2 = mb_substr($dst_nr,$font_num,15,'utf-8');
$x1 = xy($imgWidth,$imgHeight,$fontsize,$str1,2)+25;
$x2 = xy($imgWidth,$imgHeight,$fontsize,$str2,2)+25;
$y1 = $y-10;
$y2 = $y1+30;
imagefttext($dst, $fontsize, 0, $x1, $y1, $color, $font, $str1);
imagefttext($dst, $fontsize, 0, $x2, $y2, $color, $font, $str2);
}else{
imagefttext($dst, $fontsize, 0, $x, $y, $color, $font, $dst_nr);
}
header("Content-Type:image/jpg;charset=utf-8");
imagejpeg($dst);
imagedestroy($dst);
}
function find_spider(){
$useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : 'bot123');
$spider_arr=array(
'bot',
'spider',
'slurp',
'ia_archiver',
);
foreach($spider_arr as $spider){
$spider = strtolower($spider);
if(strstr($useragent,$spider)){
return true;
}
}
return false;
}
function xy($imgWidth,$imgHeight,$fontsize,$k,$pianyi){
$fontWidth = imagefontwidth($fontsize);
//$x = ($imgWidth-$fontWidth*mb_strlen($k/30))/$pianyi;
$x = ($imgWidth-$fontWidth*strlen($k))/$pianyi;
return $x;
}
?>
伪静态配资
rewrite ^/pic\/(.*?)\.jpg$ /pic/pic.php?k=$1;
使用
<img src=”http://你的域名/pic/*.jpg”>
发表评论
侧栏公告
寄语
譬如朝露博客是一个分享前端知识的网站,联系方式11523518。
热评文章
标签列表
热门文章
友情链接