PHP等比例壓縮圖片的實例代碼
更新時間:2018年07月26日 14:42:44 作者:kaykay012
本文通過一段簡單的代碼給大家介紹PHP等比例壓縮圖片的方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
具體代碼如下所示:
/**
* 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等比例壓縮圖片的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
ThinkPHP5實現(xiàn)作業(yè)管理系統(tǒng)中處理學生未交作業(yè)與已交作業(yè)信息的方法
這篇文章主要介紹了ThinkPHP5實現(xiàn)作業(yè)管理系統(tǒng)中處理學生未交作業(yè)與已交作業(yè)信息的方法,涉及thinkPHP針對數(shù)據(jù)表的查詢與遍歷操作相關實現(xiàn)技巧,需要的朋友可以參考下2016-11-11
關于 Laravel Redis 多個進程同時取隊列問題詳解
這篇文章主要給大家介紹了關于 Laravel Redis 多個進程同時取隊列問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友下面來一起學習學習吧。2017-12-12

