Online Photoshop in PHP (Series Part 4) >>

Crop Image Like Orkut

Today we will discuss about cropping of images using php and gd library.

Look at the simple example here:

$x1 = $GET['x1']; $y1 = $GET['y1']; $x2 = $GET['x2']; $y2 = $GET['y2']; $imageobject = imagecreatefromjpeg('hari.jpg'); $imagecropped = Crop($imageobject,$x1, $y1, $x2,$y2); $temppath = rand(1,99999)."hari.jpg"; imagejpeg( $imagecropped,$temppath); header('Location: simplecrop.php?img='.$temp_path ); ?>

Here the Crop function is doing the real work. x1 and y1 are Top-Left Coordinates of new image and x2, y2 are the Bottom-Right .

Here is the function for Crop

function Crop($image, $xpos1,$ypos1,$xpos2,$ypos2) { require_once('class.cropcanvas.php'); $cc =& new CropCanvas(); $cc->loadImageFromObject($image); $cc->cropToDimensions($xpos1, $ypos1, $xpos2,$ypos2 ); return $cc->getImageObject(); } ?>

You can see the class.cropcanvas.php file from the attachment.

In the above example, we have to enter each coordinates manually. See the example below:

Here you can select the area by click and drag (like orkut)

This is implemented with the help or jslib javascript. The given attachment in the end of this post contains the all.

Keep reading for the further posts in this series. Next post is on Intellegent watermarking on images.

Subscribe to my Feeds: