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

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

 

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

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

 

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

3732210772_1760277007.9824.png

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

 

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

[code]

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());
            }
        }
    }
}

[/code]

|

댓글 4개

감사합니다 ^^

감사 합니다.

감사합니다

댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

+
제목 글쓴이 날짜 조회
4개월 전 조회 589
4개월 전 조회 619
4개월 전 조회 561
4개월 전 조회 824
4개월 전 조회 531
4개월 전 조회 544
4개월 전 조회 582
4개월 전 조회 440
4개월 전 조회 593
4개월 전 조회 580
4개월 전 조회 576
4개월 전 조회 539
4개월 전 조회 548
4개월 전 조회 436
4개월 전 조회 490
4개월 전 조회 441
4개월 전 조회 404
4개월 전 조회 584
4개월 전 조회 418
4개월 전 조회 485
4개월 전 조회 494
4개월 전 조회 622
4개월 전 조회 495
4개월 전 조회 498
5개월 전 조회 468
5개월 전 조회 678
5개월 전 조회 848
5개월 전 조회 941
5개월 전 조회 609
5개월 전 조회 763