Sat 4 Jun 2005 09:50:41
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 ของระบบมากยิ่งถ้าทำหลายรูปอาจจะนิ่งไปเลยได้ครับ

















