// 웹닥터 - 업로드 이미지 자동 축소 (대용량 업로드 실패 방지 + 서버 디스크 용량 절감) // 모든 게시판 첨부파일 업로드가 공용으로 거치는 지점(bbs/write_update.php)에서 호출됨 function wd_auto_resize_image($tmp_file, &$filename) { if (!function_exists('getimagesize') || !function_exists('imagecreatetruecolor')) { return; } $max_dim = 1600; $max_bytes = 800 * 1024; // 아이폰 기본 사진 포맷(HEIC/HEIF)은 GD가 못 읽으므로 heif-convert로 JPEG 변환 후 이어서 처리 $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if (($ext == 'heic' || $ext == 'heif') && function_exists('shell_exec')) { $converted = $tmp_file.'_wdconv.jpg'; @shell_exec('heif-convert '.escapeshellarg($tmp_file).' '.escapeshellarg($converted).' 2>&1'); if (is_file($converted) && filesize($converted) > 0) { @copy($converted, $tmp_file); @unlink($converted); $filename = preg_replace('/\.(heic|heif)$/i', '.jpg', $filename); clearstatcache(true, $tmp_file); } else { @unlink($converted); return; } } $info = @getimagesize($tmp_file); if (!$info) { return; } $width = $info[0]; $height = $info[1]; $type = $info[2]; if (!$width || !$height) { return; } // 움짤이 깨지는 것을 막기 위해 GIF는 축소 대상에서 제외 if ($type == IMAGETYPE_GIF) { return; } // BMP는 무손실 비압축 포맷이라 웹에 부적합 - JPEG로 변환해서 저장 $bmp_to_jpeg = ($type == IMAGETYPE_BMP); // 스마트폰 사진의 EXIF Orientation(회전정보) 확인 - 회전 보정이 필요하면 크기가 작아도 파이프라인을 태워 픽셀을 바로 세운다 $orientation = 1; if ($type == IMAGETYPE_JPEG && function_exists('exif_read_data')) { $exif = @exif_read_data($tmp_file); if (!empty($exif['Orientation'])) { $orientation = (int)$exif['Orientation']; } } $needs_rotate = in_array($orientation, array(3, 6, 8)); if (!$bmp_to_jpeg && !$needs_rotate && $width <= $max_dim && $height <= $max_dim && @filesize($tmp_file) <= $max_bytes) { return; } $src = null; if ($type == IMAGETYPE_JPEG) { $src = @imagecreatefromjpeg($tmp_file); } else if ($type == IMAGETYPE_PNG) { $src = @imagecreatefrompng($tmp_file); } else if ($bmp_to_jpeg && function_exists('imagecreatefrombmp')) { $src = @imagecreatefrombmp($tmp_file); } else if (defined('IMAGETYPE_WEBP') && $type == IMAGETYPE_WEBP && function_exists('imagecreatefromwebp')) { $src = @imagecreatefromwebp($tmp_file); } else { return; } if (!$src) { return; } if ($needs_rotate && function_exists('imagerotate')) { if ($orientation == 3) { $rotated = imagerotate($src, 180, 0); } else if ($orientation == 6) { $rotated = imagerotate($src, -90, 0); } else { $rotated = imagerotate($src, 90, 0); } if ($rotated !== false) { imagedestroy($src); $src = $rotated; $width = imagesx($src); $height = imagesy($src); } } $ratio = min($max_dim / $width, $max_dim / $height, 1); $new_width = max(1, (int)round($width * $ratio)); $new_height = max(1, (int)round($height * $ratio)); $dst = imagecreatetruecolor($new_width, $new_height); if ($type == IMAGETYPE_PNG) { imagealphablending($dst, false); imagesavealpha($dst, true); $transparent = imagecolorallocatealpha($dst, 0, 0, 0, 127); imagefilledrectangle($dst, 0, 0, $new_width, $new_height, $transparent); } imagecopyresampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height); if ($type == IMAGETYPE_JPEG || $bmp_to_jpeg) { imageinterlace($dst, true); // 프로그레시브 JPEG - 웹에서 점진적 로딩 imagejpeg($dst, $tmp_file, 82); if ($bmp_to_jpeg) { $filename = preg_replace('/\.bmp$/i', '.jpg', $filename); } } else if ($type == IMAGETYPE_PNG) { imagepng($dst, $tmp_file, 8); } else if (defined('IMAGETYPE_WEBP') && $type == IMAGETYPE_WEBP && function_exists('imagewebp')) { imagewebp($dst, $tmp_file, 82); } imagedestroy($src); imagedestroy($dst); clearstatcache(true, $tmp_file); }
비밀글 기능으로 보호된 글입니다.
작성자와 관리자만 열람하실 수 있습니다.
본인이라면 비밀번호를 입력하세요.