JSFiddle - React, Tailwind, and code Playground

by velo_ninja

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Resize and Crop Tool</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

<div class="container">
    <h2>Image Resize and Crop Tool</h2>
    
    <!-- File input for image -->
    <input type="file" id="fileInput" accept="image/*">
    <br>

    <!-- Quality and resolution options -->
    <label for="qualitySlider">Quality: </label>
    <input type="range" id="qualitySlider" min="50" max="100" value="100">
    <span id="qualityValue">100%</span>
    <br>
    
    <label for="resolutionSelect">Resolution: </label>
    <select id="resolutionSelect">
        <option value="128x128">128x128</option>
        <option value="256x256">256x256</option>
        <option value="512x512">512x512</option>
        <option value="800x640">800x640</option>
        <option value="1024x768">1024x768</option>
        <option value="2048x1536">2048x1536</option>
        <option value="3000x2000">3000x2000</option>
        <option value="4000x3000">4000x3000</option>
    </select>

    <!-- Crop and process buttons -->
    <button id="cropButton">Enable/Disable Crop</button>
    
    <!-- Canvas for image preview -->
    <canvas id="canvas" width="600" height="400"></canvas>
    <br>
    
    <img id="preview" alt="Preview" />
    <br>
    
    <a id="downloadLink" download="compressed-image.jpg">Download Cropped Image</a>
    
    <div id="compressionInfo"></div>
</div>

<script src="script.js"></script>
</body>
</html>

CSS

body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f9;
    margin: 0;
    padding: 0;
}

.container {
    width: 80%;
    max-width: 1000px;
    margin: 50px auto;
    text-align: center;
    padding: 20px;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

canvas {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 600px;
    border: 1px solid #ddd;
    position: relative;
    cursor: crosshair;
}

img#preview {
    max-width: 100%;
    max-height: 600px;
    width: auto;
    height: auto;
    margin-top: 20px;
}

button, input[type="file"], select, input[type="range"] {
    margin: 10px;
}

#compressionInfo {
    margin-top: 10px;
    font-size: 14px;
    color: #555;
}

/* Crop overlay */
.overlay {
    position: absolute;
    background-color: rgba(0, 0, 0, 0.3);
    pointer-events: none;
}

.crop-selection {
    position: absolute;
    border: 2px solid yellow;
    background-color: rgba(255, 255, 0, 0.3);
    pointer-events: none;
}

JavaScript

let selectedFile = null;
let imgElement = new Image();
let canvas = document.getElementById('canvas');
let canvasCtx = canvas.getContext('2d');
let cropStartX, cropStartY, cropWidth, cropHeight;
let croppingArea = null;
let isCropping = false; // Track if cropping is enabled
let qualitySlider = document.getElementById('qualitySlider');
let qualityValue = document.getElementById('qualityValue');
let resolutionSelect = document.getElementById('resolutionSelect');
let previewImg = document.getElementById('preview');
let downloadLink = document.getElementById('downloadLink');
let compressionInfo = document.getElementById('compressionInfo');
let cropButton = document.getElementById('cropButton');

// Update quality value display
qualitySlider.addEventListener('input', () => {
    qualityValue.textContent = `${qualitySlider.value}%`;
    if (selectedFile) {
        processImage();
    }
});

// Change resolution based on select input
resolutionSelect.addEventListener('change', () => {
    if (selectedFile) {
        processImage();
    }
});

// File input event
document.getElementById('fileInput').addEventListener('change', function(event) {
    selectedFile = event.target.files[0];
    if (selectedFile) {
        processImage();
    }
});

// Crop button click event
cropButton.addEventListener('click', () => {
    isCropping = !isCropping;  // Toggle cropping mode
    if (isCropping) {
        enableCropSelection();
    } else {
        resetCrop();
    }
});

function processImage() {
    const quality = qualitySlider.value / 100;
    const [maxWidth, maxHeight] = resolutionSelect.value.split('x').map(Number);

    // Load and draw image on canvas
    const reader = new FileReader();
    reader.onload = (e) => {
        imgElement.onload = () => {
            canvas.width = imgElement.width;
            canvas.height = imgElement.height;
            canvasCtx.clearRect(0, 0, canvas.width, canvas.height);
            canvasCtx.drawImage(imgElement, 0, 0);
           ...