PHP画像サムネイル処理類

2334 ワード

/**
 *      
 * @param string $file_name    
 * @param string $file_folder
 * @param number $quality         (  :1-100),           (              )
 * @return multitype:number string
 */
function create_image_small($file_name, $file_folder, $quality = 50 , $file_folder1 = '') {
    
    $imgPathInfo = path_info($file_name);
    $imgFile = $file_folder . $file_name;
    $result = array();
    if (!file_exists($imgFile)) {
        $result['status'] = 404;
        $result['message'] = "       ";
        $result['src_file_name'] = $imgFile;
        return $result;
    }
    $imgInfo = getimagesize($imgFile);
    if ($imgInfo) {
        $imgWidth = $imgInfo[0];
        $imgHeight = $imgInfo[1];
        $imgType = $imgInfo[2];
        $srcImg = null;
        switch ($imgType) {
            case IMAGETYPE_GIF:
                $srcImg = imagecreatefromgif($imgFile);
                break;
            case IMAGETYPE_JPEG:
                $srcImg = imagecreatefromjpeg($imgFile);
                break;
            case IMAGETYPE_PNG:
                $srcImg = imagecreatefrompng($imgFile);
                break;
            case IMAGETYPE_WBMP:
                $srcImg = imagecreatefromwbmp($imgFile);
                break;
            default:
                break;
        }
        $smallImg = imagecreatetruecolor($imgWidth, $imgHeight);
        imagecopyresampled($smallImg, $srcImg, 0, 0, 0, 0, $imgWidth, $imgHeight, $imgWidth, $imgHeight);
        if(empty($file_folder1)){
            $file_folder1 = $file_folder;
        } 
        $smallFileName = $file_folder1 . $imgPathInfo['filename'] . "_small." . $imgPathInfo['extension'];
        
        imagejpeg($smallImg, $smallFileName, $quality);
        imagedestroy($srcImg);
        imagedestroy($smallImg);
        $result['status'] = 0;
        $result['message'] = "       ";
        $result['src_file'] = $file_name;
        $result['small_file'] = $imgPathInfo['filename'] . "_small." . $imgPathInfo['extension'];
        return $result;
    }
    $result['status'] = 500;
    $result['message'] = "         ";
    $result['src_file_name'] = $imgFile;
    return $result;
}