พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com
PHP development / การ resize ภาพ
yui
yui
Sat 4 Jun 2005 09:50:41
การ resize ภาพด้วยภาษา php ทำยังงัยเหรอค่ะ
Administrator
Administrator
Sat 4 Jun 2005 11:49:23

ต้องใช้ GD library ครับ ตัวอย่างผมเขียนให้ resize ได้ 3 ชนิด GIF,JPEG,PNG โดยการ resize แบบ aspect ratio ครับ โดยการ save เป็นชื่อตามวันเวลาครับ

$source_name = "image/sample.gif";
$dest_name = date("YmdHis");

$max_width = 800;
$max_height = 600;

//Get image info
list($width, $height, $type, $att) = getimagesize($source_name);

//Resize image aspect ratio
$dest_width = 0;
$dest_height = 0;
if($width > $max_width){
$dest_width = $max_width;
$dest_height = ($dest_width/$width)*$height;
}elseif($height > $max_height){
$dest_height = $max_height;
$dest_width = ($dest_height/$height)*$width;
}

//Create new image for each type
$resource = "";
if($type == 1){
//GIF image
$resource = imagecreatefromgif($source);
}elseif($type == 2){
//JPEG image
$resource = imagecreatefromjpeg($source);
}elseif($type == 3){
//PNG image
$resource = imagecreatefrompng($source);
}

//Save image for each type
if($type == 1){
//GIF image
imagegif($resource,$dest_name.".gif");
}elseif($type == 2){
//JPEG image
imagejpeg($resource,$dest_name.".jpg", 100);
}elseif($type == 3){
//PNG image
imagepng($resource, $dest_name.".png");
}

หรืออาจจะใช้ imagecreateresampled() ก็ได้ครับ ง่ายแต่ไม่ recommended ครับเพราะกิน resource ของระบบมากยิ่งถ้าทำหลายรูปอาจจะนิ่งไปเลยได้ครับ

Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login