PHP Compressed Image function

Source: Internet
Author: User
/** image Compression function * @param $orgin _file original picture file * @param $maxwidth maximum width * @param $maxheight Maximum height * @param $name Compressed picture name * @param $fil EType Picture Type */function resizeimage ($orgin _file, $maxwidth, $maxheight, $name, $filetype) {$im = "; switch ($filetype) {case '. jpg ': $im =imagecreatefromjpeg ($orgin _file); Break;case '. png ': $im =imagecreatefrompng ($orgin _file); Break;case '. GIF ': $im =imagecreatefromgif ($orgin _file); Break;default: $im =imagecreatefromgd ($orgin _file);} $pic _width = Imagesx ($im), $pic _height = Imagesy ($im), if ($maxwidth && $pic _width > $maxwidth) | | ($maxheight && $pic _height > $maxheight)) {if ($maxwidth && $pic _width> $maxwidth) {$widthratio = $maxwidth/$pic _width; $resizewidth _tag = true;} if ($maxheight && $pic _height> $maxheight) {$heightratio = $maxheight/$pic _height; $resizeheight _tag = true;} if ($resizewidth _tag && $resizeheight _tag) {if ($widthratio < $heightratio) $ratio = $widthratio; else$ratio = $ HeightRatio;} if ($resizewidth _tag &&! $resizeheighT_tag) $ratio = $widthratio, if ($resizeheight _tag &&! $resizewidth _tag) $ratio = $heightratio; $newwidth = $pic _ Width * $ratio; $newheight = $pic _height * $ratio; if (function_exists ("imagecopyresampled") {$newim = Imagecreatetruecolor ($newwidth, $newheight);//php system function imagecopyresampled ($newim, $im, 0,0,0,0, $newwidth, $newheight , $pic _width, $pic _height)//php system function}else{$newim = imagecreate ($newwidth, $newheight); imagecopyresized ($newim, $im, 0,0,0,0, $newwidth, $newheight, $pic _width, $pic _height);} $name = $name. $filetype, switch ($filetype) {case '. jpg ': imagejpeg ($newim, $name); Break;case '. png ': Imagepng ($newim, $ name); Break;case '. gif ': Imagegif ($newim, $name); Break;default:imagegd ($newim, $name);} Imagedestroy ($newim);} else{$name = $name. $filetype, switch ($filetype) {case '. jpg ': imagejpeg ($im, $name); Break;case '. png ': Imagepng ($im, $ name); Break;case '. gif ': Imagegif ($im, $name); Break;default:imagegd ($im, $name);}}
Image Watermark can refer to the "Imagecopymerge" function, the manual is very detailed.

The above describes the PHP compressed picture function, including the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.