ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingอยากได้ โค้ด ลดขนาดรูปครับ
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: อยากได้ โค้ด ลดขนาดรูปครับ  (อ่าน 8565 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
thespm
Newbie
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 59



ดูรายละเอียด
« เมื่อ: 18 พฤศจิกายน 2009, 22:27:54 »

คือ ผมทำเว็บเกี่ยวกับ อัลบั้มรูป แต่มีปัญหาที่รูปมีขนาดใหญ่เกินไปครับ (หมายถึงหน้าที่รวมรูปเล็กนะครับ)

จะทำยังไงดีให้รูปมันมีขนาดเล็กลง (เล็กลงเพื่อให้มัน load เร็วและประหยัด อีกด้วย)

ต้องใช้ ภาษาอะไรครับ

เช่น หน้าแสดงภาพรวม มี 15 รูป ตอนนี้ผมให้แสดงทั้ง 15 รูปที่มีขนาดเท่าของจริงเลยครับ

โดยการกำหนดให้มันเล็กลงเท่านั้นครับ แต่ขนาดที่มันโหลดยังเท่ากับขนาดเดิมอยู่ครับ

ต้องทำไงครับ

ขอบคุณครับ
บันทึกการเข้า
Synchronize
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 83
ออฟไลน์ ออฟไลน์

กระทู้: 1,231



ดูรายละเอียด
« ตอบ #1 เมื่อ: 18 พฤศจิกายน 2009, 23:03:17 »

อันนี้ตัวที่ผมใช้อยู่นะครับ ... ไม่รู้จะเครดิตใครดี เพราะ ใช้มานานจนลืมไปแล้วว่าเอามาจากไหน  Tongue

โค๊ด:
<?PHP
$ERR["UNABLE_TO_OUTPUT"] = "Unable to output: ";
$ERR["FILE_DOESNOT_EXSIT"] = "This file does not exist: ";
$ERR["FUNCTION_DOESNOT_EXIST"] = "This function does not exist: ";
$ERR["GD2_NOT_CREATED"] = "GD2 is installed, function ImageCreateTruecolor() exists, but image is not created";
$ERR["IMG_NOT_CREATED"] = "Image is not created ImageCreate(). {GD2 suppor is OFF}";
$ERR["GD2_UNAVALABLE"] = "You specified to use GD2, but not all GD2 functions are present.";
$ERR["GD2_NOT_RESIZED"] = "GD2 is installed, function ImageCopyResampled() exists, but image is not resized";
$ERR["IMG_NOT_RESIZED"] = "Image was not resized. {GD2 suppor is OFF}";
$ERR["UNKNOWN_OUTPUT_FORMAT"] = "This image format cannot bu output: ";
$ERR["NO_IMAGE_FOR_OUTPUT"] = "Image you are trying to output does not exist. ";
$ERR["IMG_NOT_SUPPORTED"] = "Can not create image. Sorry, this image type is not supported yet.";

//this class works with image
class Resize
{
var $image_original;
var $file_original;
var $image_original_width;
var $image_original_height;
var $image_original_type_code;
var $image_original_type_abbr;
var $image_original_html_sizes;

var $image_resized;
var $file_resized;
var $image_resized_width;
var $image_resized_height;
var $image_resized_type_code;
var $image_resized_type_abbr;
var $image_resized_html_sizes;

//some settings
var $jpeg_quality;
var $use_gd2;


function Resize($file_original)
{
//constructor of the class
//it takes given file and creates image out of it
global $ERR;
$this -> clear(); // clear all.

if(file_exists($file_original))
{
$this -> file_original = $file_original;
$this -> image_original = $this -> imagecreatefromfile($file_original);
if(!$this -> image_original)
{
$this -> error($ERR["IMAGE_NOT_CREATED_FROM_FILE"]." file=$file_original");
return false;
}
}
else
{
$this -> error($ERR["FILE_DOESNOT_EXSIT"]." file=$file_original");
}
}

function clear()
{
// clear all the class member varaibles

$this -> image_original = 0;
$this -> file_original = "";
$this -> image_original_width = 0;
$this -> image_original_height = 0;
$this -> image_original_type_code = 0;
$this -> image_original_type_abbr = "";
$this -> image_original_html_sizes= "";

$this -> image_resized = 0;
$this -> file_resized = "";
$this -> image_resized_width = 0;
$this -> image_resized_height = 0;
$this -> image_resized_type_code = -1;
$this -> image_resized_type_abbr = "";
$this -> image_resized_html_sizes = "";

$this -> set_parameters();
}

// jpeg_quality is image quality for out put images
// Default = 85 (Heigh)
function set_parameters($jpeg_quality="60", $use_gd2=true)
{
$this -> jpeg_quality=$jpeg_quality;
$this -> use_gd2=$use_gd2;
}

function error($msg)
{
//error messages and debug info:
// here you can implement your own error handling
//echo("<hr color='red'><font color='red'><b>$msg</b></font><hr color='red'>");
}


function imagecreatefromfile($img_file)
{
global $ERR;
$img=0;
$img_sz = getimagesize( $img_file ); ## returns array with some properties like dimensions and type;
####### Now create original image from uploaded file. Be carefull! GIF is often not supported, as far as I remember from GD 1.6
switch( $img_sz[2] )
{
case 1:
$img = $this -> _imagecheckandcreate("ImageCreateFromGif", $img_file);
$img_type = "GIF";
break;
case 2:
$img = $this -> _imagecheckandcreate("ImageCreateFromJpeg", $img_file);
$img_type = "JPG";
break;
case 3:
$img = $this -> _imagecheckandcreate("ImageCreateFromPng", $img_file);
$img_type = "PNG";
break;
// would be nice if this function will be finally supported
case 4:
$img = $this -> _imagecheckandcreate("ImageCreateFromSwf", $img_file);
$img_type = "SWF";
break;
default:
$img = 0;
$img_type = "UNKNOWN";
$this -> error($ERR["IMG_NOT_SUPPORTED"]." $img_file");
break;
}//case

if($img)
{
$this -> image_original_width=$img_sz[0];
$this -> image_original_height=$img_sz[1];
$this -> image_original_type_code=$img_sz[2];
$this -> image_original_type_abbr=$img_type;
$this -> image_original_html_sizes=$img_sz[3];
}
else
{
$this -> clear();
}

return $img;
}


function _imagecheckandcreate($function, $img_file)
{
//inner function used from imagecreatefromfile().
//Checks if the function exists and returns
//created image or false
global $ERR;
if(function_exists($function))
{
$img = $function($img_file);
}
else
{
$img = false;
$this -> error($ERR["FUNCTION_DOESNOT_EXIST"]." ".$function);
}

return $img;

}

function resizing($desired_width, $desired_height, $mode="-")
{
/*
this is core function--it resizes created image
if any of parameters == "*" then no resizing on this parameter
>> mode = "+" then image is resized to cover the region specified by desired_width, _height
>> mode = "-" then image is resized to fit into the region specified by desired_width, _height
width-to-height ratio is all the time the same
>> mode = "0" then image will be exactly resized to $desired_width _height.
geometrical distortion can occur in this case.
say u have picture 400x300 and there is circle on the picture
now u resized in mode=0 to 800x300 -- circle shape will be distorted and will look like ellipse.
GD2 provides much better quality but is not everywhere installed
*/
global $ERR;
if($desired_width == "*" && $desired_height == "*")
{
$this -> image_resized = $this -> image_original;
Return true;
}
switch($mode)
{
case "-":
case '+':
//multipliers
if($desired_width != "*")
$mult_x = $desired_width / $this -> image_original_width;
if($desired_height != "*")
$mult_y = $desired_height / $this -> image_original_height;
$ratio = $this -> image_original_width / $this -> image_original_height;

if($desired_width == "*")
{
$new_height = $desired_height;
$new_width = $ratio * $desired_height;
}
elseif($desired_height == "*")
{
$new_height = $desired_width / $ratio;
$new_width = $desired_width;
}
else
{
if($mode=="-")
{
if( $this -> image_original_height * $mult_x < $desired_height )
{
//image must be smaller than given $desired_ region
//test which multiplier gives us best result

//$mult_x does the job
$new_width = $desired_width;
$new_height = $this -> image_original_height * $mult_x;
}
else
{
//$mult_y does the job
$new_width = $this -> image_original_width * $mult_y;
$new_height = $desired_height;
}
}
else
{
//mode == "+"
// cover the region
//image must be bigger than given $desired_ region
//test which multiplier gives us best result
if( $this -> image_original_height * $mult_x > $desired_height ){
//$mult_x does the job
$new_width = $desired_width;
$new_height = $this -> image_original_height * $mult_x;
}else{
//$mult_y does the job
$new_width = $this -> image_original_width * $mult_y;
$new_height = $desired_height;
}

}
}
break;

case '0':
//fit the region exactly.
if($desired_width == "*") $desired_width = $this -> image_original_width;
if($desired_height == "*") $desired_height = $this -> image_original_height;
$new_width = $desired_width;
$new_height = $desired_height;

break;
default:
$this -> error($ERR["UNKNOWN_RESIZE_MODE"]." $mode");
break;
}

// OK here we have $new_width _height
//create destination image checking for GD2 functions:
if( $this -> use_gd2 )
{
if( function_exists("imagecreatetruecolor")){
$this -> image_resized = imagecreatetruecolor($new_width, $new_height) or $this -> error($ERR["GD2_NOT_CREATED"]);
}
else
{
$this -> error($ERR["GD2_UNAVALABLE"]." ImageCreateTruecolor()");
}
}
else
{
$this -> image_resized = imagecreate($new_width, $new_height) or $this -> error($ERR["IMG_NOT_CREATED"]);
}

//Resize
if( $this -> use_gd2 )
{
if( function_exists("imagecopyresampled"))
{
$res = imagecopyresampled($this -> image_resized,
$this -> image_original,
0, 0, //dest coord
0, 0, //source coord
$new_width, $new_height, //dest sizes
$this -> image_original_width, $this -> image_original_height // src sizes
) or $this -> error($ERR["GD2_NOT_RESIZED"]);
}
else
{
$this -> error($ERR["GD2_UNAVALABLE"]." ImageCopyResampled()");
}
}
else
{
$res = imagecopyresized($this -> image_resized,
$this -> image_original,
0, 0, //dest coord
0, 0, //source coord
$new_width, $new_height, //dest sizes
$this -> image_original_width, $this -> image_original_height // src sizes
) or $this -> error($ERR["IMG_NOT_RESIZED"]);
}
}

function output_original($destination_file, $image_type="JPG")
{
//outputs original image
//if destination file is empty image will be output to browser
// right now $image_type can be JPG or PNG
return _output_image($destination_file, $image_type, $this -> image_original);
}

function output_resized($destination_file, $image_type="JPG")
{
//if destination file is empty image will be output to browser
// right now $image_type can be JPG or PNG
$res = $this -> _output_image($destination_file, $image_type, $this -> image_resized);
if(trim($destination_file))
{
$sz=getimagesize($destination_file);
$this -> file_resized = $destination_file;
$this -> image_resized_width = $sz[0];
$this -> image_resized_height = $sz[1];
$this -> image_resized_type_code=$sz[2];
$this -> image_resized_html_sizes=$sz[3];
//only jpeg and png are really supported, but I'd like to think of future
switch($this -> image_resized_html_sizes)
{
case 0:
$this -> image_resized_type_abbr = "GIF";
break;
case 1:
$this -> image_resized_type_abbr = "JPG";
break;
case 2:
$this -> image_resized_type_abbr = "PNG";
break;
case 3:
$this -> image_resized_type_abbr = "SWF";
break;
default:
$this -> image_resized_type_abbr = "UNKNOWN";
break;
}

}
return $res;
}

function _output_image($destination_file, $image_type, $image)
{
//if destination file is empty image will be output to browser
// right now $image_type can be JPEG or PNG
global $ERR;
$destination_file = trim($destination_file);
$res = false;
if($image)
{
switch($image_type)
{
case 'JPEG':
case 'JPG':
$res = ImageJpeg($image, $destination_file, $this -> jpeg_quality);
break;
case 'PNG':
$res = Imagepng($image, $destination_file);
break;
default:
$this -> error($ERR["UNKNOWN_OUTPUT_FORMAT"]." $image_type");
break;
}
}
else
{
$this -> error($ERR["NO_IMAGE_FOR_OUTPUT"]);
}
if(!$res)
$this -> error($ERR["UNABLE_TO_OUTPUT"]." $destination_file");
return $res;
}
}
?>

ส่วนเวลาใช้งาน ใช้แบบนี้ นะครับ

โค๊ด:
$r = new Resize('___ ไฟล์ต้นทางที่จะย่อ ___');
$r -> resizing(100, 100, '+');

$r -> output_resized('___ ไฟล์ปลายที่ ที่ถูกย่อ ___.jpg', 'JPEG')

ตรง resizing ใส่ค่า 100, 100 ไว้นี่ หมายถึง ขนาดกว้าง กับ สูง นะครับ

ส่วน + ข้างหลัง เป็น option ตามนี้ + จะหมายถึง เวลาย่อ แล้ว กว้าง หรือสูง จะไม่น้อยกว่า 100 (เช่นย่อแล้ว กว้างได้ 100 แต่สูงอาจจะมากกว่า 100)

ถ้าใส่ - จะหมายถึง กว้างกะสูง จะไม่เกิน 100 (เช่นย่อแล้ว กว้างได้ 100 แต่สูงอาจจะน้อยกว่า 100)


ประมาณนี้น่ะครับ ผมว่าใช้ง่ายๆ class นี้

 wanwan017
บันทึกการเข้า

SL_master
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 57
ออฟไลน์ ออฟไลน์

กระทู้: 1,188



ดูรายละเอียด เว็บไซต์
« ตอบ #2 เมื่อ: 18 พฤศจิกายน 2009, 23:50:09 »

อ่า พอดีเลย ขอถามหน่อยครับ
ถ้าสมมุติ รูปเรา 300*300 ขนาด 50kb

และถ้าเราแก้แค่ img src="" width="100" height="100"
อยากรู้ว่า ขนาดไฟล์ จะลดลงหรือเปล่าครับ หรือ 50kb เท่าเดิม
บันทึกการเข้า

golfer007
บุคคลทั่วไป
« ตอบ #3 เมื่อ: 19 พฤศจิกายน 2009, 00:18:55 »

อ่า พอดีเลย ขอถามหน่อยครับ
ถ้าสมมุติ รูปเรา 300*300 ขนาด 50kb

และถ้าเราแก้แค่ img src="" width="100" height="100"
อยากรู้ว่า ขนาดไฟล์ จะลดลงหรือเปล่าครับ หรือ 50kb เท่าเดิม

โหลดที่ขนาดเท่าเดิมครับ
บันทึกการเข้า
thespm
Newbie
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 59



ดูรายละเอียด
« ตอบ #4 เมื่อ: 19 พฤศจิกายน 2009, 11:59:50 »

อันนี้ตัวที่ผมใช้อยู่นะครับ ... ไม่รู้จะเครดิตใครดี เพราะ ใช้มานานจนลืมไปแล้วว่าเอามาจากไหน  Tongue

โค๊ด:
<?PHP
$ERR["UNABLE_TO_OUTPUT"] = "Unable to output: ";
$ERR["FILE_DOESNOT_EXSIT"] = "This file does not exist: ";
$ERR["FUNCTION_DOESNOT_EXIST"] = "This function does not exist: ";
$ERR["GD2_NOT_CREATED"] = "GD2 is installed, function ImageCreateTruecolor() exists, but image is not created";
$ERR["IMG_NOT_CREATED"] = "Image is not created ImageCreate(). {GD2 suppor is OFF}";
$ERR["GD2_UNAVALABLE"] = "You specified to use GD2, but not all GD2 functions are present.";
$ERR["GD2_NOT_RESIZED"] = "GD2 is installed, function ImageCopyResampled() exists, but image is not resized";
$ERR["IMG_NOT_RESIZED"] = "Image was not resized. {GD2 suppor is OFF}";
$ERR["UNKNOWN_OUTPUT_FORMAT"] = "This image format cannot bu output: ";
$ERR["NO_IMAGE_FOR_OUTPUT"] = "Image you are trying to output does not exist. ";
$ERR["IMG_NOT_SUPPORTED"] = "Can not create image. Sorry, this image type is not supported yet.";

//this class works with image
class Resize
{
var $image_original;
var $file_original;
var $image_original_width;
var $image_original_height;
var $image_original_type_code;
var $image_original_type_abbr;
var $image_original_html_sizes;

var $image_resized;
var $file_resized;
var $image_resized_width;
var $image_resized_height;
var $image_resized_type_code;
var $image_resized_type_abbr;
var $image_resized_html_sizes;

//some settings
var $jpeg_quality;
var $use_gd2;


function Resize($file_original)
{
//constructor of the class
//it takes given file and creates image out of it
global $ERR;
$this -> clear(); // clear all.

if(file_exists($file_original))
{
$this -> file_original = $file_original;
$this -> image_original = $this -> imagecreatefromfile($file_original);
if(!$this -> image_original)
{
$this -> error($ERR["IMAGE_NOT_CREATED_FROM_FILE"]." file=$file_original");
return false;
}
}
else
{
$this -> error($ERR["FILE_DOESNOT_EXSIT"]." file=$file_original");
}
}

function clear()
{
// clear all the class member varaibles

$this -> image_original = 0;
$this -> file_original = "";
$this -> image_original_width = 0;
$this -> image_original_height = 0;
$this -> image_original_type_code = 0;
$this -> image_original_type_abbr = "";
$this -> image_original_html_sizes= "";

$this -> image_resized = 0;
$this -> file_resized = "";
$this -> image_resized_width = 0;
$this -> image_resized_height = 0;
$this -> image_resized_type_code = -1;
$this -> image_resized_type_abbr = "";
$this -> image_resized_html_sizes = "";

$this -> set_parameters();
}

// jpeg_quality is image quality for out put images
// Default = 85 (Heigh)
function set_parameters($jpeg_quality="60", $use_gd2=true)
{
$this -> jpeg_quality=$jpeg_quality;
$this -> use_gd2=$use_gd2;
}

function error($msg)
{
//error messages and debug info:
// here you can implement your own error handling
//echo("<hr color='red'><font color='red'><b>$msg</b></font><hr color='red'>");
}


function imagecreatefromfile($img_file)
{
global $ERR;
$img=0;
$img_sz = getimagesize( $img_file ); ## returns array with some properties like dimensions and type;
####### Now create original image from uploaded file. Be carefull! GIF is often not supported, as far as I remember from GD 1.6
switch( $img_sz[2] )
{
case 1:
$img = $this -> _imagecheckandcreate("ImageCreateFromGif", $img_file);
$img_type = "GIF";
break;
case 2:
$img = $this -> _imagecheckandcreate("ImageCreateFromJpeg", $img_file);
$img_type = "JPG";
break;
case 3:
$img = $this -> _imagecheckandcreate("ImageCreateFromPng", $img_file);
$img_type = "PNG";
break;
// would be nice if this function will be finally supported
case 4:
$img = $this -> _imagecheckandcreate("ImageCreateFromSwf", $img_file);
$img_type = "SWF";
break;
default:
$img = 0;
$img_type = "UNKNOWN";
$this -> error($ERR["IMG_NOT_SUPPORTED"]." $img_file");
break;
}//case

if($img)
{
$this -> image_original_width=$img_sz[0];
$this -> image_original_height=$img_sz[1];
$this -> image_original_type_code=$img_sz[2];
$this -> image_original_type_abbr=$img_type;
$this -> image_original_html_sizes=$img_sz[3];
}
else
{
$this -> clear();
}

return $img;
}


function _imagecheckandcreate($function, $img_file)
{
//inner function used from imagecreatefromfile().
//Checks if the function exists and returns
//created image or false
global $ERR;
if(function_exists($function))
{
$img = $function($img_file);
}
else
{
$img = false;
$this -> error($ERR["FUNCTION_DOESNOT_EXIST"]." ".$function);
}

return $img;

}

function resizing($desired_width, $desired_height, $mode="-")
{
/*
this is core function--it resizes created image
if any of parameters == "*" then no resizing on this parameter
>> mode = "+" then image is resized to cover the region specified by desired_width, _height
>> mode = "-" then image is resized to fit into the region specified by desired_width, _height
width-to-height ratio is all the time the same
>> mode = "0" then image will be exactly resized to $desired_width _height.
geometrical distortion can occur in this case.
say u have picture 400x300 and there is circle on the picture
now u resized in mode=0 to 800x300 -- circle shape will be distorted and will look like ellipse.
GD2 provides much better quality but is not everywhere installed
*/
global $ERR;
if($desired_width == "*" && $desired_height == "*")
{
$this -> image_resized = $this -> image_original;
Return true;
}
switch($mode)
{
case "-":
case '+':
//multipliers
if($desired_width != "*")
$mult_x = $desired_width / $this -> image_original_width;
if($desired_height != "*")
$mult_y = $desired_height / $this -> image_original_height;
$ratio = $this -> image_original_width / $this -> image_original_height;

if($desired_width == "*")
{
$new_height = $desired_height;
$new_width = $ratio * $desired_height;
}
elseif($desired_height == "*")
{
$new_height = $desired_width / $ratio;
$new_width = $desired_width;
}
else
{
if($mode=="-")
{
if( $this -> image_original_height * $mult_x < $desired_height )
{
//image must be smaller than given $desired_ region
//test which multiplier gives us best result

//$mult_x does the job
$new_width = $desired_width;
$new_height = $this -> image_original_height * $mult_x;
}
else
{
//$mult_y does the job
$new_width = $this -> image_original_width * $mult_y;
$new_height = $desired_height;
}
}
else
{
//mode == "+"
// cover the region
//image must be bigger than given $desired_ region
//test which multiplier gives us best result
if( $this -> image_original_height * $mult_x > $desired_height ){
//$mult_x does the job
$new_width = $desired_width;
$new_height = $this -> image_original_height * $mult_x;
}else{
//$mult_y does the job
$new_width = $this -> image_original_width * $mult_y;
$new_height = $desired_height;
}

}
}
break;

case '0':
//fit the region exactly.
if($desired_width == "*") $desired_width = $this -> image_original_width;
if($desired_height == "*") $desired_height = $this -> image_original_height;
$new_width = $desired_width;
$new_height = $desired_height;

break;
default:
$this -> error($ERR["UNKNOWN_RESIZE_MODE"]." $mode");
break;
}

// OK here we have $new_width _height
//create destination image checking for GD2 functions:
if( $this -> use_gd2 )
{
if( function_exists("imagecreatetruecolor")){
$this -> image_resized = imagecreatetruecolor($new_width, $new_height) or $this -> error($ERR["GD2_NOT_CREATED"]);
}
else
{
$this -> error($ERR["GD2_UNAVALABLE"]." ImageCreateTruecolor()");
}
}
else
{
$this -> image_resized = imagecreate($new_width, $new_height) or $this -> error($ERR["IMG_NOT_CREATED"]);
}

//Resize
if( $this -> use_gd2 )
{
if( function_exists("imagecopyresampled"))
{
$res = imagecopyresampled($this -> image_resized,
$this -> image_original,
0, 0, //dest coord
0, 0, //source coord
$new_width, $new_height, //dest sizes
$this -> image_original_width, $this -> image_original_height // src sizes
) or $this -> error($ERR["GD2_NOT_RESIZED"]);
}
else
{
$this -> error($ERR["GD2_UNAVALABLE"]." ImageCopyResampled()");
}
}
else
{
$res = imagecopyresized($this -> image_resized,
$this -> image_original,
0, 0, //dest coord
0, 0, //source coord
$new_width, $new_height, //dest sizes
$this -> image_original_width, $this -> image_original_height // src sizes
) or $this -> error($ERR["IMG_NOT_RESIZED"]);
}
}

function output_original($destination_file, $image_type="JPG")
{
//outputs original image
//if destination file is empty image will be output to browser
// right now $image_type can be JPG or PNG
return _output_image($destination_file, $image_type, $this -> image_original);
}

function output_resized($destination_file, $image_type="JPG")
{
//if destination file is empty image will be output to browser
// right now $image_type can be JPG or PNG
$res = $this -> _output_image($destination_file, $image_type, $this -> image_resized);
if(trim($destination_file))
{
$sz=getimagesize($destination_file);
$this -> file_resized = $destination_file;
$this -> image_resized_width = $sz[0];
$this -> image_resized_height = $sz[1];
$this -> image_resized_type_code=$sz[2];
$this -> image_resized_html_sizes=$sz[3];
//only jpeg and png are really supported, but I'd like to think of future
switch($this -> image_resized_html_sizes)
{
case 0:
$this -> image_resized_type_abbr = "GIF";
break;
case 1:
$this -> image_resized_type_abbr = "JPG";
break;
case 2:
$this -> image_resized_type_abbr = "PNG";
break;
case 3:
$this -> image_resized_type_abbr = "SWF";
break;
default:
$this -> image_resized_type_abbr = "UNKNOWN";
break;
}

}
return $res;
}

function _output_image($destination_file, $image_type, $image)
{
//if destination file is empty image will be output to browser
// right now $image_type can be JPEG or PNG
global $ERR;
$destination_file = trim($destination_file);
$res = false;
if($image)
{
switch($image_type)
{
case 'JPEG':
case 'JPG':
$res = ImageJpeg($image, $destination_file, $this -> jpeg_quality);
break;
case 'PNG':
$res = Imagepng($image, $destination_file);
break;
default:
$this -> error($ERR["UNKNOWN_OUTPUT_FORMAT"]." $image_type");
break;
}
}
else
{
$this -> error($ERR["NO_IMAGE_FOR_OUTPUT"]);
}
if(!$res)
$this -> error($ERR["UNABLE_TO_OUTPUT"]." $destination_file");
return $res;
}
}
?>

ส่วนเวลาใช้งาน ใช้แบบนี้ นะครับ

โค๊ด:
$r = new Resize('___ ไฟล์ต้นทางที่จะย่อ ___');
$r -> resizing(100, 100, '+');

$r -> output_resized('___ ไฟล์ปลายที่ ที่ถูกย่อ ___.jpg', 'JPEG')

ตรง resizing ใส่ค่า 100, 100 ไว้นี่ หมายถึง ขนาดกว้าง กับ สูง นะครับ

ส่วน + ข้างหลัง เป็น option ตามนี้ + จะหมายถึง เวลาย่อ แล้ว กว้าง หรือสูง จะไม่น้อยกว่า 100 (เช่นย่อแล้ว กว้างได้ 100 แต่สูงอาจจะมากกว่า 100)

ถ้าใส่ - จะหมายถึง กว้างกะสูง จะไม่เกิน 100 (เช่นย่อแล้ว กว้างได้ 100 แต่สูงอาจจะน้อยกว่า 100)


ประมาณนี้น่ะครับ ผมว่าใช้ง่ายๆ class นี้

 wanwan017

เป็นการย่อรูปแล้วโหลดรูปในขนาดที่เล็กลงถูกป่าวครับ
บันทึกการเข้า
Synchronize
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 83
ออฟไลน์ ออฟไลน์

กระทู้: 1,231



ดูรายละเอียด
« ตอบ #5 เมื่อ: 19 พฤศจิกายน 2009, 13:23:20 »

ถูกแล้วครับ ... จะเป็นการ resize รูปให้เล็กลง ประมาณว่าสร้าง thumbnail น่ะครับ

 wanwan017
บันทึกการเข้า

presto
คนรักเสียว
*

พลังน้ำใจ: 4
ออฟไลน์ ออฟไลน์

กระทู้: 161



ดูรายละเอียด เว็บไซต์
« ตอบ #6 เมื่อ: 19 พฤศจิกายน 2009, 13:43:22 »

เข้ามาหาความรู้ ยอดเลยครับ wanwan031
บันทึกการเข้า

thespm
Newbie
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 59



ดูรายละเอียด
« ตอบ #7 เมื่อ: 19 พฤศจิกายน 2009, 14:16:47 »

ขอบคุณมากครับ
บันทึกการเข้า
นายขนมต้ม
สมุนแก๊งเสียว
*

พลังน้ำใจ: 33
ออฟไลน์ ออฟไลน์

กระทู้: 510



ดูรายละเอียด เว็บไซต์
« ตอบ #8 เมื่อ: 21 พฤศจิกายน 2009, 11:42:17 »

if($_FILES['file']['tmp_name'] != "") { //เช็คว่ามีการอัปรูป
copy($_FILES['file']['tmp_name'], "upload_file/".$_FILES['file']['name']); //ทำการ copy รูป

$images = "upload_file/".$_FILES['file']['name'];
$height = 400; //กำหนดขนาดความสูง
$size = getimagesize($images);
$width = round($height*$size[0]/$size[1]); //ขนาดความกว้่างคำนวนเพื่อความสมส่วนของรูป
if($size[2] == 1) {
$images_orig = imagecreatefromgif($images); //resize รูปประเภท GIF
} else if($size[2] == 2) {
$images_orig = imagecreatefromjpeg($images); //resize รูปประเภท JPEG
}
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = imagecreatetruecolor($width, $height);
imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
imagejpeg($images_fin, $images); //ชื่อไฟล์ใหม่
imagedestroy($images_orig);
imagedestroy($images_fin);

นี่หรือป่าวคัรบ ผมทำแบบนี้นะ ไม่รุ้ว่าใช้หรือป่าว แต่มัน resize รูปนะ
« แก้ไขครั้งสุดท้าย: 21 พฤศจิกายน 2009, 11:45:21 โดย kpp712 » บันทึกการเข้า

ขาย ACC Google Adsend รับPinแล้ว Acc8ปีสายขาว รับเงินยาวววว เงินค้าง89$

carAudio | ร้อยเอ็ด |
thespm
Newbie
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 59



ดูรายละเอียด
« ตอบ #9 เมื่อ: 22 พฤศจิกายน 2009, 21:20:14 »

คือ ที่พี่ๆ ตอบมานะครับ มันเป็นโค้ดลดรูปจริงนะแหละ แต่มันเป็นการสร้าง รูปขึ้นมาใหม่นิครับ

ผมต้องการที่ใช้รูปเดิม แล้วสั่งให้โหลดในขนาดที่เรากำหนด เช่น รูป 800+600 อยากจะให้มันโหลดมาแค่ 400*300 ครับ

แต่ไม่ใช้การย่อแล้วบันทึกเป็นรูปใหม่นะครับแต่จะเป็นการนำเอารูปเดิมมาใช้ แต่โหลดน้อยลง ยิ่งพูดยิ่ง งง

สรุปนะครับ คือ มีรูป แล้วต้องการแสดงรูปออกมาในหน้ารวมรูป แล้วก็คลิกเข้าไปดูรูปใหญ่อีกทีนะครับ

แต่ในตอนที่แสดงในหน้ารวมนะครับ ใช้ไฟล์ตัวเดวกับ ไฟล์ที่ใช้แสดงรูปใหญ่เลยนะครับ แต่โหลดน้อยกว่า นะครับ น่าจะพอเข้าใจนะครับ

คือจะทำ ทำเนียบรูปรุ่นอ่ะ

(ที่จะทำแบบนี้เพื่อประหยัด พื้นที่จัดเก็บ มีน้อยอ่ะ อิอิ แบนวิช ด้วย)


ขอบคุณล้วงหน้านะครับ
บันทึกการเข้า
นายขนมต้ม
สมุนแก๊งเสียว
*

พลังน้ำใจ: 33
ออฟไลน์ ออฟไลน์

กระทู้: 510



ดูรายละเอียด เว็บไซต์
« ตอบ #10 เมื่อ: 24 พฤศจิกายน 2009, 23:57:59 »

ก็เหมือนกันคับ คือจะทำเป็น thumbnails หรือป่าว แต่ต้องแปลงcode อีกนิดหน่อย
บันทึกการเข้า

ขาย ACC Google Adsend รับPinแล้ว Acc8ปีสายขาว รับเงินยาวววว เงินค้าง89$

carAudio | ร้อยเอ็ด |
chui761
Verified Seller
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 860
ออฟไลน์ ออฟไลน์

กระทู้: 9,477



ดูรายละเอียด เว็บไซต์
« ตอบ #11 เมื่อ: 25 พฤศจิกายน 2009, 00:05:27 »

เท่าที่เข้าใจ
ยังไงก็น่าจะต้องสร้าง thumbnail เก็บเป็นเหมือนรูปใหม่อยู่ดีนะครับ อาจจะเก็บไว้คนละโฟลเดอร์กับรูปจริง

และ thumbnail ใหม่นี้ปกติมันก็ไม่ได้มีขนาดไฟล์ใหญ่เท่าไหร่นะครับ ก็ไม่น่าจะกินแบนวิท หรือพื้นที่มากมาย

แต่ถ้าเข้าใจผิดก็ขออภัย
บันทึกการเข้า

bubbleball
Administrator
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 444
ออฟไลน์ ออฟไลน์

กระทู้: 5,695



ดูรายละเอียด
« ตอบ #12 เมื่อ: 25 พฤศจิกายน 2009, 12:37:53 »

เหมือน imageResize  plugin ของ smf  เวลาแปะรูป ถ้าเกิดขนาดที่ต้องการมันจะย่อลงมาให้เองอัตโนมัติ แล้วจะขึ้นบอกว่าสามารถกดดูรูปขนาดเต็มได้ ในหน้านั้น หรือเปิดหน้าใหม่ น่าจะลองเอาตัวนั้นมาแกะดูฟังก์ชั่นมันได้อ่ะนะ
บันทึกการเข้า

กลุ่มซื้อขายบริการเกี่ยวกับ SEO อื่นๆ โดยตรง
SEO MARKET THAILAND

สงสัยติดต่ออะไรไปทักหาที่ Fair Thailand (ไม่ค่อยอ่านกล่องข้อความที่นี่)

Fair Market Thailand   กลุ่มค้าขายรวมอื่นๆ ในภายหลัง


ปลาทอง
ลายเซนต์สูงไม่เกิน 250px
ball6847
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 212
ออฟไลน์ ออฟไลน์

กระทู้: 4,174



ดูรายละเอียด
« ตอบ #13 เมื่อ: 25 พฤศจิกายน 2009, 14:05:46 »

ที่เค้าให้มามันก็ถูกต้องแล้วครับ

สิ่งที่ต้องทำก็คือ ตอนอับโหลดไม่ใช่แค่อับโหลดก็จบ สิ่งที่ต้องทำคือสร้าง thumbnail ด้วย ก็ใช้โค้ดที่ข้างบนให้ๆไปนั่นแหละครับ สรุป คือ 1 รูป จะได้รูปจริงกับเล็ก ต้องเปลืองพื้นที่อีกนิดนึง

พอไอ้หน้าที่รวมรูปเนี่ย เราก็เอา url ของรูปเล็กไปแสดง พอคลิ้กเข้ามาเราก็แสดงรูปใหญ่ มันต้องป็นแบบนี้ครับ

แต่ที่เจ้าของกระทู้ต้องการคือ คงไม่อยากจะสร้างใหม่ให้เอารูปเดิมมาสร้าง thumbnail แล้วแสดงแบบย่อส่วนณเดี๋ยวนั้นเลย คือจะทำมันก็ทำได้ แต่ถ้าเป็น shared host ก็รอวันโดนเตะได้เลย เพราะ php ทำงานกับรูปภาพจะใช้ cpu และ memory มากกว่าปกติ คิดง่ายๆ รูปใหญ่เท่าไหร่ใช้ memory เท่านั้น เราเปิด 1 หน้ารวมรูปสมมุติมีรูป 20 รูป มันจะใช้ cpu และ memory ไปเท่าไหร่แล้วครับ แต่ถ้าเป็นเครื่องตัวเองก็หมดห่วงครับ

ผมมีคลาสแนะนำผมใช้ประจำหากต้องทำงานกับรูปภาพครับ

http://phpthumb.gxdlabs.com/

ปล  แบนวิด พื้นที่ cpu และ memory ถ้าต้องแลกผมขอเอาพื้นที่ไปแลกดีกว่า
บันทึกการเข้า

g-ji
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 231
ออฟไลน์ ออฟไลน์

กระทู้: 3,254



ดูรายละเอียด เว็บไซต์
« ตอบ #14 เมื่อ: 25 พฤศจิกายน 2009, 14:20:18 »

เมื่อก่อนเคยใช้แบบนี้

CSS magic

โค๊ด:
.resizesmall{
width: 80px;
height : auto;
}

.resizesmall{
width: auto;
height : 50px;
}
.resizethumbnail {
width: 140px;
height : auto;
}

.resizethumbnail{
width: auto;
height : 140px;
}

.resizemedium {
width: 400px;
height : auto;
}

.resizemedium{
width: auto;
height : 250px;
}

.resizelarge{
width: 500px;
height : auto;
}

.resizelarge{
width: auto;
height : 350px;
}

เอาใส่ไว้ในไฟล์ CSS นะ

เวลาใช้งานก็

<img src="ที่อยู่รูป" class="resizemedium" />

รูปมันจะย่อเหลือขนาดไม่เกิน 400 * 250 (แล้วแต่กำหนดขนาด) ลองเอาไปเล่นดูจิ ใช้ไฟล์รูปเดิม แต่ข้อเสียคือ รูปมันจะไม่ชัด และขนาดไฟล์เท่าเดิม - -*

ตอนหลังเลือกเปลี่ยนมาใช้ Timthumb แทน รูปชัดดี แล้วก็ไม่กิน ทรัพยากรโฮสมากเท่าไร (สร้างไฟล์แคชรูป ขนาดเล็ก อ่า... โคตรเล็ก แล้วลบอัตโนมัติ ทุก 30 วัน แต่จี้ตั้งไว้ไม่ต้องลบ เพราะใช้รูปเดียวถาวร มันจะดึงรูปนั้นมาใช้ตลอด)

timthumb
โค๊ด:
http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/
« แก้ไขครั้งสุดท้าย: 25 พฤศจิกายน 2009, 14:25:18 โดย g-ji » บันทึกการเข้า

thespm
Newbie
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 59



ดูรายละเอียด
« ตอบ #15 เมื่อ: 28 พฤศจิกายน 2009, 21:00:06 »

ขอบคุณทุกคนมากครับ

ผมว่าผมลงทุนใช้ พื้นที่ดีกว่าครับ ดีกว่าโดนเตะ อิอิ
บันทึกการเข้า
SL_master
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 57
ออฟไลน์ ออฟไลน์

กระทู้: 1,188



ดูรายละเอียด เว็บไซต์
« ตอบ #16 เมื่อ: 30 พฤศจิกายน 2009, 14:10:21 »

การโหลดรูปขึ้นมาผมว่าไม่น่าจะถึงขั้นโดนเตะมั๊งครับ
มันจะกินแค่แบนวิท เป็นช่วงๆ
ไม่เหมือนการใช้คำสั่งดึงดาต้าเบส ซึ่งจะกินแรม และ cpu มาก

ถ้าเวปแค่โชว์รูปอย่างเดียว คงไม่น่าจะมีปัญหามั๊งครับ
ถ้าเราเป็นแพคเกจที่แบทวิทunlimit หรือ ได้แพคเกจที่ใช้แบนวิทได้เยอะ
บันทึกการเข้า

หน้า: [1]   ขึ้นบน
พิมพ์