未分類

php getimagesize的用法

getimagesize 可以檢查圖片大小格式等等資訊
可以在上傳時做檢查!

 $imgsize[1]) {    
echo "這是4x6格式的照片";
} else {
echo "這是6x4格式的照片";}

function ImageCopyResizedTrue($src,$dest,$maxWidth,$maxHeight,$quality=100) { 

        
        if (file_exists($src)  && isset($dest)) { 

            $destInfo  = pathInfo($dest); 
            $srcSize   = getImageSize($src); 
            $srcRatio  = $srcSize[0]/$srcSize[1]; 
            $destRatio = $maxWidth/$maxHeight; 
            if ($destRatio > $srcRatio) { 
                $destSize[1] = $maxHeight; 
                $destSize[0] = $maxHeight*$srcRatio; 
            } 
            else { 
                $destSize[0] = $maxWidth; 
                $destSize[1] = $maxWidth/$srcRatio; 
            } 


           
            if ($destInfo['extension'] == "gif") $dest = substr_replace($dest, 'jpg', -3); 

          
            $destImage = imageCreateTrueColor($destSize[0],$destSize[1]); 

           
            switch ($srcSize[2]) { 
                case 1: $srcImage = imageCreateFromGif($src); break; 
                case 2: $srcImage = imageCreateFromJpeg($src); break; 
                case 3: $srcImage = imageCreateFromPng($src); break; 
                default: return false; break; 
            } 

          
            ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1], 
                                $srcSize[0],$srcSize[1]); 

       
            switch ($srcSize[2]) { 
                case 1: case 2: imageJpeg($destImage,$dest,$quality); break; 
                case 3: imagePng($destImage,$dest); break; 
            } 
            return true; 
        } 
        else { 
            return false; 
        } 
    } 

?>