


พอดีมีคนถามมานะครับ ว่า ... จะทำ Random image ที่หัวเวป PHPBB 3 ได้อย่างไร คือพอเข้าเวปบอร์ดที ก็มีการเปลี่ยนรูปที่หัวเวปทีนึง แบบรูปด้านบนของเวปบอร์ด Thai3d นี้แหละครับ มันจะเปลี่ยนสลับไปเรื่อยๆ เป็นการให้ความบันเทิงกับผู้เข้าชมเวป หรือไว้ในกรณีที่มีรูปอยากจะโชว์แยะ ไม่อยากให้เห็นเพียงรูปเดียว อะไรงั้น
ขั้นตอนมีดังนี้คือ
1. copy code php ด้านล่างนี้แล้ว save เป็นชื่อ random.php (เครดิต code นี้ จากเวป http://www.hotscripts.com นะครับ)
Code: Select all
<?php
$folder = '.';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}
if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
file_exists( $folder.$imageInfo['basename'] )
) {
$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}
if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}
?>
3. นำรูปที่เราต้องการ random ไปใส่ไว้ใน dir random ที่ว่านี้ด้วย จะใส่กี่รูปก็ตามสะดวก เดี๋ยวตอนมัน random มันก็จะจัดการดึงไปเอง
4. ไปที่ dir ของ template ที่เราใช้งานอยู่ ณ ตอนนี้ เช่นของผมใช้ template ชื่อว่า black แล้ว dir ของ phpbb คือ wb ...ผมก็จะเข้าไปที่ wb/styles/black/template แล้วหาไฟล์ที่ชื่อว่า overall_header.html จากนั้นก็โหลดไฟล์นี้มาลงยังเครื่องของเราเพื่อเข้าไปแก้บางอย่าง
5. เปิดไฟล์ overall_header.html แล้วแก้ตามภาพด้านล่างนี้ครับ เพื่อให้มันมองไปยังไฟล์ random.php แทนที่จะมองไปยังภาพหัวเวปอันเดิม (ภาพบนคือก่อนแก้ ภาพล่างคือแก้แล้ว)
6. ที่ FTP ...ให้เราแก้ overall_header.html เป็น overall_header.html-old หรือชื่ออะไรก็ได้ เป็นการสำรองไว้ครับ เผื่อจะกลับมาใช้ไฟล์เดิม จากนั้นก็เอา overall_header.html ที่เราแก้แล้วใส่เข้าไปเพื่อให้บอร์ดทำงานด้วยระบบ random ...ตอนนี้บอร์ดอาจจะยังไม่ดำเนินการใหม่ให้เรานะครับ อาจจะต้องไป Purge the cache ที่หน้า admin ด้วย
7. สุดท้ายแล้วครับ ไปยังหน้า admin แล้วกดปุ่ม Purge the cache เพื่อให้บอร์ดมันดำเนินการกับอะไรที่เราแก้ไขไป จากนั้นก็กลับมาหน้าแรกของบอร์ด ลอง refresh ไปมาสองสามหน ก็จะพบว่าเกิดการเปลี่ยนภาพที่หัวบอร์ดในทุกๆ ครั้งที่เรา refresh


