DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • HTML5 < time > element: returned!
  • 6 Easy Ways to Start Coding Accessibly
  • A Beginner’s Guide to Automated Cross-Browser Compatibility Testing
  • Understanding Web Standards — Shadow DOM and Custom HTML Elements

Trending

  • Software Verification and Validation With Simple Examples
  • The Systemic Process of Debugging
  • DevSecOps: Integrating Security Into Your DevOps Workflow
  • Java Parallel GC Tuning
  1. DZone
  2. Coding
  3. Languages
  4. Crop image on the client side with JCrop and HTML5 canvas element

Crop image on the client side with JCrop and HTML5 canvas element

Antonio Santiago user avatar by
Antonio Santiago
·
Oct. 26, 11 · Interview
Like (0)
Save
Tweet
Share
29.00K Views

Join the DZone community and get the full member experience.

Join For Free

Suppose you are working on a nice web application where the user can upload images to, for example, a shop catalogue (mmm… that makes me think on something :p ) but wait… you don’t the catalogue uses the whole image you upload instead a piece of it. So, we need to crop the image.

Click for demo

I won’t go in depth on how to create the whole process, in summary:

  1. User choose an images and app upload to server.
  2. Wep app shows the images to user and allows him/her to crop the desired piece of image.
  3. Crop parameters (x, y, widht, height) are sent to server which responsible to really crop the image.

Here we talk about the step 2: the web page that allows to crop (or choose some image area) and send the parameters to the serve to make the real crop (for example using GD or ImageMagick).

The real magic comes from JCrop, a jQuery plugin to select areas within an image. As I note in the demo, in the JCrop project page there is a demo that makes a preview of the selection using two <img> elements.

Here the we are going to make something very similar but using the HTML5 canvas element (to practice a bit :p).

The first step is to add one img element pointing to the image and one canvas element which will act as the previous:

<img src="./sago.jpg" id="target" alt="Flowers" />
<canvas id="preview" style="width:150px;height:150px;overflow:hidden;"></canvas>


Now the code that preview the selected cropped image every time the user updates the selected area:

$('#target').Jcrop({
    onChange : updatePreview,
    onSelect : updatePreview,
    aspectRatio : 1
});
 
function updatePreview(c) {
    if(parseInt(c.w) > 0) {
        // Show image preview
        var imageObj = $("#target")[0];
        var canvas = $("#preview")[0];
        var context = canvas.getContext("2d");
        context.drawImage(imageObj, c.x, c.y, c.w, c.h, 0, 0, canvas.width, canvas.height);
    }
};


As you can see the main parameters are the coordinate fields (variable ‘c’). One way to send it to server is to store in four hidden input filed in a form, and send them on submit.

Looking again to the post I think there are too much words to simply show an easy HTML5 canvas example :)

HTML Element

Opinions expressed by DZone contributors are their own.

Related

  • HTML5 < time > element: returned!
  • 6 Easy Ways to Start Coding Accessibly
  • A Beginner’s Guide to Automated Cross-Browser Compatibility Testing
  • Understanding Web Standards — Shadow DOM and Custom HTML Elements

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: