Php code for generating proportional thumbnails for uploading images _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
Php code for generating proportional thumbnails for uploading images. Upload an image file using php and generate a Scaled thumbnail. For more information, see. The code is as follows: Copy the code? Phpfunction_UP a simple example of using php to upload an image file and then generate an equal proportion of the thumbnail effect. For more information, see.
The code is as follows:

Function _ UPLOADPIC ($ upfile, $ maxsize, $ updir, $ newname = 'Date '){

If ($ newname = 'Date ')

$ Newname = date ("Ymdhis"); // use date as the file name

$ Name = $ upfile ["name"];

$ Type = $ upfile ["type"];

$ Size = $ upfile ["size"];

$ Tmp_name = $ upfile ["tmp_name"];

Switch ($ type ){

Case 'image/pjpeg ':

Case 'image/jpeg ':

$ Extend = ". jpg ";

Break;

Case 'image/GIF ':

$ Extend = ". gif ";

Break;

Case 'image/png ':

$ Extend = ". png ";

Break;

}

If (emptyempty ($ extend )){

Echo ("Warning! Only the image type can be uploaded: GIF. JPG ");

Exit ();

}

If ($ size> $ maxsize ){

$ Maxpr = $ maxsize/1000;

Echo ("Warning! The size of the uploaded image cannot exceed ". $ maxpr." K! ");

Exit ();

}

If (move_uploaded_file ($ tmp_name, $ updir. $ newname. $ extend )){

Return $ updir. $ newname. $ extend;

}

}

Function show_pic_scal ($ width, $ height, $ picpath ){

$ Imginfo = GetImageSize ($ picpath );

$ Imgw = $ imginfo [0];

$ Imgh = $ imginfo [1];

$ Ra = number_format ($ imgw/$ imgh), 1); // aspect ratio

$ Ra2 = number_format ($ imgh/$ imgw), 1); // aspect ratio

If ($ imgw> $ width or $ imgh> $ height ){

If ($ imgw> $ imgh ){

$ NewWidth = $ width;

$ NewHeight = round ($ newWidth/$ ra );

} Elseif ($ imgw <$ imgh ){

$ NewHeight = $ height;

$ NewWidth = round ($ newHeight/$ ra2 );

} Else {

$ NewWidth = $ width;

$ NewHeight = round ($ newWidth/$ ra );

}

} Else {

$ NewHeight = $ imgh;

$ NewWidth = $ imgw;

}

$ Newsize [0] = $ newWidth;

$ Newsize [1] = $ newHeight;

Return $ newsize;

}

Function getImageInfo ($ src)

{

Return getimagesize ($ src );

}

/**

* Create an image and return the resource type

* @ Param string $ src Image path

* @ Return resource $ im: returned resource type

***/

Function create ($ src)

{

$ Info = getImageInfo ($ src );

Switch ($ info [2])

{

Case 1:

$ Im = imagecreatefromgif ($ src );

Break;

Case 2:

$ Im = imagecreatefromjpeg ($ src );

Break;

Case 3:

$ Im = imagecreatefrompng ($ src );

Break;

}

Return $ im;

}

/**

* Thumbnail main function

* @ Param string $ src Image path

* @ Param int $ w thumbnail width

* @ Param int $ h Thumbnail height

* @ Return mixed return the thumbnail path

***/

Function resize ($ src, $ w, $ h)

{

$ Temp = pathinfo ($ src );

$ Name = $ temp ["basename"]; // file name

$ Dir = $ temp ["dirname"]; // folder where the file is located

$ Extension = $ temp ["extension"]; // file extension

$ Savepath = "{$ dir}/{$ name}"; // saved path of the thumbnail. the new file name is * .thumb.jpg.

// Obtain basic image information

$ Info = getImageInfo ($ src );

$ Width = $ info [0]; // get the image width

$ Height = $ info [1]; // get the image height

$ Per1 = round ($ width/$ height, 2); // calculate the aspect ratio of the original image.

$ Per2 = round ($ w/$ h, 2); // calculate the thumbnail aspect ratio.

// Calculate the scaling ratio

If ($ per1> $ per2 | $ per1 = $ per2)

{

// If the aspect ratio of the source image is greater than or equal to the aspect ratio of the thumbnail, the width prevails.

$ Per = $ w/$ width;

}

If ($ per1 <$ per2)

{

// If the aspect ratio of the source image is smaller than the aspect ratio of the thumbnail, the height prevails.

$ Per = $ h/$ height;

}

$ Temp_w = intval ($ width * $ per); // calculates the width of the scaled source image.

$ Temp_h = intval ($ height * $ per); // calculate the scaled height of the source image.

$ Temp_img = imagecreatetruecolor ($ temp_w, $ temp_h); // Create a canvas

$ Im = create ($ src );

Imagecopyresampled ($ temp_img, $ im, 0, 0, 0, $ temp_w, $ temp_h, $ width, $ height );

If ($ per1> $ per2)

{

Imagejpeg ($ temp_img, $ savepath, 100 );

Imagedestroy ($ im );

Return addBg ($ savepath, $ w, $ h, "w ");

// The width takes precedence and the background is added when the height is insufficient after scaling.

}

If ($ per1 = $ per2)

{

Imagejpeg ($ temp_img, $ savepath, 100 );

Imagedestroy ($ im );

Return $ savepath;

// Proportional scaling

}

If ($ per1 <$ per2)

{

Imagejpeg ($ temp_img, $ savepath, 100 );

Imagedestroy ($ im );

Return addBg ($ savepath, $ w, $ h, "h ");

// The height takes precedence and the background is added when the width is insufficient after scaling.

}

}

/**

* Add background

* @ Param string $ src Image path

* @ Param int $ w background image width

* @ Param int $ h background image height

* @ Param String $ first determines the final position of the image. w width first h height first wh: Proportional

* @ Return returns the image with the background added.

***/

Function addBg ($ src, $ w, $ h, $ fisrt = "w ")

{

$ Bg = imagecreatetruecolor ($ w, $ h );

$ White = imagecolorallocate ($ bg, 255,255,255 );

Imagefill ($ bg, $ white); // fill the background

// Obtain the target image information

$ Info = getImageInfo ($ src );

$ Width = $ info [0]; // target image width

$ Height = $ info [1]; // target Image height

$ Img = create ($ src );

If ($ fisrt = "wh ")

{

// Proportional scaling

Return $ src;

}

Else

{

If ($ fisrt = "w ")

{

$ X = 0;

$ Y = ($ h-$ height)/2; // center vertically

}

If ($ fisrt = "h ")

{

$ X = ($ w-$ width)/2; // horizontally centered

$ Y = 0;

}

Imagecopymerge ($ bg, $ img, $ x, $ y, 100, $ width, $ height );

Imagejpeg ($ bg, $ src, 100 );

Imagedestroy ($ bg );

Imagedestroy ($ img );

Return $ src;

}
}
?>

Usage:

$ Filename = (_ UPLOADPIC ($ _ FILES ["upload"], $ maxsize, $ updir, $ newname = 'Date '));
$ Show_pic_scal = show_pic_scal (230,230, $ filename );
Resize ($ filename, $ show_pic_scal [0], $ show_pic_scal [1]);

Bytes. The code is as follows? Php function_UP...

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.