標籤:php 縮圖 png
【本文原創,謝絕轉載】
一、出現的問題這幾天做了手機上傳照片並裁出縮圖的介面的測試,發現不管怎麼,產生的縮圖都是一片漆黑。:-(然後就把這個縮圖類單拿出來進行測試,發現只要是手機拍出來的照片都不能進行縮圖的處理。。。。
二、問題分析以及解決方案
經過群裡的請教,發現問題可能是出現在檔案的類型的判斷上,因為png圖片內建一個透明的圖層,導致不能直接轉換成jpg的檔案,而手機排出的照片副檔名是jpg.所以,得出的結論是
手機拍出的是jpg副檔名的png圖片。
由於副檔名是可以隨意修改的,不是很能保證檔案的資訊的準確性,所以我們採用了 getimagesize 函數進行檔案類型的擷取。
//擷取真實的圖片類型 list($width, $height, $type, $attr) = getimagesize($this->sur_file); switch($type) { case 1 : $img_type = 'gif'; break; case 2 : $img_type = 'jpeg'; break; case 3 : $img_type = 'png'; break; case 15 : $img_type = 'wbmp'; break; default : return false; }
三、產生縮圖類
下面把修改後的產生縮圖的類貼出來,供大家指正~
/** * php產生縮圖類 * 修改者 點點細雨 * 文章出處 : http://blog.csdn.net/diandianxiyu_geek * 2014-07-23 解決了圖片類型不能正常識別的問題 */class thumb { public $sur_file; //讀取的原圖片 public $des_file; //產生靶心圖表片 public $tem_file; //臨時圖片 public $tag; //縮減標籤 0,預設,按固定的高寬產生 1,按比列或固定最大長度產生 -1,按某個寬度或某個高度縮小 public $resize_width; //$tag為0時,目標檔案寬 public $resize_height; //$tag為0時,目標檔案高 public $sca_max; //$tag為1時,<0$sca_max<1時為縮小比例;$sca_max>1時為最大長度(高或寬之中的最大值) public $type; //圖片類型 public $width; //原圖片寬 public $height; //原圖片高 public $size; //原圖大小 //建構函式 public function __construct($surpic, $reswid, $reshei, $despic, $mark, $scamax) { $this->sur_file = $surpic; $this->resize_width = $reswid; $this->resize_height = $reshei; $this->tag = $mark; $this->sca_max = $scamax; list($width, $height, $type, $attr) = getimagesize($this->sur_file); switch ($type) { case 1 : $img_type = 'gif'; break; case 2 : $img_type = 'jpeg'; break; case 3 : $img_type = 'png'; break; case 15 : $img_type = 'wbmp'; break; default : return false; } $this->type = $img_type; //擷取圖片類型 $this->init_img(); //初始化圖片 $this->des_file = $despic; //靶心圖表片地址 $this->width = $width; $this->height = $height; $this->size = filesize($surpic); $this->new_img(); imagedestroy($this->tem_file); } //圖片初始化函數 private function init_img() { if ($this->type == 'jpeg') { $this->tem_file = imagecreatefromjpeg($this->sur_file); } elseif ($this->type == 'jpg') { $this->tem_file = imagecreatefromjpeg($this->sur_file); } elseif ($this->type == 'gif') { $this->tem_file = imagecreatefromgif($this->sur_file); } elseif ($this->type == 'png') { $this->tem_file = imagecreatefrompng($this->sur_file); } elseif ($this->type == 'bmp') { $this->tem_file = imagecreatefrombmp($this->sur_file); //bmp.php中包含 } } //圖片產生函數 private function new_img() { $ratio = ($this->width) / ($this->height); //原圖比例 $resize_ratio = ($this->resize_width) / ($this->resize_height); //縮減後比例 $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //產生新圖片 imagealphablending($newimg, false); //這裡很重要,意思是不合并顏色,直接用$img映像顏色替換,包括透明色; imagesavealpha($newimg, true); if ($this->tag == 0) { //按固定高寬截取縮圖 $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); //產生新圖片 if ($ratio >= $resize_ratio) {//即等比例下,縮圖的高比原圖長,因此高不變 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, (($this->height) * $resize_ratio), $this->height); } elseif ($ratio < $resize_ratio) {//即等比例下,縮圖的寬比原圖長,因此寬不變 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width) / $resize_ratio)); } } elseif ($this->tag == 1) { //按固定比例或最大長度縮小 if ($this->sca_max < 1) { //按比例縮小 $newimg = imagecreatetruecolor((($this->width) * ($this->sca_max)), (($this->height) * ($this->sca_max))); //產生新圖片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, (($this->width) * ($this->sca_max)), (($this->height) * ($this->sca_max)), $this->width, $this->height); } elseif ($this->sca_max > 1) { //按某個最大長度縮小 if ($ratio >= 1) { //寬比高長 $newimg = imagecreatetruecolor($this->sca_max, ($this->sca_max / $ratio)); //產生新圖片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max, ($this->sca_max / $ratio), $this->width, $this->height); } else { $newimg = imagecreatetruecolor(($this->sca_max * $ratio), $this->sca_max); //產生新圖片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->sca_max * $ratio), $this->sca_max, $this->width, $this->height); } } } elseif ($this->tag == -1) { //按某個寬度或某個高度縮小 if ($resize_ratio >= 1) {//新高小於新寬,則圖片按新寬度縮小 $newimg = imagecreatetruecolor($this->resize_width, ($this->resize_width / $ratio)); //產生新圖片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, ($this->resize_width / $ratio), $this->width, $this->height); } elseif ($resize_ratio < 1) {//新寬小於新高,則圖片按新高度縮小 $newimg = imagecreatetruecolor(($this->resize_height * $ratio), $this->resize_height); //產生新圖片 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->resize_height * $ratio), $this->resize_height, $this->width, $this->height); } } //輸出新圖片 if ($this->type == 'jpeg' || $this->type == 'jpg') { imagejpeg($newimg, $this->des_file); } elseif ($this->type == 'gif') { imagegif($newimg, $this->des_file); } elseif ($this->type == 'png') { imagepng($newimg, $this->des_file); } elseif ($this->type == 'bmp') { imagebmp($newimg, $this->des_file); //bmp.php中包含 } }#function new_img() end}
【PHP縮圖類】手機照片不能產生縮圖問題以及解決方案