【PHPサムネイル類】携帯電話の写真でサムネイルを生成できない問題および解決策


【オリジナル、転載お断り】
一、発生した問題
ここ数日、携帯電話で写真をアップロードしてサムネイルを切り出すインタフェースのテストをしたところ、いずれにしても生成されたサムネイルは真っ暗だった.(
そしてこのサムネイル類リストを出してテストしたところ、携帯電話で撮った写真であればサムネイルの処理はできないことがわかりました...
二、問題分析及び解決方案
群の中の教えを得て、問題はファイルのタイプの判断に現れるかもしれないことを発見して、pngのピクチャーは1つの透明なレイヤーを持っているため、直接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
}