/** * desription 壓縮圖片 * @param sting $imgsrc 圖片路徑 * @param string $imgdst 壓縮后保存路徑 public function compressedImage($imgsrc, $imgdst) { list($width, $height, $type) = getimagesize($imgsrc); $new_width = $width;//壓縮后的圖片寬 $new_height = $height;//壓縮后的圖片高 if($width = 600){ $per = 600 / $width;//計算比例 $new_width = $width * $per; $new_height = $height * $per; switch ($type) { case 1: $giftype = check_gifcartoon($imgsrc); if ($giftype) { header( Content-Type:image/gif $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是質量、壓縮圖片容量大小 imagejpeg($image_wp, $imgdst, 90); imagedestroy($image_wp); imagedestroy($image); break; case 2: header( Content-Type:image/jpeg $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是質量、壓縮圖片容量大小 imagejpeg($image_wp, $imgdst, 90); imagedestroy($image_wp); imagedestroy($image); break; case 3: header( Content-Type:image/png $image_wp = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //90代表的是質量、壓縮圖片容量大小 imagejpeg($image_wp, $imgdst, 90); imagedestroy($image_wp); imagedestroy($image); break; }
相關推薦:
如何使用php代碼實現印章的摳圖(代碼)
php實現支付寶app支付和異步通知的代碼實例
以上就是php如何實現等比例壓縮圖片的代碼的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答