테스트 사이트 - 개발 중인 베타 버전입니다

스마트에디터 + 이미지호스팅에 업로드

· 5개월 전 · 509 · 4

프로젝트 작업중 고용량의 이미지를 업로드하여 트래픽이 터지는 업체가 있어서 이미지호스팅을 적용해보았습니다. ChatGPT를 활용하여 스마트에이터에 이미지호스팅으로 연결되도록 설정해봤습니다.

 

필요하신분 있으시면 이용하세요.

 

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

 

약 1126번째줄 

 

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
        $file = new \stdClass();
        $file->oriname = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);

        $filename_ext = pathinfo($name, PATHINFO_EXTENSION);
        $file->name = $this->get_file_passname().'_'.str_replace(".", "_", $this->get_microtime()).".".$filename_ext;

        $file->size = $this->fix_integer_overflow(intval($size));
        $file->type = $type;

        if (SMARTEDITOR_UPLOAD_IMG_CHECK && !$this->reprocessImage($uploaded_file, null)) {
            $file->error = $this->get_error_message('accept_file_types');
            return $file;
        }

        if ($this->validate($uploaded_file, $file, $error, $index)) {
            $this->handle_form_data($file, $index);

            // === FTP 설정 ===
            $ftp_server = "#FTP주소#";
            $ftp_user_name = "#유저#";
            $ftp_user_pass = "#패스워드#";
            $ftp_base_path = "www/data/editor"; // 이미지호스팅에 맞춰서 설정해주세요.(/data/editor은 하단에서 자동 폴더생성됨)
            $ftp_full_path = $ftp_base_path . "/" . $file->name;

            // 로컬 파일 존재 확인
            if (!file_exists($uploaded_file)) {
                $file->error = "업로드 파일 없음";
                return $file;
            }

            // FTP 연결
            $conn_id = ftp_connect($ftp_server);
            if (!$conn_id || !ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
                $file->error = "FTP 연결 실패";
                return $file;
            }
            ftp_pasv($conn_id, true);

            // 디렉토리 생성
            $ftp_dir_parts = explode('/', $ftp_base_path);
            $cwd = '';
            foreach ($ftp_dir_parts as $part) {
                if ($part == '') continue;
                $cwd .= '/' . $part;
                if (!@ftp_chdir($conn_id, $cwd)) {
                    if (!ftp_mkdir($conn_id, $cwd)) {
                        ftp_close($conn_id);
                        $file->error = "FTP 디렉토리 생성 실패: $cwd";
                        return $file;
                    }
                }
            }
            ftp_chdir($conn_id, '/');

            // FTP 업로드
            if (!ftp_put($conn_id, $ftp_full_path, $uploaded_file, FTP_BINARY)) {
                ftp_close($conn_id);
                $file->error = "FTP 업로드 실패";
                return $file;
            }

            // 권한 설정 (Cafe24는 644 필요)
            if (function_exists('ftp_chmod')) {
                ftp_chmod($conn_id, 0644, $ftp_full_path);
            }

            // URL 지정 (Cafe24 CDN)
            $file->url = "#제공받는 CDN주소#/data/editor/" . $file->name;

            // === 원격 파일을 임시로 다운로드해서 이미지 정보 추출 ===
            $tmp_local_file = sys_get_temp_dir() . "/" . $file->name;
            if (ftp_get($conn_id, $tmp_local_file, $ftp_full_path, FTP_BINARY)) {
                // 사이즈 갱신
                $file_size = filesize($tmp_local_file);
                $file->size = $file_size;

                // 이미지 검사
                if ($this->is_valid_image_file($tmp_local_file)) {
                    $this->handle_image_file($tmp_local_file, $file);

                    if ($this->options['is_resize']) {
                        $resize_options = [
                            'max_width' => $this->options['resize_max_width'],
                            'max_height' => $this->options['resize_max_height'],
                            'jpeg_quality' => $this->options['resize_jpeg_compress'],
                            'auto_orient' => true,
                        ];
                        if ($this->create_scaled_image($file->name, '', $resize_options)) {
                            $file->size = filesize($tmp_local_file);
                        }
                    }

                    $image_info = getimagesize($tmp_local_file);
                    $file->width = $image_info[0];
                    $file->height = $image_info[1];
                    $file->type = $image_info['mime']; // image/png 등

                    if (function_exists('run_replace')) {
                        $file->url = run_replace('get_editor_upload_url', $file->url, $ftp_full_path, $file);
                    }

                    $this->files[] = $file->name;
                } else {
                    $file->error = $this->get_error_message('accept_file_types');
                }

                unlink($tmp_local_file);
            } else {
                $file->error = "FTP 원격 파일 다운로드 실패";
            }

            ftp_close($conn_id);

            $this->set_additional_file_properties($file);
        }

        return $file;
    }

댓글 작성

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

로그인하기

댓글 4개

5개월 전

감사합니다, 유용하게 사용할 수 있을듯 합니다

5개월 전
트래픽 걱정이 많이 줄어들겠습니다. 감사합니다 ^^
5개월 전

신박하군요
추천 꾹!하고 갑니다

감사합니다.

게시글 목록

번호 제목
24318
24317
24315
24309
24294
24293
24277
24262
24260
24253
24251
24236
24233
24228
24226
24221
24214
24203
24201
24199
24196
24195
24194
24192
24191
24187
24185
24183
24172
24168