UvumiTools Crop

cropping test

Description

In today's web2.0 era, more and more websites are user generated and boast dynamic content. Users sumbit text, images, videos, providing content for entire web sites. Most of the time, people submit photos straight from their camera because they don't have photo editing programs or they don't know how to use them. This simplified tool gives your users the ability to create a selection area that can be used to crop an image, live on your web site.

Features

Requirements

What you'll need:

How To

Initialization

new uvumiCropper(image,options);

Arguments

Options

Implementation

First, you simply import the files provided in the zip archive into your document. While you can insert <script> tags anywhere, we recommend you put them inside the <head> tag:

<head>
<!-- Insert these lines in your document's head -->
<link rel="stylesheet" type="text/css" media="screen" href="css/uvumi-crop.css"/>
<script type="text/javascript" src="js/mootools-for-crop.js"> </script>
<script type="text/javascript" src="js/UvumiCrop.js"> </script>
</head>

Then you must prepare the HTML code. You need one image. Let's just give it the id "myImage".

<div>
<img id="myImage" src="my_image.jpg" alt="My image" />
</div>

"my_image.jpg" is just a random image file name, we just assume it exists.

Then we initialize the cropper object with this image's ID. Don't forget to wrap the ID with quotes when passing it to the object (double or single, doesn't matter);

<script type="text/javascript" >
new uvumiCropper('myImage');
</script>

In the end, if we put everything together. Mootools requires a strict Doctype to work properly. Use something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/uvumi-crop.css"/>
<script type="text/javascript" src="js/mootools-for-crop.js"> </script>
<script type="text/javascript" src="js/UvumiCrop.js"> </script> <script type="text/javascript" >
new uvumiCropper('myImage');
</script>
</head>
<body>
<div>
<img id="myImage" src="my_image.jpg" alt="My image" />
</div>
</body>
</html>

You should get something like this. A cropper with default options: 80*80 pixels, resizable, keeps aspect ratio, no preview, 50% opacity mask

example 1

Let's try a step above. This time, we'll set no fixed ratio, with a preview. First part where you include the required script is the same. But you'll need an existing container for preview (because the script is not supposed to guess where to display it). Where just going to create an empty div, with id "myPreview".

<div>
<div id="myPreview" </div> <img id="myImage" src="my_image.jpg" alt="My image" />
</div>

Then we must tell the script to use this div as a preview container when we initialize the object, with the preview option. We also said we didn't want to keep the selection's aspect ratio, so we're going to set keepRatio to false

<script type="text/javascript" >
new uvumiCropper('myImage',{
keepRatio:false,
preview:'myPreview'
});
</script>

Put it together:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/uvumi-crop.css"/>
<script type="text/javascript" src="js/mootools-for-crop.js"> </script>
<script type="text/javascript" src="js/UvumiCrop.js"> </script> <script type="text/javascript" >
new uvumiCropper('myImage',{
keepRatio:false,
preview:'myPreview'
});
</script>
</head>
<body>
<div>
<div id="myPreview" </div> <img id="myImage" src="my_image.jpg" alt="My image" />
</div>
</body>
</html>

And you should get something like this. Try to resize the selection and check out how the preview reacts.

example 2

One more example: we're going to modify the genearal look of the cropper by assigning alternate CSS classes. We are also going to change the default minimum width and height, and, just to show that it's possible, disable resizing.

<script type="text/javascript" >
new uvumiCropper('myImage',{
maskOpicity:0.3,
maskClassName:'blueMask',
resizerClassName:'yellowSelection',
mini:{
x:120,
y:90
},
resizable:false
});
</script>

HTML will be the same as the first exemple, but we are going to create new CSS classes, corresponding the those we set in the cropper's options

<style type="text/css" >
.yellowSelection{
border: 2px dotted #FFB82F;
}

.blueMask{
background-color:#00f;
cursor:pointer;
}
</style>

Resulting code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/uvumi-crop.css"/>
<style type="text/css" >
.yellowSelection{
border: 2px dotted #FFB82F;
}

.blueMask{
background-color:#00f;
cursor:pointer;
}
</style>
<script type="text/javascript" src="js/mootools-for-crop.js"> </script>
<script type="text/javascript" src="js/UvumiCrop.js"> </script> <script type="text/javascript" >
new uvumiCropper('myImage',{
maskOpacity:0.3,
maskClassName:'blueMask',
resizerClassName:'yellowSelection',
mini:{
x:120,
y:90
},
resizable:false
});
</script>
</head>
<body>
<div>
<img id="myImage" src="my_image.jpg" alt="My image" />
</div>
</body>
</html>

Should produce this, if not, go back and check for typos or mistakes:

example 3

Ok, last but not least, we'll look at how the callback function works. At the same time, we are going to demontrate the HTML resizing feature: We are going to pick an 800px wide image, but we are going to scale it down to 400px match with the other. So techincally, when we draw a 100px*100px selection on this scaled down image, it virtually represents 200*200px on the full size image, the one the server will use. Same goes with top and height, and preview too, everything has to match this X2 factor. The script handles it and does all the conversion for you, if the image has been scaled up or down.

We prepare the HTML first. We are going to create a form with four inputs. For the image, we are going to set the WIDTH attribute (we can either set here or with CSS, works with height too):

<div>
<img id="myImage" width="400" src="my_image.jpg" alt="My image" />
</div>
<form action="crop-image.php" method="post" >
<p>
<input id="input-top" name="top" type="text" readonly="readonly" />
<input id="input-left" name="left" type="text" readonly="readonly" />
<input id="input-width" name="width" type="text" readonly="readonly" />
<input id="input-height" name="height" type="text" readonly="readonly" />
<input name="image" value="my_image.jpg" type="hidden" />
</p>
<p>
<button type="submit" > Send </button>
</p>
</form>

Note that we create four read-only text inputs for this example, but in a real production environment, we'd probably set them as hidden, just like the one containing the image name (or any other kind of unique identifier). Now we want to synchronize those inputs with our cropper:

<script type="text/javascript" >
new uvumiCropper('myImage',{
onComplete:function(top,left,width,height){
$('input-top').set('value', top);
$('input-left').set('value', left);
$('input-width').set('value', width);
$('input-height').set('value', height);
}
});
</script>

It's that simple!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/uvumi-crop.css"/>
<script type="text/javascript" src="js/mootools-for-crop.js"> </script>
<script type="text/javascript" src="js/UvumiCrop.js"> </script> <script type="text/javascript" >
new uvumiCropper('myImage',{
onComplete:function(top,left,width,height){
$('input-top').set('value', top);
$('input-left').set('value', left);
$('input-width').set('value', width);
$('input-height').set('value', height);
}
});
</script>
</head>
<body>
<div>
<img id="myImage" width="400" src="my_image.jpg" alt="My image" />
</div>
<form action="crop-image.php" method="post" >
<p>
<label for="input-top" >Top: </label> <input id="input-top" name="top" type="text" readonly="readonly" />
<label for="input-left" >Left: </label> <input id="input-left" name="left" type="text" readonly="readonly" />
<label for="input-width" >Width: </label> <input id="input-width" name="width" type="text" readonly="readonly" />
<label for="input-height" >Height: </label> <input id="input-height" name="height" type="text" readonly="readonly" />
<input name="image" value="my_image.jpg" type="hidden" />
</p>
<p>
<button type="submit" > Send </button>
</p>
</form>
</body>
</html>

You can see the form's inputs being updated whenever you release the selection. Note how the values are twice as large as the actual selection because the picture is scalled down by 50%:

example 4

Actual picture size : 800*698 pixels
Displayed size : 400*349 pixels
Coordinates generated match the picture's real size.

Known Issues

Download

Change Log