12CropImage version 1
Introduction
This is a little script that can crop uploaded images to a specified size. I needed this script for an administration part of a website. The user of the site wanted to change the images every now and then. Because he didn’t want to learn Photoshop, I thought it would be nice, if he could crop the uploaded pictures himself. So far I couldn’t find a cropping script online, that’s why I made it myself. The script is not complicated, but it’s just the idea to combine DHTML with GD ;-)
How does it work?
The script uses parts of the DHTML Library of Thomas Brattli of DHTMLcentral.com. This excellent library gives you control over layers in most browsers. This control makes it possible to drag and resize the cropping selection. It also gives the necessary data for determining scale, and size of the crop. This data is send to the PHP script. The script uses some basic math and GD-functions to rescale the images.
What do I need?
To make the script work you need a web server with PHP installed. PHP must be configured to use the GD-library for image manipulation. Most php-hosts also have GD installed. Until GD version 2.0 images are palette based and both Gif and Jpeg are supported, from version 2.0 GD supports true colour images in Png and Jpeg. Gif is no longer supported. This is also the reason why most hosts, including mine, still have a lower version of GD installed. This script only supports Jpeg.
Who can use the script?
Unlike all other content on my site, my plugins and scripts are released under a GNU General Public License. A copy of the full license text is included with your Wordpress distribution and can also be found here. I appreciate it when you let me know if and how you use my script…
Installation?
The script I made for download is very simple in it’s layout. It uses normal buttons to operate and no style sheets to change its appearance. All these things are up to you and can be changed by yourself. Basically you just unzip the files and place them in a directory on your server. After that there are only some variables, which you have to set. I will discuss them shortly. The first four variables can be called from outside the script. The others are fixed.
$org: sets the name of the original image. Can also be called from outside$res: sets the name of the cropped result*.$crw: sets the width in pixels of the cropped result.$crh: sets the height in pixels of the cropped result.
$jpegqual: Sets the quality of the crop, ranges from 10-100, the higher the better.
$redirect: If you rename this file or include it in another, you can change this value.$javafile: The relative path to the 12cropimage.js file.$spacer : The relative path to a spacer image. This is a 1 by 1 pixel transparent image.$gdversion: Set this value to 2 if you use GD2 and to 1if you use any previous version**.
$maxwinw: Determines the maximum width by which the uploaded images are showed in the cropping window***.$maxwinh: Determines the maximum height by which the uploaded images are showed in the cropping window***.
$txt: This is an array with all the text that is used in the script. Change it if you want to use another language.
Please note that if you have register_globals off and you call for instance 12cropimage.php?org=source.jpg&res=result.jpg, you’ll have to get the values using $_GET['org'] and $_GET['res'].
* NOTE: Make sure the directory you choose to save the result to, has writing permissions!
** Unfortunately there’s a major bug in GD2’s function imagecopyresampled and although the use of this function could further improve the quality of the cropped image, it’s not possible to use it in this script.
*** If an uploaded image exceeds the maximum dimensions it will be automatically resized. The crop will be made with the original and the crop selector will be scaled down the same as the image has been, to show it on your screen.
See the script in action
I received a lot of requests to show the code of the demo, including the upload script. Therefor I made a little zip package that includes the code I used for the demo. Beware that the upload part is one of the first things I made in PHP and does not excel in beauty. Click here to download the package.
Download the script
You can download the script as a zip-file. In this zip-file are four files:
- 12cropimage.php; the script itself
- 12cropimage.js; the necessary JavaScript from DHTMLcentral
- spacer.gif: transparent image needed for cropper
- 12readmefirst.txt: text file with a copy of this text.
The current version is version 0.2: download 12cropimage_v0.2.zip
Problems / Questions / Suggestions / Support?
Sorry, but I do longer give support for this plugin. Development has been dead for 2 years and this page is nothing more than an archive.
What changed?
0.2 Stripped out the surplus of code.
0.1 This were it started.