강의 컨설팅 트레이닝 무료진단 무료책자 마케팅편지 마케팅정보공유 다이어리 서비스제휴 고객센터

파일업로드 썸네일 제작 class
작성자 : 13 김영철
등록날짜 : 2009.01.13 22:54
1,244

<?
class upload
{
    public $upload_tmpFileName = null;
    public $upload_fileName = null;
    public $upload_fileSize = null;
    public $upload_fileType = null;
    public $upload_fileWidth = null;
    public $upload_fileHeight = null;
    public $upload_fileExt = null;
    public $upload_fileImageType = null;
    public $upload_count = 0;
    public $upload_directory = null;
    public $upload_subdirectory = null;
    public $denied_ext = array
    (
        "php"    => ".phps",
        "phtml"    => ".phps",
        "html"    => ".txt",
        "htm"    => ".txt",
        "inc"    => ".txt",
        "sql"    => ".txt",
        "cgi"    => ".txt",
        "pl"    => ".txt",
        "jsp"    => ".txt",
        "asp"    => ".txt",
        "phtm"    => ".txt"
    );

    public function __construct($upload_dir)
    {
        $this->upload_directory = $upload_dir;
        $this->upload_subdirectory = time();
    }

    public function define($files)
    {
        if(is_array($files['tmp_name']))
        {
            for($i = 0; $i < sizeof($files['tmp_name']); $i++)
            {
                $this->_uploadErrorChecks($files['error'][$i], $files['name'][$i]);

                if(is_uploaded_file($files['tmp_name'][$i]))
                {
                    $this->upload_tmpFileName[$i] = $files['tmp_name'][$i];
                    $this->upload_fileName[$i] = $this->_emptyToUnderline($this->_checkFileName($files['name'][$i]));
                    $this->upload_fileSize[$i] = (int) $files['size'][$i] ? $files['size'][$i] : 0;
                    $this->upload_fileType[$i] = $files['type'][$i];
                    $this->upload_fileExt[$i] = $this->_getExtension($files['name'][$i]);

                    // 이미지 파일
                    if($this->_isThisImageFile($files['type'][$i]) == true)
                    {
                        $img = $this->_getImageSize($files['tmp_name'][$i]);
                        $this->upload_fileWidth[$i] = (int) $img[0];
                        $this->upload_fileHeight[$i] = (int) $img[1];
                        $this->upload_fileImageType[$i] = $img[2];
                    }
                }
            }
        }
        else
        {
            $this->_uploadErrorChecks($files['error'], $files['name']);

            if(is_uploaded_file($files['tmp_name']))
            {
                $this->upload_tmpFileName = $files['tmp_name'];
                $this->upload_fileName = $this->_emptyToUnderline($this->_checkFileName($files['name']));
                $this->upload_fileSize = (int) ($files['size']) ? $files['size'] : 0;
                $this->upload_fileType = $files['type'];
                $this->upload_fileExt = $this->_getExtension($files['name']);

                // 이미지 파일
                if($this->_isThisImageFile($files['type']) == true)
                {
                    $img = $this->_getImageSize($files['tmp_name']);
                    $this->upload_fileWidth = $img[0];
                    $this->upload_fileHeight = $img[1];
                    $this->upload_fileImageType = $img[2];
                }
            }
        }
    }

