ย่อรูปเป็น Thumbnail สำหรับแสดงผล

จะว่าไป การย่อรูปThumbnail นี้ก็ไม่ค่อยเวิร์คเท่าไหร่เพราะมันจะมาประมวลจับรูปขนาดใหญ่มาย่อเป็น Thumbnail เอาอีตอนแสดงผลนี่แหล่ะ แต่อาจจำเป็นบางกรณี เอามาแป่ะไว้ก่อนแล้วกัน
Code

<?header("Content-type : image/png");

header("Content-type : image/jpeg");

header("Content-type : image/gif");

$filedir= $_GET['filedir'];// พาธรูปที่นี่

$img_orig=ImageCreateFromJPEG($filedir);

$width_orig = ImagesX($img_orig);

$height_orig = ImagesY($img_orig);

$height_thm = 80;

$width_thm=round($height_thm*$width_orig/$height_orig);

$img_thm = imagecreateTrueColor($width_thm,$height_thm) ;

ImageCopyResized($img_thm,$img_orig,0,0,0,0,$width_thm+1,$height_thm+1,$width_orig,$height_orig);

imagejpeg($img_thm,"",75);

imagejpeg($img_thm);

imagedestroy($img_thm);

imagedestroy($img_orig);

?>

ก๊อปโค้ดด้านบน save เป็นชื่ออะไรก็ได้ สมมติให้ชื่อ “thumbnail.inc.php” เวลาเรียกใช้ก็โดยส่งค่าพาธไฟล์เพื่อ Resize ไปกับตัวแปล ‘filedir’ วิธีเรียกก็เรียกดังนี้นะครับ

<img src="http://tatump.10thingsabout.com/blog/thumbnail.inc.php?filedir=ชื่อพาธ">

ปล. PHP ต้องมี Extension GD Lib ด้วยนะจ๊ะ

ปล.2 ถ้าจะเปลี่ยนขนาด Thumbnail ให้เปลี่ยนที่ $height_thm = 80; เด้อ (สูตรนี้ใช้ความสูงเป็นหลักในการคำนวณความกว้างอัตโนมัติ)

You can leave a response, or trackback from your own site. Responses are currently closed, but you can trackback from your own site. You can skip to the end and leave a response. Pinging is currently not allowed. Both comments and pings are currently closed.
Powered by WordPress | Shop Free T-Mobile Phones Online | Thanks to Verizon Phones without Contract, MMORPG Games and Sell Car