Code for generating proportional thumbnails for uploading images in php

Source: Internet
Author: User
Tags imagejpeg

Upload an image file using php and generate a scaled thumbnail. For more information, see.

The Code is as follows: Copy code

 

<? Php
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]);

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.