썰타래 게시판 마개조 기록(4) : 첨부 이미지 wepb 변환 로직 추가 (260624 수정, v1.8.0 대응)
첨부 이미지 wepb 변환 로직 추가함
이걸로 이미지 직접 첨부해도 용량 부담이 덜어지게 된다!
260624 수정 → v1.8.0 대응
구) v1.4.5 적용 시 → lib/board_common.lib.php에 있는 auto_resize_and_convert_to_webp($file_path) 함수 이용
현) v1.8.0 적용 → lib/board_common.lib.php에 있는 optimize_uploaded_image($file_path, $target_width) 함수 이용
이미지 리사이징 및 webp 변환을 담당하는 함수가 변경되어 그에 따라 수정 이루어짐
1. write_update.skin.php 최하단에 로직 추가
본문 작성 시 첨부한 이미지에 적용됨
v1.4.5 적용
// 일반 첨부파일 전용 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);
}
}
}
}
}
}v1.8.0 적용
// 일반 첨부파일 전용 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('optimize_uploaded_image')) {
$orig_w = $image_info[0]; // 원본 가로 크기
// 기본 목표 크기는 1200px
// 만약 원본이 1200px보다 크면 1200으로 리사이즈
// 원본이 1200px 이하로 작다면 원본에서 1px을 빼서 강제로 리사이즈 로직을 트리거함
$target_width = ($orig_w > 1200) ? 1200 : $orig_w - 1;
// 0 이하가 되지 않도록 방어 코드 추가
if ($target_width < 1) {
$target_width = 1;
}
$new_webp_path = optimize_uploaded_image($file_path, $target_width);
if ($new_webp_path && $new_webp_path != $file_path) {
$new_filename = basename($new_webp_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) && realpath($file_path) !== realpath($new_webp_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);
}이 코드의 사이에
v1.4.5 적용
//타래 전용 배열(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);
}
}
}
}
}
}
}
}v1.8.0 적용
//타래 전용 배열(wr_11~wr_26) WebP 변환 및 물리 파일/컬럼 동기화
if (function_exists('optimize_uploaded_image')) {
$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) {
$orig_w = $image_info[0];
// 마찬가지로 1200보다 작으면 1px을 빼서 강제 변환 프로세스 작동 유도
$target_width = ($orig_w > 1200) ? 1200 : $orig_w - 1;
if ($target_width < 1) {
$target_width = 1;
}
$new_webp_path = optimize_uploaded_image($file_path, $target_width);
if ($new_webp_path && $new_webp_path != $file_path) {
$new_url = str_replace(G5_DATA_PATH, G5_DATA_URL, $new_webp_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) && realpath($file_path) !== realpath($new_webp_path)) {
@unlink($file_path);
}
}
}
}
}
}
}
}이 webp 변환 코드를 넣는다
7mb짜리 월페이퍼 png 이미지를 첨부했을 때
기존에는 1.38mb png 이미지로 업로드 되었고
변환 로직 추가 후에는 353kb webp 이미지로 변환되어 저장된 것을 확인할 수 있었다
본래도 이미지는 좀 줄어들긴 했지만 막 첨부하기엔 역시 좀 부담스러우니까... 이제 직접 첨부해도 부담이 덜한 것이다!
- 이전글썰타래 게시판 마개조 기록(3) : css 조정 26.05.17
- 다음글썰타래 게시판 마개조 기록(5) : 최근 갱신일 추가 26.06.02
댓글목록
댓글
니루 @tabom0w0