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