유용한 도구

이미지 압축기 (용량 줄이기)

작성자 정보

  • 차트분석 작성
  • 작성일

컨텐츠 정보

본문

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>이미지 압축기</title>

    <!-- Bootstrap 5 -->
    <link href="bootstrap@5.3.0/dist/css/bootstrap.min.css"" TARGET="_blank" rel="nofollow">https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"" TARGET="_blank" rel="nofollow">https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <style>
        body {
            background-color: #f8f9fa;
            padding: 30px;
        }
        .container {
            max-width: 600px;
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        .preview {
            max-width: 100%;
            margin-top: 10px;
            border-radius: 5px;
        }
        .loader {
            display: none;
            margin: 20px auto;
        }
    </style>
</head>
<body>

    <div class="container">
        <h2 class="mb-3">이미지 압축기</h2>
        <p class="text-muted">이미지를 업로드하면 압축하여 용량을 줄입니다.</p>

        <input type="file" id="imageInput" class="form-control mb-3" accept="image/*">
       
        <!-- 압축 품질 조절 -->
        <label for="qualityRange" class="form-label">압축 품질: <span id="qualityValue">80</span>%</label>
        <input type="range" class="form-range" min="10" max="100" value="80" id="qualityRange" oninput="updateQualityValue()">

        <button class="btn btn-primary w-100 mt-2" onclick="compressImage()"> 이미지 압축</button>

        <!-- 로딩 인디케이터 -->
        <div class="loader mt-3">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">압축 중...</span>
            </div>
            <p class="mt-2">이미지 압축 중...</p>
        </div>

        <!-- 미리보기 -->
        <h5 class="mt-3"> 압축된 이미지 미리보기</h5>
        <img id="previewImage" class="preview" style="display: none;">

        <button id="downloadBtn" class="btn btn-success w-100 mt-3" style="display: none;" onclick="downloadCompressedImage()"> 다운로드</button>
    </div>

    <script>
        let compressedBlob;

        function updateQualityValue() {
            document.getElementById("qualityValue").innerText = document.getElementById("qualityRange").value;
        }

        function compressImage() {
            const fileInput = document.getElementById("imageInput");
            const previewImage = document.getElementById("previewImage");
            const loader = document.querySelector(".loader");
            const downloadBtn = document.getElementById("downloadBtn");

            if (fileInput.files.length === 0) {
                alert("이미지를 업로드하세요.");
                return;
            }

            const file = fileInput.files[0];
            const reader = new FileReader();

            loader.style.display = "block";
            previewImage.style.display = "none";
            downloadBtn.style.display = "none";

            reader.onload = function (event) {
                const img = new Image();
                img.src = event.target.result;

                img.onload = function () {
                    const canvas = document.createElement("canvas");
                    const ctx = canvas.getContext("2d");

                    // 원본 크기 유지
                    canvas.width = img.width;
                    canvas.height = img.height;

                    // 캔버스에 이미지 그리기
                    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);

                    // 압축 품질 설정
                    const quality = document.getElementById("qualityRange").value / 100;

                    // 이미지 압축 및 Blob 생성
                    canvas.toBlob((blob) => {
                        compressedBlob = blob;
                        const compressedURL = URL.createObjectURL(blob);

                        // 미리보기 표시
                        previewImage.src = compressedURL;
                        previewImage.style.display = "block";

                        // 다운로드 버튼 활성화
                        downloadBtn.style.display = "block";

                        loader.style.display = "none";
                    }, "image/jpeg", quality);
                };
            };

            reader.readAsDataURL(file);
        }

        function downloadCompressedImage() {
            if (!compressedBlob) {
                alert("압축된 이미지가 없습니다.");
                return;
            }

            const a = document.createElement("a");
            a.href = URL.createObjectURL(compressedBlob);
            a.download = "compressed_image.jpg";
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        }
    </script>

</body>
</html>
이미지 압축기

이미지 압축기

이미지를 업로드하면 압축하여 용량을 줄입니다.

압축 중...

이미지 압축 중...

압축된 이미지 미리보기

관련자료

📊 크립토 공포지수 Latest Crypto Fear & Greed Index
알림 0