我們的很多信息查詢網站基本上都是提供純文字内容,沒有圖片,隻有少數網站(例如字典)有圖片、音頻素材我們才能在網站上提供。
其實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");
......
省略繪圖部分
......
}
评论