phpinfo函數中有趣的PHP Logo和Zend Logo圖片(原創)
餘超 yuchao86@gmail.com
開啟PHP的原始碼,當你試圖找到logo.gif時,卻找不到此檔案,那phpinfo.php輸出的PHPlogo是怎麼生存的呢??
查看phpinfo.php檔案中對應的原始碼也只能看到這樣一個連結phpinfo.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42,究竟是怎麼產生的呢?
下面我們來看./main/logos.h 檔案中,用zend_logo與php_logo數組儲存了PHP標誌和Zend標誌。所以你根本在發行包裡找不到zend.gif檔案,
如下所示:
#define CONTEXT_TYPE_IMAGE_GIF "Content-Type: image/gif"
static const unsigned char zend_logo[] = {
71, 73, 70, 56, 57, 97, 113, 0, 72, 0,
213, 0, 0, 13, 13, 14, 1, 3, 6, 2,
//省略中間部分
46, 143, 167, 96, 131, 23, 221, 120, 200, 72,
214, 74, 16, 0, 0, 59 };
static const unsigned char php_logo[] = {
71, 73, 70, 56, 57, 97, 120, 0, 67, 0,
230, 106, 0, 127, 130, 184, 57, 55, 71, 40,
//省略中間部分
21, 116, 187, 251, 221, 240, 142, 119, 188, 3,
1, 0, 59, 0 };
static const unsigned char php_egg_logo[] = {
71, 73, 70, 56, 57, 97, 120, 0, 67, 0,
231, 255, 0, 18, 25, 33, 32, 30, 34, 28,
//省略中間部分,這個是愚人節發送的彩蛋logo
54, 240, 3, 66, 148, 6, 228, 178, 11, 192,
192, 4, 236, 158, 239, 2, 17, 16, 0, 59};
現在可能你又有疑問了??這些數字是怎麼產生圖片的呢?
請看下面一個程式:
<?php
$filename = "logo-small.gif";
$fp = fopen($filename, "rb");
$buffer = fread ($fp, filesize ($filename));
fclose($fp);
$len = strlen($buffer);
$fp = fopen("logo-small.h", "wb");
fwrite($fp, "unsigned char php_logo[] = {");
for ($i=0; $i<=$len; $i++) {
if($i % 10 == 0) {
fwrite($fp, "\n\t\t");
}
if($i == $len) {
$str = str_pad(ord(substr($buffer, $i, 1)), 3, " ", STR_PAD_LEFT);
} else {
$str = str_pad(ord(substr($buffer, $i, 1)), 3, " ", STR_PAD_LEFT) . ", ";
}
fwrite($fp, $str);
}
fwrite($fp, " };\n");
fclose($fp);
?>
這個是上一個過程的逆過程,就是把一張gif圖片轉換成一個C語言的數組並存放到.h標頭檔中,
轉換後同樣可以得到一個相應的數組如下:
unsigned char php_logo[] = {
71, 73, 70, 56, 55, 97, 51, 0, 51, 0,
244, 0, 0, 251, 121, 34, 251, 129, 48, 252,
138, 62, 252, 146, 76, 252, 155, 89, 252, 163,
103, 253, 171, 117, 253, 180, 131, 253, 188, 145,
253, 197, 159, 254, 205, 173, 254, 213, 187, 254,
222, 200, 254, 230, 214, 255, 239, 228, 255, 247,
242, 255, 255, 255, 0, 0, 0, 0, 0, 0,
...................... };
用這個數組替換掉./main/logos.h中相應的數組內容就可以更換logo了,
ord函數是轉換為對應的ASCII碼,而str_pad函數則是使用數組對齊,STR_PAD_LEFT參數是靠左對齊
最後重新編譯一下PHP原始碼即可修改phpinfo中的LOGO及ZENDLOGO了。
另外你可以使用php_logo_guid()和zend_logo_guid()函數如下語句產生PHPlogo和ZENDlogo
echo '<img src="' . $_SERVER['PHP_SELF'] .
'?=' . php_logo_guid() . '" alt="PHP Logo !" />';
通過我執行發現,這個guid值始終是一個,在編譯的時候就確定的了。
參考:
http://www.webhostingtalk.com/showthread.php?t=254983
http://bytes.com/topic/php/answers/561086-phpinfo-how-does-generates-php-logo
http://hk2.php.net/php_logo_guid
http://hk2.php.net/zend_logo_guid