smarteditor2로 이미지 추가할 때 용량 줄이기 정보
smarteditor2로 이미지 추가할 때 용량 줄이기
본문
저용량 서버를 사용하시는 분에게 추천합니다.
smarteditor2에서 이미지 고용량 이미지 업로드 할때 서버에 무리가 갈수 있습니다.
또한 고용량 이미지 경우 유틸 프로그램 포함시킬수도 있습니다.
*** 에디터에서 이미지 축소하는 방법

editor/smarteditor2/photo_uploader/popup/php/UploadHandler.php
move_uploaded_file($uploaded_file, $file_path); 소스 아래 추가하시면 됩니다.
if (filesize($file_path) > 1024 * 1024) {
$image_info = getimagesize($file_path);
if ($image_info) {
list($width, $height) = $image_info;
$mime = $image_info['mime'];
// 지원하는 mime 유형만 허용
$allowed_mime_types = ['image/jpeg', 'image/png'];
if (!in_array($mime, $allowed_mime_types)) {
throw new Exception('Unsupported image format.');
}
// Exif 방향 처리 (JPEG만 해당)
$orientation = 1;
if ($mime === 'image/jpeg' && function_exists('exif_read_data')) {
$exif = @exif_read_data($file_path);
if (!empty($exif['Orientation'])) {
$orientation = $exif['Orientation'];
}
}
if ($width > 900) {
$new_width = 900;
$new_height = intval($height * ($new_width / $width));
try {
switch ($mime) {
case 'image/jpeg':
$src = imagecreatefromjpeg($file_path);
break;
case 'image/png':
$src = imagecreatefrompng($file_path);
break;
default:
throw new Exception('Unsupported image type');
}
// Exif 회전 보정
switch ($orientation) {
case 3:
$src = imagerotate($src, 180, 0);
break;
case 6:
$src = imagerotate($src, -90, 0);
break;
case 8:
$src = imagerotate($src, 90, 0);
break;
}
$dst = imagecreatetruecolor($new_width, $new_height);
// PNG 투명도 유지
if ($mime === 'image/png') {
imagealphablending($dst, false);
imagesavealpha($dst, true);
}
imagecopyresampled(
$dst, $src,
0, 0, 0, 0,
$new_width, $new_height,
imagesx($src), imagesy($src)
);
switch ($mime) {
case 'image/jpeg':
imagejpeg($dst, $file_path, 85);
break;
case 'image/png':
imagepng($dst, $file_path);
break;
}
imagedestroy($src);
imagedestroy($dst);
} catch (Exception $e) {
// 오류 로깅 또는 처리를 여기에 추가
error_log($e->getMessage());
}
}
}
}
추천
3
3
댓글 4개

감사합니다 ^^

감사 합니다.

감사합니다

감사합니다.