Image Cropping and Compression using JSuites Library

Md Imran

 In modern web development, manipulating images on the client-side has become a crucial aspect of enhancing user experience. One such task involves image cropping, and the JSuites library provides a robust solution for implementing this functionality seamlessly within a web page.

Introduction

This tutorial aims to demonstrate the creation of an image cropper using the JSuites library along with HTML and JavaScript. This library offers a comprehensive set of tools for UI components, including a powerful image cropping module that enables users to crop images dynamically.



HTML Structure

The HTML structure consists of elements required to display the image and the cropping area. It also includes buttons to trigger the cropping action and manage user interactions.

JSuites Library Integration

The implementation utilizes the JSuites library for handling image cropping. This library simplifies the process by providing methods and functionalities to define cropping areas, retrieve cropped images, and manage user events effortlessly.

Implementation Overview

The JavaScript section primarily focuses on initializing the JSuites image cropper, defining the cropping area, capturing user actions, and retrieving the cropped image data.

Key Components

  1. HTML Structure: Contains the necessary elements to display the image and manage user interactions.
  2. JSuites Image Cropper: Utilizes the library's features to define the cropping area, handle user interactions, and retrieve the cropped image.

Implementation Steps

Step 1: Setting up the HTML Structure

The HTML file contains elements for displaying the image and a container for the JSuites image cropper. It also includes buttons for triggering the cropping action and managing user interactions.

Step 2: Integrating JSuites Image Cropper

Through JavaScript, the JSuites library is initialized, and the image cropper module is configured with specific dimensions and properties to allow users to select and crop an area of the image.

Step 3: Capturing Cropped Image

Upon user interaction (e.g., clicking a 'Crop' button), the cropped image data is retrieved using JSuites' methods. This data can then be processed further or displayed as needed within the application.

Step 4: Further Customization and Handling Events

Additional customization options and event handling can be incorporated to enhance the user experience, providing functionalities such as confirming or canceling the cropped image.

<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <link rel="stylesheet" href="https://jsuites.net/v4/jsuites.css" type="text/css" /> </head> <body> <!-- HTML Structure --> <div id="ic0010" class="w3-modal w3-small"> <!-- Modal content --> <div class="w3-modal-content w3-card-4 w3-animate-zoom"> <div style="display: flex;"> <div id="i-cropper" style="border:1px solid red; margin: 5px;"></div> </div> <i id="isd"></i> <div class="w3-row"> <button id="iget" class="w3-btn w3-blue">Crop</button> <button id="igetc" class="w3-btn w3-green" onclick="$('#iget').click();$('#ic0010').hide()">Conform</button> <button class="w3-btn w3-red" onclick="$('#ic0010').hide()">Close</button> </div> </div> </div> <div id="ir" onclick="$('#ic0010').show()"><img style="width:137px; height:165px; margin: 5px;"></div> <!-- Script Tags for Libraries --> <script src="https://jsuites.net/v4/jsuites.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@jsuites/cropper/cropper.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jsuites/cropper/cropper.min.css" type="text/css" /> <!-- JavaScript Logic --> <script> $('#ir').click(function() { $('#igetc').hide(); $('#iget').show(); cropper(document.getElementById('i-cropper'), { area: [205, 245], crop: [77, 90] }); $('#iget').click(function() { $(this).hide(); $('#igetc').show(); var cImg = document.getElementById('i-cropper').crop.getCroppedImage(); var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); canvas.width = 137; canvas.height = 177; ctx.drawImage(cImg, 0, 0, canvas.width, canvas.height); var coi = canvas.toDataURL('image/jpeg', 0.4); var iz = ((coi.length - 'data:image/jpeg;base64,'.length) * 3 / 4 / 1024).toFixed(2); $('#ir img').attr('src', coi); $('#isd').html('Image Size: ' + iz + ' KB'); }); }); </script> </body> </html>

Conclusion

In summary, leveraging the JSuites library for implementing an image cropping tool in HTML and JavaScript streamlines the process of enabling users to crop images dynamically within a web application. This integration showcases the simplicity and effectiveness of using JSuites to enhance image manipulation functionalities.

Developers can explore and expand upon this example, tailoring it to suit diverse project requirements and providing users with an intuitive image cropping experience.


This blog post highlights the implementation of an image cropper using the JSuites library, outlining steps to integrate and utilize its features for creating a dynamic and user-friendly image cropping tool within a web page.

Post a Comment

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.