Defining An Image Crop Area With Javascript

Ever want to allow your web development clients to crop their own images as they upload them through your powerful CMS? If so, you've found a solution! No more explaining to them how to use an image manipulation program just to crop photos from their digital camera before they post them on their website! This Javascript code gives you a cross-browser, cross-platform simple way of defining a crop selection on an uploaded image.

Source Code Download

The code included in this source download contains the JavaScript, CSS and PHP files used to create ths demo. The PHP functions for this task are nearly identical to my Image Watermarking functions, except instead of using imagecopyresampled, you'd use imagecopy (and adjust a few function call parameters).

Step 1: Uploading The Image To Crop

First, upload a JPEG or PNG image file from your hard drive that you want to crop. This image will be used in the demonstration for defining the image crop selection area with the Javascript code.


Note that if you upload an image that is wider than this display area (between menus, borders, etc.) then the crop selection may not appear in the correct position on the image. I have not ben able to find a way to detect position changes due to floated elements like the column to the right.',

Updates

As I use this script more and more, I may find bugs or decide to implement something in a different way. When such is the case, I will add a note to this page about the differences since last version (don't expect a detailed changelog though).