Digittrix logo
                                        <link rel="stylesheet"
 href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.6.1/cropper.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.6.1/cropper.min.js"></script>

                                    

Cropper.js guarantees accurate cropping and flexible dimension management, essential for custom web development projects.

Step 2: Create Blade View for Image Cropper Form

Create a Blade view resources/views/image-crop.blade.php with the following form:

                                        <form method="POST" action="/upload-image">
    @csrf
    <label>Width</label>
    <input type="number" id="width" placeholder="Enter width">

    <label>Height</label>
    <input type="number" id="height" placeholder="Enter height">
    <br><br>

    <input type="file" id="imageInput" accept="image/*">
    
    <div class="image-container">
        <img id="image" style="max-width:100%;">
    </div>

    <input type="hidden" name="cropped_image" id="cropped_image">
    <br>

    <button type="button" id="cropBtn">Crop Image</button>
    <button type="submit">Submit</button>
</form>

<h3>Preview</h3>
<img id="preview" style="max-width:300px;">
                                    

This form lets users input dynamic dimensions, choose an image, and preview the cropped result — a crucial requirement for website development services involving image processing.

Step 3: Add Cropper.js JavaScript

Add the following script below the form to handle client-side cropping:

                                        <script>
let cropper;

document.getElementById("imageInput").addEventListener("change", function(e) {
    let width = document.getElementById("width").value;
    let height = document.getElementById("height").value;

    if (width === "" || height === "") {
        alert("Please enter width and height first");
        return;
    }

    let aspectRatio = width / height;
    let file = e.target.files[0];
    let reader = new FileReader();

    reader.onload = function(event) {
        let image = document.getElementById("image");
        image.src = event.target.result;

        if (cropper) {
            cropper.destroy();
        }

        cropper = new Cropper(image, {
            aspectRatio: aspectRatio,
            viewMode: 1,
            autoCropArea: 1,
            movable: true,
            zoomable: true,
            scalable: true,
        });
    };

    reader.readAsDataURL(file);
});

document.getElementById("cropBtn").addEventListener("click", function() {
    let width = document.getElementById("width").value;
    let height = document.getElementById("height").value;

    if (!cropper) {
        alert("Select image first");
        return;
    }

    let canvas = cropper.getCroppedCanvas({
        width: width,
        height: height
    });

    let base64 = canvas.toDataURL("image/png");
    document.getElementById("preview").src = base64;
    document.getElementById("cropped_image").value = base64;
});
</script>
                                    

Process Overview:

  1. Enter width and height.
  2. Select an image from your device.
  3. Crop the image using the interactive preview.
  4. Click Submit to upload the cropped image to the Laravel backend.

Step 4: Handle Image Upload in Laravel

In your controller, process the cropped image as follows:

                                        public function upload(Request $request)
{
    $request->validate([
        'cropped_image' => 'required|string',
    ]);

    $croppedImage = $request->input('cropped_image');
    $imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $croppedImage));
    $filename = 'image_'.time().'.png';

    Storage::disk('public')->put($filename, $imageData);

    return back()->with('success', 'Image uploaded successfully!');
}
                                    

This backend code converts the base64 cropped image into a PNG file and stores it securely, which is common practice in custom web development for Laravel projects.

Benefits of Using a Dynamic Image Cropper in Laravel

  • User-friendly: Allows users to crop images before upload.
  • Dynamic dimensions: Supports custom width and height.
  • Real-time preview: Users see exactly what will be uploaded.
  • Optimized for production: Reduces backend image processing.
  • Supports professional web services: Ideal for website development services and scalable solution applications.

Final Words

Adding a dynamic image cropper in Laravel improves both user experience and functionality. Using Cropper.js on the frontend and Laravel on the backend allows you to deliver high-quality website development services that handle images effectively.

If you are looking to hire Laravel developers for custom web or website development services, adding features like dynamic image cropping showcases expertise and enhances your application’s reliability.

Tech Stack & Version

Frontend

  1. HTML
  2. JavaScript
  3. Tailwind CSS
  4. Alpine.js

Backend

  1. PHP 8.1+
  2. Laravel 9 / 10 / 11
  3. Migrations
  4. Eloquent ORM

Deployment

  1. AWS EC2
  2. DigitalOcean
  3. Cloud Storage
  4. NGINX
  5. Apache