$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";