스마트에디터 + 이미지호스팅에 업로드 > 그누보드5 팁자료실

그누보드5 팁자료실

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

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

본문

프로젝트 작업중 고용량의 이미지를 업로드하여 트래픽이 터지는 업체가 있어서 이미지호스팅을 적용해보았습니다. 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;
    }

추천
6

댓글 4개

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

회원로그인

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