Thumbnail Image (GD 처리)

관리자 · 2006-05-15 22:33:33
// 제가 만들어쓰는 엔진에서 gd 처리 부분만 따로 적어보겠습니다.
참고해서 쓰십시오.

// 호출시
put_gdimage('원본파일명', 생성가로, 생성세로, '저장될파일명');

// 정상생성여부 확인시 : 정상생성시 1을 리턴, 이외는 실패

// gd 이미지 생성
// $img_name = 생성에 사용될 원본 파일명, $width = 생성이미지 가로크기, $height = 생성이미지 세로크기, $save_name = 저장될 파일명
function put_gdimage($img_name, $width, $height, $save_name){

    $gd = gd_info();
    $gdver = substr(preg_replace("/[^0-9]/", "", $gd['GD Version']), 0, 1);
    if(!$gdver) return "GD 버젼체크 실패거나 GD 버젼이 1 미만입니다.";

    $srcname = "../upload/goods/".$img_name;
    $timg = getimagesize($srcname);
    if($timg[2] != 1 && $timg[2] != 2 && $timg[2] != 3) return "확장자가 jp(e)g/png/gif 가 아닙니다.";

    // jpg, jpeg
    if($timg[2] == 2){
        
        $cfile = imagecreatefromjpeg($srcname);
        // gd 버전별로
        if($gdver == 2){
            $dest = imagecreatetruecolor($width, $height);
            imagecopyresampled($dest, $cfile, 0, 0, 0, 0, $width, $height, $timg[0], $timg[1]);
        }else{
            $dest = imagecreate($width, $height);
            imagecopyresized($dest, $cfile, 0, 0, 0, 0, $width, $height, $timg[0], $timg[1]);
        }
        imagejpeg($dest, "../upload/goods/".$save_name, 90);
    
    // png
    }else if($timg[2] == 3){

        $cfile = imagecreatefrompng($srcname);
        if($gdver == 2){
            $dest = imagecreatetruecolor($width, $height);
            imagecopyresampled($dest, $cfile, 0, 0, 0, 0, $width, $height, $timg[0], $timg[1]);
        }else{
            $dest = imagecreate($width, $height);
            imagecopyresized($dest, $cfile, 0, 0, 0, 0, $width, $height, $timg[0], $timg[1]);
        }
        imagepng($dest, "../upload/goods/".$save_name, 90);

    // gif
    }else if($timg[2] == 1){

        $cfile = imagecreatefromgif($srcname);
        if($gdver == 2){
            $dest = imagecreatetruecolor($width, $height);
            imagecopyresampled($dest, $cfile, 0, 0, 0, 0, $width, $height, $timg[0], $timg[1]);
        }else{
            $dest = imagecreate($width, $height);
            imagecopyresized($dest, $cfile, 0, 0, 0, 0, $width, $height, $timg[0], $timg[1]);
        }    
        imagegif($dest, "../upload/goods/".$save_name, 90);
    }
    // 메모리에 있는 그림 삭제
    imagedestroy($dest);
    return 1;
}

* 출처 : phpschool.com - sjsjin 님