Rounded Image Cropper using HTML5 and Javascript

Here’s a JavaScript solution that uses HTML5’s canvas to create a basic photo upload, zoom, crop, and download functionality. This script utilizes the Cropper.js library for easy image zooming and cropping.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Upload and Crop</title>

    <!-- Cropper.js CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.css" rel="stylesheet">
    
    <!-- Custom CSS -->
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background-color: #f0f2f5;
            margin: 0;
        }
        
        .container {
            text-align: center;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            max-width: 900px;
            width: 100%;
            margin: 30px 0px;

            display: flex;
            flex-direction: row;
        }

        .container .left-panel{
            width: 70%;
            padding: 0 10px;
        }
        .container .right-panel{
            width: 30%;
            padding: 0 10px;
        }

        h2 {
            color: #333;
            margin-bottom: 20px;
        }

        input[type="file"] {
            display: block;
            margin: 10px auto;
            padding: 8px;
            font-size: 16px;
            cursor: pointer;
        }

        #image {
            max-width: 100%;
            border: 2px solid #ddd;
            border-radius: 4px;
            margin: 20px 0;
        }

        #download {
            padding: 10px 20px;
            font-size: 16px;
            color: #fff;
            background-color: #007bff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        #download:hover {
            background-color: #0056b3;
        }

        #download:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        /* Preview section styling */
        .preview {
            margin-top: 20px;
            margin-bottom: 20px;
        }

        .preview canvas {
            border-radius: 50%; /* To show the preview as a circle */
            border: 2px solid #ddd;
            max-width: 150px;
            max-height: 150px;
            background: #ccc;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left-panel">
            <h3>Round Image Cropper</h3>

            <!-- Image upload input -->
            <input type="file" id="uploadImage" accept="image/*">

            <!-- Image container -->
            <div style="margin-bottom: 20px;">
                <img id="image" src="noimageavailable.jpg" alt="Upload an image to crop">
            </div>
        </div>
            
        <div class="right-panel">
            <!-- Preview section for real-time cropped image -->
            <div class="preview" id="previewSection">
                <h3>Preview of Cropped Image:</h3>
                <canvas id="previewCanvas" width="150" height="150"></canvas>
            </div>

            <!-- Upload button -->
            <button id="download" style="display:none;">Download Cropped Image</button>
            
        </div>
            
    </div>

    <!-- Cropper.js Script -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.js"></script>

    <script>
        const uploadImage = document.getElementById("uploadImage");
        const image = document.getElementById("image");
        const downloadButton = document.getElementById("download");
        const previewSection = document.getElementById("previewSection");
        const previewCanvas = document.getElementById("previewCanvas");
        const previewContext = previewCanvas.getContext("2d");

        let cropper;

        // Generate a random 12-character string
        function generateRandomFilename(length = 12) {
            const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            let result = '';
            for (let i = 0; i < length; i++) {
                result += characters.charAt(Math.floor(Math.random() * characters.length));
            }
            return result;
        }

        // Image upload and cropper initialization
        uploadImage.addEventListener("change", (e) => {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    image.src = e.target.result;
                    image.style.display = "block";

                    // Initialize Cropper.js after image is loaded
                    image.onload = () => {
                        if (cropper) cropper.destroy();
                        cropper = new Cropper(image, {
                            aspectRatio: 1,
                            viewMode: 1,
                            scalable: true,
                            zoomable: true,
                            setDragMode: 'move',
                            crop() {
                                updatePreview();
                            }
                        });
                        downloadButton.style.display = "inline-block";
                    };
                };
                reader.readAsDataURL(file);
            }
        });

        // Function to update the preview canvas with the cropped image
        function updatePreview() {
            if (!cropper) return;
            
            const canvas = cropper.getCroppedCanvas({ width: 150, height: 150 });

            // Clear previous preview
            previewContext.clearRect(0, 0, previewCanvas.width, previewCanvas.height);

            // Draw the cropped image as a circle
            previewContext.beginPath();
            previewContext.arc(75, 75, 75, 0, Math.PI * 2); // Centered circle in a 150x150 canvas
            previewContext.closePath();
            previewContext.clip();
            
            previewContext.drawImage(canvas, 0, 0, 150, 150);
        }

        // Upload button - Cropping, rounding, and uploading to the server
        downloadButton.addEventListener("click", () => {
            if (cropper) {
                const canvas = cropper.getCroppedCanvas({ width: 150, height: 150 });

                // Create a rounded, transparent version
                const roundedCanvas = document.createElement("canvas");
                roundedCanvas.width = canvas.width;
                roundedCanvas.height = canvas.height;
                const context = roundedCanvas.getContext("2d");

                context.beginPath();
                context.arc(
                    canvas.width / 2,
                    canvas.height / 2,
                    canvas.width / 2,
                    0,
                    2 * Math.PI
                );
                context.closePath();
                context.clip();

                context.drawImage(canvas, 0, 0);


                // Download the image
                roundedCanvas.toBlob((blob) => {
                    const link = document.createElement("a");
                    link.href = URL.createObjectURL(blob);
                    link.download = `${generateRandomFilename()}.png`;
                    link.click();
                    URL.revokeObjectURL(link.href);
                }, "image/png");
            }
        });
    </script>
</body>
</html>