Php image scaling code-a very useful method for proportional scaling or capturing a thumbnail of a specified size

Source: Internet
Author: User
/*************************************** * ******************* Functionresize: ** createsaresizedimagebasedonthemaxwidth * specifiedaswellasgenerates

 

/*************************************** *******************

* Function resize:

*

* = Creates a resized image based on the max width

* Specified as well as generates a thumbnail from

* A rectangle cut from the middle of the image.

*

* @ Dir = directory image is stored in

* @ Newdir = directory new image will be stored in

* @ Img = the image name

* @ Newimg = the image new name

* @ Max_w = the max width of the resized image

* @ Max_h = the max height of the resized image

* @ Th_x = The starting point of the x-coordinate

* @ Th_y = The starting point of the y-coordinate

* @ Th_w = the width of the thumbnail

* @ Th_h = the height of the thumbnail

* @ Cut = Whether to need cut out

* @ Center = whether cut from the middle of the image

*

**************************************** ******************/

 

Function resize ($ dir, $ newdir, $ img, $ newimg, $ max_w, $ max_h, $ th_x = '', $ th_y ='', $ th_w = '', $ th_h = '', $ cut = FALSE, $ center = FALSE)

{

// Set destination directory

If (! $ Newdir) $ newdir = $ dir;

If (! $ Newimg) $ newimg = $ img;

 

// Get original images width and height

List ($ or_w, $ or_h, $ or_t) = getimagesize ($ dir. $ img );

Switch ($ or_t ){

// Original image

Case 1:

$ Or_image = imagecreatefromgif ($ dir. $ img );

Break;

Case 2:

$ Or_image = imagecreatefromjpeg ($ dir. $ img );

Break;

Case 3:

$ Or_image = imagecreatefrompng ($ dir. $ img );

Break;

Default:

Return 'unsupported Image format'; www.2cto.com

Break;

}

 

// Make sure image is a jpeg

// If ($ or_t = 2 ){

// Obtain the image's ratio

// $ Ratio = ($ or_h/$ or_w );

$ Ratio = ($ max_h/$ max_w );

 

// Original image

// $ Or_image = imagecreatefromjpeg ($ dir. $ img );

 

// Resize image?

If ($ or_w> $ max_w | $ or_h> $ max_h ){

 

// Resize by height, then width (height dominant)

If ($ max_h <$ max_w ){

$ Rs_h = $ max_h;

$ Rs_w = $ rs_h/$ ratio;

}

// Resize by width, then height (width dominant)

Else {

$ Rs_w = $ max_w;

$ Rs_h = $ ratio * $ rs_w;

}

 

// Copy old image to new image

$ Rs_image = imagecreatetruecolor ($ rs_w, $ rs_h );

Imagecopyresampled ($ rs_image, $ or_image, 0, 0, 0, 0, $ rs_w, $ rs_h, $ or_w, $ or_h );

}

// Image requires no resizing

Else {

$ Rs_w = $ or_w;

$ Rs_h = $ or_h;

 

$ Rs_image = $ or_image;

}

 

// Generate resized image

DrawImage ($ rs_image, $ newdir. $ newimg, $ or_t, 100 );

 

// Crop to generate the specified size start -------------------

If ($ cut ){

$ Th_image = imagecreatetruecolor ($ th_w, $ th_h );

 

// Cut out a rectangle from the resized image and store in thumbnail

If ($ center ){

$ New_w = ($ rs_w/2)-($ th_w/2 ));

$ New_h = ($ rs_h/2)-($ th_h/2 ));

} Else {

$ New_w = $ th_x;

$ New_h = $ th_y;

}

Imagecopyresampled ($ th_image, $ rs_image, 0, 0, $ new_w, $ new_h, $ rs_w, $ rs_h, $ rs_w, $ rs_h );

// Generate thumbnail

DrawImage ($ th_image, $ newdir. 'th _ '. $ newimg, $ or_t, 100 );

@ ImageDestroy ($ th_image );

}

// Generate the specified size of end -------------------

 

// Return true;

//}

 

// Image type was not jpeg!

/* Else {

Return false;

}*/

@ ImageDestroy ($ rs_image );

}

 

/**

* @ Resource image

* @ Filename

* @ Quality

* @ Type

**/

 

Function DrawImage ($ resource, $ filename, $ type, $ quality ){

Switch ($ type ){

// Original image

Case 1:

$ Or_image = imagegif ($ resource, $ filename );

Break;

Case 2:

$ Or_image = imagejpeg ($ resource, $ filename, $ quality );

Break;

Case 3:

$ Or_image = imagepng ($ resource, $ filename );

Break;

}

}

 

From the aa705123123 column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.