PHP實(shí)現(xiàn)將上傳圖片自動(dòng)縮放到指定分辨率,并保持清晰度封裝類示例
本文實(shí)例講述了PHP實(shí)現(xiàn)將上傳圖片自動(dòng)縮放到指定分辨率,并保持清晰度封裝類。分享給大家供大家參考,具體如下:
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);//獲取圖片的真實(shí)寬、高、類型 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; } }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP file_get_contents 函數(shù)超時(shí)的幾種解決方法
在使用file_get_contents函數(shù)的時(shí)候,經(jīng)常會(huì)出現(xiàn)超時(shí)的情況,在這里要通過查看一下錯(cuò)誤提示,看看是哪種錯(cuò)誤,比較常見的是讀取超時(shí),這種情況大家可以通過一些方法來盡量的避免或者解決。2009-07-07PHP實(shí)現(xiàn)獲取域名的方法小結(jié)
這篇文章主要介紹了PHP實(shí)現(xiàn)獲取域名的方法,實(shí)例總結(jié)了常見的獲取域名的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11PHP數(shù)組操作實(shí)例分析【添加,刪除,計(jì)算,反轉(zhuǎn),排序,查找等】
這篇文章主要介紹了PHP數(shù)組操作,結(jié)合實(shí)例形式分析php針對(duì)數(shù)組的添加,刪除,計(jì)算,反轉(zhuǎn),排序,查找等操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12