我们的很多信息查询网站基本上都是提供纯文字内容,没有图片,只有少数网站(例如字典)有图片、音频素材我们才能在网站上提供。
其实PHP是有绘图功能的,可以根据文字内容生成需要的图片,我们去年曾经在一个繁体字库网站尝试了PHP生成图片,是可行的,但后期一直没有推广运用,最近又把这个事情重拾起来,进行了尝试,下面是一个简单的从矢量字库获取数据生成图片的PHP片段例子ziku_ziti.php:
<?php
header("Content-type: image/png");
if (isset($_GET['width'])) {
$width = $_GET['width'];
} else {
$width = 64;
}
if (isset($_GET['height'])) {
$height = $_GET['height'];
} else {
$height = 64;
}
if (isset($_GET['font'])) {
$font = $_GET['font'];
} else {
$font = '';
}
if (isset($_GET['text'])) {
$text = $_GET['text'];
} else {
$text = '你好';
}
$count = mb_strlen($text);
if ($count > 1) $width = 64*$count;
// Create the image
$im = imagecreatetruecolor($width, $height);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $width, $height, $white);
// The text to draw
//$text = '你好';
// Replace path by your own font path
switch($font) {
case 'TwSung':
$font_path = '/ziku/Fonts/TW-Sung-98_1.ttf';
break;
case 'TWKai':
$font_path = '/ziku/Fonts/TW-Kai-98_1.ttf';
break;
default:
$font_path = '/ziku/Fonts/TW-Sung-98_1.ttf';
}
// Add some shadow to the text
//imagettftext($im, 60, 0, 11, 21, $grey, $font, $text);
// Add the text
$size = $width/$count*7.5/10;
$x = 0;
$y = $height*8/10;
imagettftext($im, $size, 0, $x, $y, $black, $font_path, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
用上面这个就可以生成图片了,访问网站网站类似是这样:
https://www.example.com/ziku_ziti.php?text=你好&font=TWKai
但这不算一个标准图片,可以用apache rewrite来实现伪静态,使这个网址看上去像这样:
https://www.example.com/ziku_ziti.php?text=你好&font=TWKai
下载可以保存为“你好.png”这样的文件名,而不是像前者保存的文件名成了“ziku_ziti.png”。
伪静态的设置办法是在https.conf或者.htaccess里面这样设置:
RewriteRule ^tw_sung/(.*)\.png$ ziku_ziti.php?text=$1&font=TWSung RewriteRule ^tw_kai/(.*)\.png$ ziku_ziti.php?text=$1&font=TWKai
就可以实现。然后在html页面中就可以放置下面的代码来让用户访问图片:
<img alt="你好 (楷體矢量字庫)" src="/tw_kai/%E4%BD%A0%E5%A5%BD.png" height="64" width="128" />

在amp或者mip版本中我们是通过模板文件对以上图片html代码进行正则表达式替换来实现需要的图片文件代码。
<amp-img alt="你好 (楷體矢量字庫)" src="/tw_kai/%E4%BD%A0%E5%A5%BD.png" height="64" width="128"></amp-img> <mip-img alt="你好 (楷體矢量字庫)" src="/tw_kai/%E4%BD%A0%E5%A5%BD.png" height="64" width="128"></mip-img>
上面只是一个最简单的文字转图片,PHP还有很多其它绘图函数,例如画线、画框、画圆、复制粘贴图片等,我们还可以用来在其它查询网站生成更复杂的图片。更多可以参考《GD 和图像处理 函数》、《PHP图像生成和处理》。
生成了图片后还可以提交给搜索引擎:
另外记录几个要点:
- Google建议amp图片最小宽度要1200px
- 搜索结果出图功能,图片要求详解:图片清晰度高,长宽比为3:2,图片大小不得低于300*200px
补充:上面这是用php程序加apache重写做的png图片路径,如果是Drupal网站,也可以在自定义模块module文件中定义路径,下面是某个网站中绘制名片图片的例如:
function example_com_menu() {
$items= array();
//搜索跳转
$items['search_redirect'] = array (
'title'=>'search redirect',
'page callback'=>'search_redirect',
'access arguments'=>array('access content'),
);
//名片PNG图
$items['mingpian'] = array (
'title'=>'mingpian',
'page callback'=>'mingpian_image',
'access arguments'=>array('access content'),
);
return $items;
}
//名片PNG图
//输入:$nid.png
//输出:png图片
function mingpian_image($input=333) {
$nid = strtok($input,'.');
$file_type = strtok('.');
header("Content-type: image/png");
header("Cache-Control: public, max-age=864001");
......
省略绘图部分
......
}
评论