93 lines
2.2 KiB
PHP
93 lines
2.2 KiB
PHP
<?php
|
|
|
|
$baseDir = $_SERVER['DOCUMENT_ROOT'] . "/upload/customer_image/";
|
|
|
|
// 이미지 압축 + 리사이즈 + JPG 변환
|
|
function compress($src, $dest, $quality = 75) {
|
|
|
|
$info = getimagesize($src);
|
|
if (!$info) return false;
|
|
|
|
list($w, $h, $type) = $info;
|
|
|
|
switch ($type) {
|
|
case IMAGETYPE_JPEG:
|
|
$img = imagecreatefromjpeg($src);
|
|
break;
|
|
case IMAGETYPE_PNG:
|
|
$img = imagecreatefrompng($src);
|
|
break;
|
|
case IMAGETYPE_WEBP:
|
|
if (!function_exists('imagecreatefromwebp')) return false;
|
|
$img = imagecreatefromwebp($src);
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
if (!$img) return false;
|
|
|
|
$maxWidth = 1200;
|
|
|
|
if ($w > $maxWidth) {
|
|
$newW = $maxWidth;
|
|
$newH = intval(($h / $w) * $newW);
|
|
} else {
|
|
$newW = $w;
|
|
$newH = $h;
|
|
}
|
|
|
|
$tmp = imagecreatetruecolor($newW, $newH);
|
|
|
|
// PNG / WEBP 투명 처리
|
|
if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_WEBP) {
|
|
imagealphablending($tmp, false);
|
|
imagesavealpha($tmp, true);
|
|
$transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
|
|
imagefilledrectangle($tmp, 0, 0, $newW, $newH, $transparent);
|
|
}
|
|
|
|
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
|
|
|
|
$result = imagejpeg($tmp, $dest, $quality);
|
|
|
|
imagedestroy($img);
|
|
imagedestroy($tmp);
|
|
|
|
return $result;
|
|
}
|
|
|
|
// 전체 파일 순회
|
|
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir));
|
|
|
|
foreach ($rii as $file) {
|
|
if ($file->isDir()) continue;
|
|
|
|
$path = $file->getPathname();
|
|
|
|
// 너무 작은 파일 skip
|
|
if (filesize($path) < 500000) continue;
|
|
|
|
$info = pathinfo($path);
|
|
|
|
// 이미 jpg면 덮어쓰기 방식
|
|
if (strtolower($info['extension']) === 'jpg' || strtolower($info['extension']) === 'jpeg') {
|
|
echo "Compressing JPG: $path\n";
|
|
compress($path, $path);
|
|
continue;
|
|
}
|
|
|
|
// 확장자 변경
|
|
$newPath = $info['dirname'] . '/' . $info['filename'] . '.jpg';
|
|
|
|
echo "Converting: $path -> $newPath\n";
|
|
|
|
if (compress($path, $newPath)) {
|
|
// 원본 삭제
|
|
unlink($path);
|
|
} else {
|
|
echo "FAILED: $path\n";
|
|
}
|
|
}
|
|
|
|
echo "DONE\n"; |