    private function _isThisImageFile($type)
    {
        if(preg_match("/image/i", $type) == false && preg_match("/flash/", $type) == false)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    private function _uploadErrorChecks($errorCode, $fileName)
    {
        if($errorCode == UPLOAD_ERR_INI_SIZE)
        {
            throw new Exception($fileName . " : 업로드 제한용량(".ini_get('upload_max_filesize').")을 초과한 파일입니다.");
        }
        else if($errorCode == UPLOAD_ERR_FORM_SIZE)
        {
            throw new Exception($fileName . " : 업로드한 파일이 HTML 에서 정의되어진 파일 업로드 제한용량을 초과하였습니다.");
        }
        else if($errorCode == UPLOAD_ERR_PARTIAL)
        {
            throw new Exception("파일이 일부분만 전송되었습니다. ");
        }
    }

    private function _mkUploadDir()
    {
        if(is_dir($this->upload_directory) == false)
        {
            if(@mkdir($this->upload_directory, 0755) == false)
            {
                throw new Exception($this->upload_directory . " 디렉토리를 생성하지 못하였습니다. 퍼미션을 확인하시기 바랍니다.");
            }
        }
    }

    private function _mkUploadSubDir()
    {
        if(is_writable($this->upload_directory) == false)
        {
            throw new Exception($this->upload_directory . " 에 쓰기 권한이 없습니다. " . $this->upload_subdirectory . "디렉토리를 생성하지 못하였습니다.");
        }
        else
        {
            $uploaded_path = $this->upload_directory . "/" . $this->upload_subdirectory;

            if(is_dir($uploaded_path) == false)
            {
                if(@mkdir($uploaded_path, 0755) == false)
                {
                    throw new Exception($uploaded_path . " 디렉토리를 생성하지 못하였습니다. 퍼미션을 확인하시기 바랍니다.");
                }
            }
            else
            {
                if(is_writable($uploaded_path) == false)
                {
                    throw new Exception($uploaded_path . " 디렉토리에 쓰기 권한이 없습니다. 파일을 업로드 할 수 없습니다.");
                }
            }
        }
    }

    public function uploadedFiles()
    {
        if(is_array($this->upload_tmpFileName))
        {
            $this->_mkUploadDir();
            $this->_mkUploadSubDir();

            for($i = 0; $i < sizeof($this->upload_tmpFileName); $i++)
            {
                $uploaded_filename = $this->upload_directory . "/" . $this->upload_subdirectory . "/" . $this->upload_fileName[$i];

                if(@move_uploaded_file($this->upload_tmpFileName[$i], $uploaded_filename) == false)
                {
                    throw new Exception($uploaded_filename . " 을 저장하지 못하였습니다.");
                }
                else
                {
                    $this->upload_count += 1;
                }

                @unlink($this->upload_tmpFileName[$i]);
            }
        }
        else
        {
            if(is_uploaded_file($this->upload_tmpFileName))
            {
                $this->_mkUploadDir();
                $this->_mkUploadSubDir();

                $uploaded_filename = $this->upload_directory . "/" . $this->upload_subdirectory . "/" . $this->upload_fileName;

                if(@move_uploaded_file($this->upload_tmpFileName, $uploaded_filename) == false)
                {
                    throw new Exception($uploaded_filename . " 을 저장하지 못하였습니다.");
                }
                else
                {
                    $this->upload_count += 1;
                }
                @unlink($this->upload_tmpFileName);
            }
        }
    }

    // 이미지정보
    private function _getImageSize($tmp_file)
    {
        $img = @getimagesize($tmp_file);

        $img[0] = $img[0] ? $img[0] : 0;
        $img[1] = $img[1] ? $img[1] : 0;

        return $img;
    }

    // 금지 확장자명을 허용 확장자로 변경하여 파일명 지정
    private function _checkFileName($fileName)
    {
        $fileName = strtolower($fileName);

        foreach($this->denied_ext as $key => $value)
        {
            if($this->_getExtension($fileName) == trim($key))
            {
                $expFileName = explode(".", $fileName);

                for($i = 0; $i < sizeof($expFileName) - 1; $i++)
                {
                    $fname .= $expFileName[$i] . $value;
                }
                return $fname;
            }
        }
        return $fileName;
    }

    /**
     * 파일명의 빈 부분을 "_" 로 변경
     */
    private function _emptyToUnderline($fileName)
    {
        return preg_replace("/\ /i", "_", $fileName);
    }

    /**
     * 확장자 추출
     */
    private function _getExtension($fileName)
    {
        //return strtolower(substr(strrchr($fileName, "."), 1));
        $path = pathinfo($fileName);
        return $path['extension'];
    }

    /**
     * 이미지 파일이 아닌 파일이 존재한다면 에러
     */
    public function checkImageOnly()
    {
        if(is_array($this->upload_tmpFileName))
        {
            for($i = 0; $i < sizeof($this->upload_tmpFileName); $i++)
            {
                if($this->_isThisImageFile($this->upload_fileType[$i]) == false)
                {
                    throw new Exception($this->upload_fileName[$i] . " 은 이미지 파일이 아닙니다.");
                }
            }
        }
        else
        {
            if(is_uploaded_file($this->upload_tmpFileName))
            {
                if($this->_isThisImageFile($this->upload_fileType) == false)
                {
                    throw new Exception($this->upload_fileName . " 은 이미지 파일이 아닙니다.");
                }
            }
        }
    }

    /**
     * gd library information - array
     * --------------------------------------------------------------------------------
     * GD Version : string value describing the installed libgd version.
     * Freetype Support : boolean value. TRUE if Freetype Support is installed.
     * Freetype Linkage : string value describing the way in which Freetype was linked. Expected values are: 'with freetype',
                          'with TTF library', and 'with unknown library'. This element will only be defined if Freetype Support
                          evaluated to TRUE.
     * T1Lib Support : boolean value. TRUE if T1Lib support is included.
     * GIF Read Support : boolean value. TRUE if support for reading GIF images is included.
     * GIF Create Support : boolean value. TRUE if support for creating GIF images is included.
     * JPG Support : boolean value. TRUE if JPG support is included.
     * PNG Support : boolean value. TRUE if PNG support is included.
     * WBMP Support : boolean value. TRUE if WBMP support is included.
     * XBM Support : boolean value. TRUE if XBM support is included.
     * --------------------------------------------------------------------------------
     */
    public function makeThumbnailed($max_width, $max_height, $thumb_head = null)
    {
        if(extension_loaded("gd") == false)
        {
            throw new Exception("GD 라이브러리가 설치되어 있지 않습니다.");
        }
        else
        {
            $gd = @gd_info();

            if(substr_count(strtolower($gd['GD Version']), "2.") == 0)
            {
                $this->_thumbnailedOldGD($max_width, $max_height, $thumb_head);
            }
            else
            {
                $this->_thumbnailedNewGD($max_width, $max_height, $thumb_head);
            }
        }
    }

    /**
     * GD Library 1.X 버전대를 위한 섬네일 함수
     *
     * GIF 포맷을 지원하지 않음
     */
    private function _thumbnailedOldGD($max_width, $max_height, $thumb_head)
    {
        if(is_array($this->upload_tmpFileName))
        {
            $this->_mkUploadDir();
            $this->_mkUploadSubDir();

            for($i = 0; $i < sizeof($this->upload_tmpFileName); $i++)
            {
                if($this->_isThisImageFile($this->upload_fileType[$i]) == true)
                {
                    switch($this->upload_fileImageType[$i])
                    {
                        case 1 :
                            $im = @imagecreatefromgif($this->upload_tmpFileName[$i]);
                        break;
                        case 2 :
                            $im = @imagecreatefromjpeg($this->upload_tmpFileName[$i]);
                        break;
                        case 3 :
                            $im = @imagecreatefrompng($this->upload_tmpFileName[$i]);
                        break;
                    }

                    if(!$im)
                    {
                        throw new Exception("썸네일 이미지 생성 중 문제가 발생하였습니다.");
                    }
                    else
                    {
                        $sizemin = $this->_getWidthHeight($this->upload_fileWidth[$i], $this->upload_fileHeight[$i], $max_width, $max_height);

                        $small = @imagecreate($sizemin[width], $sizemin[height]);

                        @imagecolorallocate($small, 255, 255, 255);
                        @imagecopyresized($small, $im, 0, 0, 0, 0, $sizemin[width], $sizemin[height], $this->upload_fileWidth[$i], $this->upload_fileHeight[$i]);

                        $thumb_head = ($thumb_head != null) ? $thumb_head : "thumb";
                        $thumb_filename = $this->upload_directory . "/" . $this->upload_subdirectory . "/" . $this->upload_fileName[$i] . ".${thumb_head}";

                        if ($this->upload_fileImageType[$i] == 2)
                        {
                            if(@imagejpeg($small, $thumb_filename, 100) == false)
                            {
                                throw new Exception("jpg/jpeg 썸네일 이미지를 생성하지 못하였습니다.");
                            }
                        }
                        else if ($this->upload_fileImageType[$i] == 3)
                        {
                            if(@imagepng($small, $thumb_filename) == false)
                            {
                                throw new Exception("png 썸네일 이미지를 생성하지 못하였습니다.");
                            }
                        }

                        if($small != null)
                        {
                            @imagedestroy($small);
                        }
                        if($im != null)
                        {
                            @imagedestroy($im);
                        }
                    }
                }
            }
        }
        else
        {
            if(is_uploaded_file($this->upload_tmpFileName))
            {
                $this->_mkUploadDir();
                $this->_mkUploadSubDir();

                if($this->_isThisImageFile($this->upload_fileType) == true)
                {
                    switch($this->upload_fileImageType)
                    {
                        case 1 :
                            $im = @imagecreatefromgif($this->upload_tmpFileName);
                        break;
                        case 2 :
                            $im = @imagecreatefromjpeg($this->upload_tmpFileName);
                        break;
                        case 3 :
                            $im = @imagecreatefrompng($this->upload_tmpFileName);
                        break;
                    }

                    if(!$im)
                    {
                        throw new Exception("썸네일 이미지 생성 중 문제가 발생하였습니다.");
                    }
                    else
                    {
                        $sizemin = $this->_getWidthHeight($this->upload_fileWidth, $this->upload_fileHeight, $max_width, $max_height);

                        $small = @imagecreate($sizemin[width], $sizemin[height]);

                        @imagecolorallocate($small, 255, 255, 255);
                        @imagecopyresized($small, $im, 0, 0, 0, 0, $sizemin[width], $sizemin[height], $this->upload_fileWidth, $this->upload_fileHeight);

                        $thumb_head = ($thumb_head != null) ? $thumb_head : "thumb";
                        $thumb_filename = $this->upload_directory . "/" . $this->upload_subdirectory . "/" . $this->upload_fileName . ".${thumb_head}";

                        if ($this->upload_fileImageType == 2)
                        {
                            if(@imagejpeg($small, $thumb_filename, 100) == false)
                            {
                                throw new Exception("jpg/jpeg 썸네일 이미지를 생성하지 못하였습니다.");
                            }
                        }
                        else if ($this->upload_fileImageType == 3)
                        {
                            if(@imagepng($small, $thumb_filename) == false)
                            {
                                throw new Exception("png 썸네일 이미지를 생성하지 못하였습니다.");
                            }
                        }

                        if($small != null)
                        {
                            @imagedestroy($small);
                        }
                        if($im != null)
                        {
                            @imagedestroy($im);
                        }
                    }
                }
            }
        }
    }

    /**
     * GD Library 2.X 버전대를 위한 섬네일 함수
     *
     * GIF 포맷을 지원함
     */
    private function _thumbnailedNewGD($max_width, $max_height,  $thumb_head)
    {
        if(is_array($this->upload_tmpFileName))
        {
            $this->_mkUploadDir();
            $this->_mkUploadSubDir();

            for($i = 0; $i < sizeof($this->upload_tmpFileName); $i++)
            {
                if($this->_isThisImageFile($this->upload_fileType[$i]) == true)
                {
                    switch($this->upload_fileImageType[$i])
                    {
                        case 1 :
                            $im = @imagecreatefromgif($this->upload_tmpFileName[$i]);
                        break;
                        case 2 :
                            $im = @imagecreatefromjpeg($this->upload_tmpFileName[$i]);
                        break;
                        case 3 :
                            $im = @imagecreatefrompng($this->upload_tmpFileName[$i]);
                        break;
                    }

                    $sizemin = $this->_getWidthHeight($this->upload_fileWidth[$i], $this->upload_fileHeight[$i], $max_width, $max_height);

                    $small = @imagecreatetruecolor($sizemin[width], $sizemin[height]);

                    @imagecolorallocate($small, 255, 255, 255);
                    @imagecopyresampled($small, $im, 0, 0, 0, 0, $sizemin[width], $sizemin[height], $this->upload_fileWidth[$i], $this->upload_fileHeight[$i]);

                    $thumb_head = ($thumb_head != null) ? $thumb_head : "thumb";
                    $thumb_filename = $this->upload_directory . "/" . $this->upload_subdirectory . "/" . $this->upload_fileName[$i] . ".${thumb_head}";

                    if($this->upload_fileImageType[$i] == 1)
                    {
                        if(@imagegif($small, $thumb_filename) == false)
                        {
                            throw new Exception("gif 썸네일 이미지를 생성하지 못하였습니다.");
                        }
                    }
                    else if ($this->upload_fileImageType[$i] == 2)
                    {
                        if(@imagejpeg($small, $thumb_filename, 100) == false)
                        {
                            throw new Exception("jpg/jpeg 썸네일 이미지를 생성하지 못하였습니다.");
                        }
                    }
                    else if ($this->upload_fileImageType[$i] == 3)
                    {
                        if(imagepng($small, $thumb_filename) == false)
                        {
                            throw new Exception("png 썸네일 이미지를 생성하지 못하였습니다.");
                        }
                    }

                    if($small != null)
                    {
                        @imagedestroy($small);
                    }
                    if($im != null)
                    {
                        @imagedestroy($im);
                    }
                }
            }
        }
        else
        {
            if(is_uploaded_file($this->upload_tmpFileName))
            {
                $this->_mkUploadDir();
                $this->_mkUploadSubDir();

                if($this->_isThisImageFile($this->upload_fileType) == true)
                {
                    switch($this->upload_fileImageType)
                    {
                        case 1 :
                            $im = @imagecreatefromgif($this->upload_tmpFileName);
                        break;
                        case 2 :
                            $im = @imagecreatefromjpeg($this->upload_tmpFileName);
                        break;
                        case 3 :
                            $im = @imagecreatefrompng($this->upload_tmpFileName);
                        break;
                    }

                    $sizemin = $this->_getWidthHeight($this->upload_fileWidth, $this->upload_fileHeight, $max_width, $max_height);

                    $small = @imagecreatetruecolor($sizemin[width], $sizemin[height]);

                    @imagecolorallocate($small, 255, 255, 255);
                    @imagecopyresampled($small, $im, 0, 0, 0, 0, $sizemin[width], $sizemin[height], $this->upload_fileWidth, $this->upload_fileHeight);

                    $thumb_head = ($thumb_head != null) ? $thumb_head : "thumb";
                    $thumb_filename = $this->upload_directory . "/" . $this->upload_subdirectory . "/" . $this->upload_fileName . ".${thumb_head}";

                    if($this->upload_fileImageType == 1)
                    {
                        if(@imagegif($small, $thumb_filename) == false)
                        {
                            throw new Exception("gif 썸네일 이미지를 생성하지 못하였습니다.");
                        }
                    }
                    else if ($this->upload_fileImageType == 2)
                    {
                        if(@imagejpeg($small, $thumb_filename, 100) == false)
                        {
                            throw new Exception("jpg/jpeg 썸네일 이미지를 생성하지 못하였습니다.");
                        }
                    }
                    else if ($this->upload_fileImageType == 3)
                    {
                        if(imagepng($small, $thumb_filename) == false)
                        {
                            throw new Exception("png 썸네일 이미지를 생성하지 못하였습니다.");
                        }
                    }

                    if($small != null)
                    {
                        @imagedestroy($small);
                    }
                    if($im != null)
                    {
                        @imagedestroy($im);
                    }
                }
            }
        }
    }

    /**
     * 제한된 가로/세로 길이에 맞추어 원본이미지의 줄인 windth, height 값을 리턴
     * _getWidthHeight(원본이미지가로, 원본이미지세로, 제한가로, 제한세로)
     */
    private function _getWidthHeight($org_width, $org_height, $max_width, $max_height)
    {
        $img = array();

       

"쇼핑몰·홈페이지·오픈마켓
블로그·페이스북·이메일 등의 각종 마케팅 글쓰기,
각종 광고, 영업, 판매, 제안서, 전단지
반응율 3배×10배 이상 높이는 마법의 8단계 공식"
자세히보기

Comments

번호 제목 글쓴이 날짜 조회
3285 URL Cache를 사용하여 웹을 더욱 빠르게 13 김영철 01.13 980
3284 php5 99 단국강토 01.02 1054
3283 php 세션css 99 단국강토 12.30 1066
3282 [hatelove님의 JBBS 알고리즘 강좌 9] 13 김영철 01.14 1069
3281 reset , foreach 13 김영철 01.13 1106
3280 png 99 단국강토 12.30 1120
3279 플래시로 만든 php 함수 사전 13 김영철 01.13 1162
3278 PHP강좌】PHP URL함수 13 김영철 01.13 1166
3277 foreach 와 배열 13 김영철 01.14 1172
3276 주화면의 최신글을 preload로 빠르게 13 김영철 01.13 1184
3275 [php] 내장함수 13 김영철 01.13 1189
3274 태그 허용 함수???? 이제 개념을 바꾸자 13 김영철 01.14 1206
3273 플래쉬 Panels 에 대한 기본개념들 99 단국강토 01.06 1212
3272 foreach 13 김영철 01.13 1213
3271 파일관련함수 13 김영철 01.13 1213
3270 [hatelove님의 JBBS 알고리즘 강좌 7] 13 김영철 01.14 1215
3269 PHP도 객체지향형 프로그램이다..!!(클래스,상속동...) 13 김영철 01.13 1216
3268 PHP입문 - 함수 13 김영철 01.13 1216
3267 플래시에서 pc cam 영상보여주기 99 단국강토 02.16 1218
3266 php기본함수 정리!! 13 김영철 01.13 1219
3265 시스템콜인 open 계열 함수와 스트림기반의 fopen 계열.. 13 김영철 01.13 1221
3264 소스를 간편하게 만들어 주는 with문 99 단국강토 02.10 1225
3263 객체 정의하기[이론,예제] 99 단국강토 01.29 1229
3262 php 파일 업, 다운로드 13 김영철 01.13 1232
3261 역인덱스 게시판 | 13 김영철 01.14 1241
열람중 파일업로드 썸네일 제작 class 13 김영철 01.13 1245
3259 초보자용 이것저것 몇가지 팁 13 김영철 01.14 1246
3258 간단 날짜계산 99 단국강토 02.16 1249
3257 window 객체 M 최고의하루 12.20 1249
3256 디렉토리 폴더 모든파일 표시 [php] 13 김영철 01.14 1250
마케팅
특별 마케팅자료
다운로드 마케팅자료
창업,경영
기획,카피,상품전략
동기부여,성취