以上为演示。
写完橙语君、朱鹭子和阿求三个单词机器人后,就想做一个页面把这些Bot们都列出来(这个页面现在在这里),以便好奇的人点了Source Name后能同时看到另外两个机器人,多点关注度,可是饭否本身的签名图插件因为年代久远,效果实在不敢恭维:

而后想起以前用过的、WandSea编写的TwSig(推特签名图),决定趁这这个机会学习一下相关函数的使用方法,成果就是开头的那个样子。
继续阅读可以看到完整代码以及下载,初学PHP之故,代码只是能用就OK了,而且还借用了很多别人的方法,肯定有很多吐槽的地方,还请多包涵。
代码下载(不含调用的字体):Fanfou Forum Signature Plugin
主要知识点:file_get_contents、json_decode、imagecreatetruecolor、imagecopyresampled、imagecolorallocate、imagettftext、imagecopy、imagepng
false){
$r_json = json_decode($r_json, true);
if(!$r_json['error']){
$font = 'wqy.ttc';
$img = imagecreatetruecolor(400,150);
$img_back = imagecreatefrompng('sigback.png');
imagecopyresampled($img,$img_back,0,0,0,0,400,150,400,150);
$color_white = imagecolorallocate($img,255,255,255);
$color_skyblue = imagecolorallocate($img,0,120,255);
$txt_id = $r_json['name'];
$txt_fo = $r_json['friends_count'];
$txt_foer = $r_json['followers_count'];
$txt_msgc = $r_json['statuses_count'];
$txt_recentmsg = $r_json['status']['text'];
if(!$txt_recentmsg || $txt_recentmsg == '') $txt_recentmsg = '我只向关注我的人公开我的消息。';
if(strlen($txt_recentmsg) > 315) $txt_recentmsg = cut_str($txt_recentmsg,105);
$txt_avatar = $r_json['profile_image_url_large'];
if(!$txt_avatar || $txt_avatar == '') $txt_avatar = 'avatar00.jpg';
$pic_avatar = imagecreatefromjpeg($txt_avatar);
imagetextouter($img,10,132,33,'#EEFFFF',$font,$txt_id,'#66CCFF');
imagettftext($img,9,0,133,53,$color_white,$font,'关注/'.$txt_fo.' '.'关注者/'.$txt_foer.' '.'消息数/'.$txt_msgc);
imagettftext($img,9,0,131,51,$color_skyblue,$font,'关注/'.$txt_fo.' '.'关注者/'.$txt_foer.' '.'消息数/'.$txt_msgc);
imagettftext($img,9,0,132,71,$color_white,$font,autowrap(9,0,$font,$txt_recentmsg,260));
imagettftext($img,9,0,131,70,$color_skyblue,$font,autowrap(9,0,$font,$txt_recentmsg,260));
imagecopy($img,$pic_avatar,20,27,0,0,96,96);
}else{
$img = imagecreatefrompng('sig500.png');
}
}else{
$img = imagecreatefrompng('sig500.png');
}
}else{
$img = imagecreatefrompng('sig404.png');
}
imagepng($img);
imagedestroy($img);
imagedestroy($img_back);
}else{
$img = imagecreatefrompng('sig502.png');
imagepng($img);
imagedestroy($img);
}
function autowrap($fontsize, $angle, $fontface, $string, $width) {
// Autowrap function by NetPuter
// From: http://orzdream.com/2010/08/auto-wrap-in-php-gd/
// 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
$content = "";
// 将字符串拆分成一个个单字 保存到数组 letter 中
for ($i=0;$i $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
return $content;
}
function imagetextouter(&$im, $size, $x, $y, $color, $fontfile, $text, $outer)
// Image Outerliner
// From http://www.ugia.cn/?p=88
{
if (!function_exists('ImageColorAllocateHEX'))
{
function ImageColorAllocateHEX($im, $s)
{
if($s{0} == "#") $s = substr($s,1);
$bg_dec = hexdec($s);
return imagecolorallocate($im,
($bg_dec & 0xFF0000) >> 16,
($bg_dec & 0x00FF00) >> 8,
($bg_dec & 0x0000FF)
);
}
}
$ttf = false;
if (is_file($fontfile))
{
$ttf = true;
$area = imagettfbbox($size, $angle, $fontfile, $text);
$width = $area[2] - $area[0] + 2;
$height = $area[1] - $area[5] + 2;
}
else
{
$width = strlen($text) * 10;
$height = 16;
}
$im_tmp = imagecreate($width, $height);
$white = imagecolorallocate($im_tmp, 255, 255, 255);
$black = imagecolorallocate($im_tmp, 0, 0, 0);
$color = ImageColorAllocateHEX($im, $color);
$outer = ImageColorAllocateHEX($im, $outer);
if ($ttf)
{
imagettftext($im_tmp, $size, 0, 0, $height - 2, $black, $fontfile, $text);
imagettftext($im, $size, 0, $x, $y, $color, $fontfile, $text);
$y = $y - $height + 2;
}
else
{
imagestring($im_tmp, $size, 0, 0, $text, $black);
imagestring($im, $size, $x, $y, $text, $color);
}
for ($i = 0; $i < $width; $i ++)
{
for ($j = 0; $j < $height; $j ++)
{
$c = ImageColorAt($im_tmp, $i, $j);
if ($c !== $white)
{
ImageColorAt ($im_tmp, $i, $j - 1) != $white || imagesetpixel($im, $x + $i, $y + $j - 1, $outer);
ImageColorAt ($im_tmp, $i, $j + 1) != $white || imagesetpixel($im, $x + $i, $y + $j + 1, $outer);
ImageColorAt ($im_tmp, $i - 1, $j) != $white || imagesetpixel($im, $x + $i - 1, $y + $j, $outer);
ImageColorAt ($im_tmp, $i + 1, $j) != $white || imagesetpixel($im, $x + $i + 1, $y + $j, $outer);
ImageColorAt ($im_tmp, $i - 1, $j - 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j - 1, $outer);
ImageColorAt ($im_tmp, $i + 1, $j - 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j - 1, $outer);
ImageColorAt ($im_tmp, $i - 1, $j + 1) != $white || imagesetpixel($im, $x + $i - 1, $y + $j + 1, $outer);
ImageColorAt ($im_tmp, $i + 1, $j + 1) != $white || imagesetpixel($im, $x + $i + 1, $y + $j + 1, $outer);
}
}
}
imagedestroy($im_tmp);
}
function cut_str($sourcestr,$cutlength){
//Double-byted chars substr function
//From: http://hi.baidu.com/ayu1106/blog/item/b62d73ede57521d7b31cb1c0.html
$returnstr='';
$i=0;
$n=0;
$str_length=strlen($sourcestr);//字符串的字节数
while (($n<$cutlength) and ($i<=$str_length)){
$temp_str=substr($sourcestr,$i,1);
$ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
if ($ascnum>=224){ //如果ASCII位高与224,
$returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符
$i=$i+3; //实际Byte计为3
$n++; //字串长度计1
}elseif ($ascnum>=192){ //如果ASCII位高与192,
$returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
$i=$i+2; //实际Byte计为2
$n++; //字串长度计1
}elseif ($ascnum>=65 && $ascnum<=90){ //如果是大写字母,
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1; //实际的Byte数仍计1个
$n++; //但考虑整体美观,大写字母计成一个高位字符
}else{ //其他情况下,包括小写字母和半角标点符号,
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1; //实际的Byte数计1个
$n=$n+0.5; //小写字母和半角标点等与半个高位字符宽...
}
}
if ($str_length>$cutlength){
$returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
}
return $returnstr;
}
?>
注:请不要在评论内容的任何位置出现链接,否则您的评论将被自动移入回收站,且永远不会被复审。
Please do not put any link anywhere in your comment, or it will be automatically deleted and never be reviewed.