라공에디션

썰타래 게시판 마개조 기록(4) : 첨부 이미지 wepb 변환 로직 추가

<span class="sv_member">니루</span>
니루 @tabom0w0
2026-05-22 00:09

첨부 이미지 wepb 변환 로직 추가함

이걸로 이미지 직접 첨부해도 용량 부담이 덜어지게 된다!


1. write_update.skin.php 최하단에 로직 추가

본문 작성 시 첨부한 이미지에 적용됨

// 일반 첨부파일 전용 WebP 변환 및 DB 레코드 동기화
$sql = "SELECT bf_no, bf_file, bf_source FROM {$g5['board_file_table']}
        WHERE bo_table = '{$bo_table}' AND wr_id = '{$wr_id}'
        ORDER BY bf_no";
$result = sql_query($sql);

while ($row = sql_fetch_array($result)) {
    $file_path = G5_DATA_PATH . "/file/{$bo_table}/" . $row['bf_file'];

    if (file_exists($file_path)) {
        $image_info = @getimagesize($file_path);
        if ($image_info !== false) {
            if (function_exists('auto_resize_and_convert_to_webp')) {
                $convert_result = auto_resize_and_convert_to_webp($file_path);

                if (isset($convert_result['success']) && $convert_result['success'] && $convert_result['new_path'] != $file_path) {
                    $new_filename = basename($convert_result['new_path']);

                    sql_query("UPDATE {$g5['board_file_table']}
                              SET bf_file = '{$new_filename}'
                              WHERE bo_table = '{$bo_table}'
                              AND wr_id = '{$wr_id}'
                              AND bf_no = '{$row['bf_no']}'");

                    if (file_exists($file_path)) {
                        @unlink($file_path);
                    }
                }
            }
        }
    }
}


2. write_update.tarae.skin.php 에도 변환 로직 추가

타래 잇기 시 이미지 첨부할 때 적용됨

if (!empty($update_fields)) {
    $update_sql = "UPDATE {$write_table} SET ".implode(', ', $update_fields)." WHERE wr_id = '{$wr_id}'";
    sql_query($update_sql, false);
}

이 코드와

// 새 메모 작성 시 부모 글 ID를 유지 (wr_parent가 자기 자신으로 덮이는 문제 보정)
if ($w == '' && isset($_POST['wr_parent']) && (int)$_POST['wr_parent'] > 0) {
    $parent_wr_id = (int)$_POST['wr_parent'];
    sql_query("UPDATE {$write_table} SET wr_parent = '{$parent_wr_id}' WHERE wr_id = '{$wr_id}'", false);
}

이 코드의 사이에

//타래 전용 배열(wr_11~wr_26) WebP 변환 및 물리 파일/컬럼 동기화
if (function_exists('auto_resize_and_convert_to_webp')) {
    // 업데이트된 데이터를 다시 정확하게 한 번 조회합니다.
    $tarae_row = sql_fetch("SELECT * FROM {$write_table} WHERE wr_id = '{$wr_id}'");
    if ($tarae_row) {
        for ($i = 1; $i <= $max_image_count; $i++) {
            $wr_field = 'wr_' . ($tarae_image_start_index - 1 + $i);
            $img_url = isset($tarae_row[$wr_field]) ? $tarae_row[$wr_field] : '';

            // 우리 서버에 저장된 로컬 파일인 경우에만 골라내어 변환 처리 진행
            if (!empty($img_url) && strpos($img_url, G5_DATA_URL.'/file/'.$bo_table) === 0) {
                $file_path = str_replace(G5_DATA_URL, G5_DATA_PATH, $img_url);

                if (file_exists($file_path)) {
                    $image_info = @getimagesize($file_path);
                    if ($image_info !== false) {
                        $convert_result = auto_resize_and_convert_to_webp($file_path);

                        if (isset($convert_result['success']) && $convert_result['success'] && $convert_result['new_path'] != $file_path) {
                            // 새로운 WebP 경로를 기반으로 웹 URL 재주소화
                            $new_url = str_replace(G5_DATA_PATH, G5_DATA_URL, $convert_result['new_path']);
                            
                            // 게시글 여분필드 데이터 주소 변경 업데이트
                            sql_query("UPDATE {$write_table} SET {$wr_field} = '".sql_real_escape_string($new_url)."' WHERE wr_id = '{$wr_id}'", false);

                            // 변환 전 기존 구 확장자 원본 파일 엄격 제거
                            if (file_exists($file_path)) {
                                @unlink($file_path);
                            }
                        }
                    }
                }
            }
        }
    }
}

요 webp 변환 코드를 넣는다


7mb짜리 월페이퍼 png 이미지를 첨부했을 때

기존에는 1.38mb png 이미지로 업로드 되었고

변환 로직 추가 후에는 353kb webp 이미지로 변환되어 저장된 것을 확인할 수 있었다

본래도 이미지는 좀 줄어들긴 했지만 막 첨부하기엔 역시 좀 부담스러우니까... 이제 직접 첨부해도 부담이 덜한 것이다!

댓글목록

니루

댓글

니루 @tabom0w0
webp 변환 코드는 라공님의 타임라인 스킨에서 참고했습니다