PHP實現(xiàn)將上傳圖片自動縮放到指定分辨率,并保持清晰度封裝類示例
更新時間:2019年06月17日 10:57:10 作者:ltx06
這篇文章主要介紹了PHP實現(xiàn)將上傳圖片自動縮放到指定分辨率,并保持清晰度封裝類,涉及php針對jpeg、png、gif等格式圖片的縮放與輸出相關操作技巧,需要的朋友可以參考下
本文實例講述了PHP實現(xiàn)將上傳圖片自動縮放到指定分辨率,并保持清晰度封裝類。分享給大家供大家參考,具體如下:
class AutoImage{ private $image; public function resize($src, $width, $height){ //$src 就是 $_FILES['upload_image_file']['tmp_name'] //$width和$height是指定的分辨率 //如果想按指定比例放縮,可以將$width和$height改為$src的指定比例 $this->image = $src; $info = getimagesize($src);//獲取圖片的真實寬、高、類型 if($info[0] == $width && $info[1] == $height){ //如果分辨率一樣,直接返回原圖 return $src; } switch ($info['mime']){ case 'image/jpeg': header('Content-Type:image/jpeg'); $image_wp = imagecreatetruecolor($width, $height); $image_src = imagecreatefromjpeg($src); imagecopyresampled($image_wp, $image_src, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); imagedestroy($image_src); imagejpeg($image_wp,$this->image); break; case 'image/png': header('Content-Type:image/png'); $image_wp = imagecreatetruecolor($width, $height); $image_src = imagecreatefrompng($src); imagecopyresampled($image_wp, $image_src, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); imagedestroy($image_src); imagejpeg($image_wp,$this->image); break; case 'image/gif': header('Content-Type:image/gif'); $image_wp = imagecreatetruecolor($width, $height); $image_src = imagecreatefromgif($src); imagecopyresampled($image_wp, $image_src, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); imagedestroy($image_src); imagejpeg($image_wp,$this->image); break; } return $this->image; } }
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設計算法總結(jié)》、《PHP數(shù)學運算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP file_get_contents 函數(shù)超時的幾種解決方法
在使用file_get_contents函數(shù)的時候,經(jīng)常會出現(xiàn)超時的情況,在這里要通過查看一下錯誤提示,看看是哪種錯誤,比較常見的是讀取超時,這種情況大家可以通過一些方法來盡量的避免或者解決。2009-07-07PHP數(shù)組操作實例分析【添加,刪除,計算,反轉(zhuǎn),排序,查找等】
這篇文章主要介紹了PHP數(shù)組操作,結(jié)合實例形式分析php針對數(shù)組的添加,刪除,計算,反轉(zhuǎn),排序,查找等操作實現(xiàn)技巧,需要的朋友可以參考下2016-12-12