Can PHP's GD library compress images by percentage?

Source: Internet
Author: User
Tags imagejpeg
Using the PHP GD library to compress pictures, you can fill in the specific width and height for compression. So, can be compressed by percentage, such as compressed into the original 90%,80% .... such as?

Reply content:

Using the PHP GD library to compress pictures, you can fill in the specific width and height for compression. So, can be compressed by percentage, such as compressed into the original 90%,80% .... such as?

Yes, I just got a compress for my friend at noon.

    
   $maxWidth) {$maxWidth = $maxWidth;       if ($srcHeight > $maxHeight) {$maxHeight = ($srcHeight/$srcHeight) * $maxWidth;       } else {$maxHeight = $srcHeight;      } return Array (' width ' = $maxWidth, ' height ' = $maxHeight);       } if ($srcHeight > $maxHeight) {$maxHeight = $maxHeight;       if ($srcWidth > $maxWidth) {$maxWidth = ($srcWidth/$srcHeight) * $maxHeight;       } else {$maxWidth = $srcWidth;      } return Array (' width ' = $srcWidth, ' height ' = $maxHeight);    } return Array (' width ' = $srcWidth, ' height ' = $srcHeight); }/** * Equal scale generation thumbnail * * @param string $srcFile original file path * @param string $dstFile destination file path * @p Aram integer $maxWidth The maximum width of the generated target file * @param the maximum height of the target file generated by the integer $maxHeight * @return The Boolean build succeeds returns the TR UE, otherwise returns false */function Makethumb ($srcFile, $dstFile, $maxWidth, $maxHeight) {if ($size = gEtimagesize ($srcFile)) {$srcWidth = $size [0];       $srcHeight = $size [1];       $mime = $size [' MIME '];         Switch ($mime) {case ' image/jpeg ';         $isJpeg = true;        Break         Case ' image/gif ';         $isGif = true;        Break         Case ' image/png ';         $isPng = true;        Break         Default:return false;       Break       }//header ("Content-type: $mime");       $arr = $this->getnewsize ($maxWidth, $maxHeight, $srcWidth, $srcHeight);       $thumbWidth = $arr [' width '];       $thumbHeight = $arr [' height '];        if (Isset ($isJpeg) && $isJpeg) {$dstThumbPic = Imagecreatetruecolor ($thumbWidth, $thumbHeight);        $srcPic = Imagecreatefromjpeg ($srcFile);        Imagecopyresampled ($dstThumbPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);        Imagejpeg ($dstThumbPic, $dstFile, 100);        Imagedestroy ($dstThumbPic);        Imagedestroy ($srcPic);       return true; } ElseiF (isset ($isGif) && $isGif) {$dstThumbPic = Imagecreatetruecolor ($thumbWidth, $thumbHeight);        Create a transparent canvas imagealphablending ($dstThumbPic, true);        Imagesavealpha ($dstThumbPic, true);        $trans _colour = Imagecolorallocatealpha ($dstThumbPic, 0, 0, 0, 127);        Imagefill ($dstThumbPic, 0, 0, $trans _colour);        $srcPic = Imagecreatefromgif ($srcFile);        Imagecopyresampled ($dstThumbPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);        Imagegif ($dstThumbPic, $dstFile);        Imagedestroy ($dstThumbPic);        Imagedestroy ($srcPic);       return true;        } elseif (Isset ($isPng) && $isPng) {$dstThumbPic = Imagecreatetruecolor ($thumbWidth, $thumbHeight);        Create a transparent canvas imagealphablending ($dstThumbPic, true);        Imagesavealpha ($dstThumbPic, true);        $trans _colour = Imagecolorallocatealpha ($dstThumbPic, 0, 0, 0, 127);        Imagefill ($dstThumbPic, 0, 0, $trans _colour); $srcPic = Imagecreatefrompng ($srcFile);        Imagecopyresampled ($dstThumbPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);        Imagepng ($dstThumbPic, $dstFile);        Imagedestroy ($dstThumbPic);        Imagedestroy ($srcPic);       return true;       } else {return false;      }} else {return false; }}}?>

Imagejpeg () has a parameter to set the compression ratio
http://php.net/manual/zh/function.imagejpeg.php

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