$v){ // if(is_array($v)){ // foreach($v as $k2=>$v2){ // $CFG[$k][$k2]=str_replace('sys_path',$CFG['paths']['sys_path'],$CFG[$k][$k2]); // $CFG[$k][$k2]=str_replace('site_path',$CFG['paths']['site_path'],$CFG[$k][$k2]); // } // }else{ // $CFG[$k]=str_replace('sys_path',$CFG['paths']['sys_path'],$CFG[$k]); // $CFG[$k]=str_replace('site_path',$CFG['paths']['site_path'],$CFG[$k]); // } // } // echo '',"\n"; ?> Defining An Image Crop Area With Javascript

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).

',"\n", '

Image Cropping Error

',"\n"; foreach($imgData['errors'] as $err) echo '

',$err,'

',"\n"; echo '
',"\n"; }else{ echo '
',"\n", '

Image Cropping Successful

',"\n", '

Below is the resulting image from your crop selection.

',"\n", '

Your Uploaded Image

',"\n", '
',"\n"; } }else if(isset($_FILES['image'])){ // file upload form was posted, first set a filename $filename='images/'.date('YmdHi').preg_replace('`[^a-z0-9.]`i','',$_FILES['image']['name']); // now move the uploaded file to its new destination if($_FILES['image']['error']==0 && move_uploaded_file($_FILES['image']['tmp_name'],dirname(__FILE__).'/'.$filename)){ // file was moved successfully, we'll want to show the cropping thing now along with the uploaded image $size=getimagesize(dirname(__FILE__).'/'.$filename); // be sure this is an IMAGE and not some kind of script or other bad thing! if(!is_array($size)){ echo '
',"\n", '

ERROR Uploading File

',"\n", '

',"\n", ' You did not upload a valid image file. Please note the errors listed below and try again or report',"\n", ' them to Justin.',"\n", '

',"\n", '
',"\n"; }else{ echo '
',"\n", '

STEP 2: Define Your Image Cropping Selection

',"\n", '

',"\n", ' To select your image crop area, click the opposite diagonal corners of the area you want to keep.',"\n", ' A blue selection box should appear. If the selection isn\'t where you want it, click the image again to clear the selection and try again.',"\n", ' When you have your selection in the correct position, click the "Crop Image" button and the image will then be cropped to your selection.',"\n", '

',"\n", '

',"\n", '
',"\n", '
',"\n", ' ',"\n", '
',"\n", '
',"\n"; } }else{ // give the user reasons for failure echo '
',"\n", '

ERROR Uploading Image

',"\n", '

',"\n", ' There was an error encountered while trying to upload your image file. Please note the errors listed below and try again or report',"\n", ' them to Justin if asked to do so.',"\n", '

',"\n"; $ERR=FALSE; // check if we can write to the target if(!is_writable(dirname(__FILE__).'/'.$filename) || !is_writable(dirname(__FILE__).'/images')){ echo '

Server configuration error... In other words, Justin forgot to set file permissions for the directory where images are stored. Please tell him about it.

',"\n"; $ERR=TRUE; } // check if there was an upload error and report it switch($_FILES['image']['error']){ case UPLOAD_ERR_INI_SIZE: printf('

The uploaded file exceeds the maximum filesize for file uploads of %d MB.

'."\n",ini_get('upload_max_filesize')/1048576); $ERR=TRUE; break; case UPLOAD_ERR_FORM_SIZE: printf('

The uploaded file exceeds the maximum filesize of %d MB for this demonstration.

'."\n",$_POST['MAX_FILE_SIZE']/1048576); $ERR=TRUE; break; case UPLOAD_ERR_PARTIAL: echo '

The uploaded file was only partially uploaded.

',"\n"; $ERR=TRUE; break; case UPLOAD_ERR_NO_FILE: echo '

No file was uploaded.

',"\n"; $ERR=TRUE; break; } if(!$ERR){ echo '

I actually don\'t know what went wrong! Everything looks OK from this end. Try it again, if it still fails, then tell Justin.

'; } echo '
',"\n"; } }else{ // show the file upload form ?>

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).