Php uploads an image and generates a specified size chart proportionally

Source: Internet
Author: User
Tags imagejpeg

Php uploads an image and generates a specified size chart proportionally. This is an image scaling function, after the new image is uploaded to $ srcfile, the file is scaled down by $ thumbwidth to the maximum image width and $ thumbheitht to generate a small image.

Upload an image in the php tutorial and generate a specified size chart proportionally
This is an image scaling function. You can give the newly uploaded image $ srcfile and then compress the image size by using $ thumbwidth to reduce the maximum size of the Image Width and $ thumbheitht to reduce the image height and the maximum size to generate a small image.
Image Scaling Function
Parameter description:

$ Srcfile source image address;
$ Dir New Graph directory
$ Thumbwidth: Zoom out the maximum graph width
$ Thumbheitht zoom in to the maximum Image Height
$ Ratio by default, proportional scaling is reduced to 1 to a fixed size.
*/
Function makethumb ($ srcfile, $ dir, $ thumbwidth, $ thumbheight, $ ratio = 0)
{
// Determine whether a file exists
If (! File_exists ($ srcfile) return false;
// Generate a new file with the same name, but the directory is different
$ Imgname = explode ('/', $ srcfile );
$ Arrcount = count ($ imgname );
$ Dstfile = $ dir. $ imgname [$ arrcount-1];
// Thumbnail size
$ Tow = $ thumbwidth;
$ Toh = $ thumbheight;
If ($ tow <40) $ tow = 40;
If ($ toh <45) $ toh = 45;
// Obtain image information
$ Im = '';
If ($ data = getimagesize ($ srcfile )){
If ($ data [2] = 1 ){
$ Make_max = 0; // gif is not processed
If (function_exists ("imagecreatefromgif ")){
$ Im = imagecreatefromgif ($ srcfile );
}
} Elseif ($ data [2] = 2 ){
If (function_exists ("imagecreatefromjpeg ")){
$ Im = imagecreatefromjpeg ($ srcfile );
}
} Elseif ($ data [2] = 3 ){
If (function_exists ("imagecreatefrompng ")){
$ Im = imagecreatefrompng ($ srcfile );
}
}
}
If (! $ Im) return '';
$ Srcw = imagesx ($ im );
$ Srch = imagesy ($ im );
$ Towh = $ tow/$ toh;
$ Srcwh = $ srcw/$ srch;
If ($ towh <= $ srcwh ){
$ Ftow = $ tow;
$ Ftoh = $ ftow * ($ srch/$ srcw );
} Else {
$ Ftoh = $ toh;
$ Ftow = $ ftoh * ($ srcw/$ srch );
}
If ($ ratio ){
$ Ftow = $ tow;
$ Ftoh = $ toh;
}
// Zoom out the image
If ($ srcw> $ tow | $ srch> $ toh | $ ratio ){
If (function_exists ("imagecreatetruecolor") & function_exists ("imagecopyresampled") & @ $ ni = imagecreatetruecolor ($ ftow, $ ftoh )){
Imagecopyresampled ($ ni, $ im, 0, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch );
} Elseif (function_exists ("imagecreate") & function_exists ("imagecopyresized") & @ $ ni = imagecreate ($ ftow, $ ftoh )){
Imagecopyresized ($ ni, $ im, 0, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch );
} Else {
Return '';
}
If (function_exists ('imagejpeg ')){
Imagejpeg ($ ni, $ dstfile );
} Elseif (function_exists ('imagepng ')){
Imagepng ($ ni, $ dstfile );
}
} Else {
// Directly copy smaller than the size
Copy ($ srcfile, $ dstfile );
}
Imagedestroy ($ im );
If (! File_exists ($ dstfile )){
Return '';
} Else {
Return $ dstfile;
}
}

?>

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.