smarteditor2로 이미지 추가할 때 용량 줄이기 > 그누보드5 팁자료실

그누보드5 팁자료실

smarteditor2로 이미지 추가할 때 용량 줄이기 정보

smarteditor2로 이미지 추가할 때 용량 줄이기

본문

저용량 서버를 사용하시는 분에게 추천합니다.

 

smarteditor2에서 이미지 고용량 이미지 업로드 할때 서버에 무리가 갈수 있습니다.

또한 고용량 이미지 경우 유틸 프로그램 포함시킬수도 있습니다.

 

*** 에디터에서 이미지 축소하는 방법 

3732210772_1760277007.9824.png

editor/smarteditor2/photo_uploader/popup/php/UploadHandler.php

 

move_uploaded_file($uploaded_file, $file_path); 소스 아래 추가하시면 됩니다.


if (filesize($file_path) > 1024 * 1024) {
    $image_info = getimagesize($file_path);
    if ($image_info) {
        list($width, $height) = $image_info;
        $mime = $image_info['mime'];
        // 지원하는 mime 유형만 허용
        $allowed_mime_types = ['image/jpeg', 'image/png'];
        if (!in_array($mime, $allowed_mime_types)) {
            throw new Exception('Unsupported image format.');
        }
        // Exif 방향 처리 (JPEG만 해당)
        $orientation = 1;
        if ($mime === 'image/jpeg' && function_exists('exif_read_data')) {
            $exif = @exif_read_data($file_path);
            if (!empty($exif['Orientation'])) {
                $orientation = $exif['Orientation'];
            }
        }
        if ($width > 900) {
            $new_width = 900;
            $new_height = intval($height * ($new_width / $width));
            try {
                switch ($mime) {
                    case 'image/jpeg':
                        $src = imagecreatefromjpeg($file_path);
                        break;
                    case 'image/png':
                        $src = imagecreatefrompng($file_path);
                        break;
                    default:
                        throw new Exception('Unsupported image type');
                }
                // Exif 회전 보정
                switch ($orientation) {
                    case 3:
                        $src = imagerotate($src, 180, 0);
                        break;
                    case 6:
                        $src = imagerotate($src, -90, 0);
                        break;
                    case 8:
                        $src = imagerotate($src, 90, 0);
                        break;
                }
                $dst = imagecreatetruecolor($new_width, $new_height);
                // PNG 투명도 유지
                if ($mime === 'image/png') {
                    imagealphablending($dst, false);
                    imagesavealpha($dst, true);
                }
                imagecopyresampled(
                    $dst, $src,
                    0, 0, 0, 0,
                    $new_width, $new_height,
                    imagesx($src), imagesy($src)
                );
                switch ($mime) {
                    case 'image/jpeg':
                        imagejpeg($dst, $file_path, 85);
                        break;
                    case 'image/png':
                        imagepng($dst, $file_path);
                        break;
                }
                imagedestroy($src);
                imagedestroy($dst);
            } catch (Exception $e) {
                // 오류 로깅 또는 처리를 여기에 추가
                error_log($e->getMessage());
            }
        }
    }
}
추천
3

댓글 4개

전체 2,724 |RSS
그누보드5 팁자료실 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT