Open In App

Image Crop Tool Card using Tailwind CSS & JavaScript

The Image Crop Tool is a web application designed to allow users to upload an image crop it to their desired dimensions and download the cropped image. It provides a user-friendly interface for selecting an image file adjusting the crop size and performing the cropping operation.

Approach to create Image Crop Tool Card :

Example: Implementation to design a image crop tool using tailwind CSS.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>The Image Crop Tool</title>
    <link href=
          rel="stylesheet">
    <link href=
          rel="stylesheet">
    <style>
        .container {
            font-size: 1.5rem;
            width: 80%;
            max-width: 800px;
        }
 
        .label {
            font-size: 1.3rem;
        }
    </style>
</head>
 
<body class="bg-gray-100 min-h-screen flex
             flex-col justify-center items-center">
    <div class="container max-w-md w-full p-8 bg-white
                rounded-lg shadow-lg border-2 border-green-400">
        <h1 class="text-3xl font-semibold text-center mb-8">
              Image Crop Tool
          </h1>
        <div class="mb-4">
            <label for="file-input" class="block text-sm label text-gray-700">
                  Select Image:
              </label>
            <input type="file" id="file-input"
                   class="w-full border border-gray-300 rounded-md
                          mt-3 py-2 px-3 focus:outline-none
                          focus:border-blue-500">
        </div>
        <div class="mb-4">
            <label for="preview" class="block text-sm label text-gray-700">
                  Preview:
              </label>
            <div id="preview" class="border border-gray-300
                                     rounded-md overflow-hidden mt-3"
                 style="width: 700px; height: 300px;">
              </div>
        </div>
        <div class="mb-4">
            <label for="crop-size" class="block text-sm label text-gray-700">
                  Crop Size:
              </label>
            <input type="range" id="crop-size" class="w-full"
                   min="50" max="300" value="200">
        </div>
        <div class="flex items-center justify-between mb-4">
            <button id="crop-btn"
                class="px-6 py-3 bg-blue-500 text-white rounded-md
                       hover:bg-blue-600 focus:outline-none">Crop Image
              </button>
            <button id="download-btn"
                    class="px-6 py-3 bg-green-500 text-white rounded-md
                           hover:bg-green-600 focus:outline-none">
                  Download
              </button>
            <button id="clear-btn"
                    class="px-6 py-3 bg-red-500 text-white rounded-md
                              hover:bg-red-600 focus:outline-none">
                  Clear
              </button>
        </div>
    </div>
    <script>
        const fileInput = document.getElementById('file-input');
        const preview = document.getElementById('preview');
        const cropSizeInput = document.getElementById('crop-size');
        const cropBtn = document.getElementById('crop-btn');
        const downloadBtn = document.getElementById('download-btn');
        const clearBtn = document.getElementById('clear-btn');
        let cropper = null;
        fileInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            const reader = new FileReader();
            reader.onload = (event) => {
                if (cropper) {
                    cropper.destroy();
                }
                preview.innerHTML = '';
                const img = document.createElement('img');
                img.src = event.target.result;
                img.id = 'image';
                img.style.maxWidth = '100%';
                preview.appendChild(img);
                cropper = new Cropper(img, {
                    aspectRatio: 1,
                    crop(event) {
                    }
                });
            };
            reader.readAsDataURL(file);
        });
        cropBtn.addEventListener('click', () => {
            if (cropper) {
                const canvas = cropper.getCroppedCanvas({
                    width: cropSizeInput.value,
                    height: cropSizeInput.value,
                });
                const croppedImage = document.createElement('img');
                croppedImage.src = canvas.toDataURL();
                preview.innerHTML = '';
                preview.appendChild(croppedImage);
            }
        });
        downloadBtn.addEventListener('click', () => {
            if (cropper) {
                cropper.getCroppedCanvas().toBlob((blob) => {
                    const url = URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = 'cropped-image.png';
                    document.body.appendChild(a);
                    a.click();
                    URL.revokeObjectURL(url);
                });
            }
        });
        clearBtn.addEventListener('click', () => {
            if (cropper) {
                cropper.destroy();
            }
            preview.innerHTML = '';
            fileInput.value = '';
        });
    </script>
</body>
 
</html>

Output:




Article Tags :