PHP upload, resize and crop image class

PHP upload, resize and crop image class

I have been using the foundation of this script for the last few years to upload files – particularly imagery – and then quickly and easily resize or create multiple thumbnails of the file. The version I had been using only handled jpegs but I have now expanded it to handle transparent PNGs and GIFs – as well as opaque – and WBMPs.

So what is special about this script – here is a list of its features:

  • Uploads any image / file
  • Creates destination folder if it does not exist
  • Safely renames the file – removes spaces, special characters etc
  • Options to overwrite, make unique, abort or throw error if duplicate found
  • Easily create multiple resized images
  • Easily crop images to particular height and width from given XY coordinates
  • Keep or ignore aspect ratio
  • Specify new filenames
  • Add a prefix to the filename
  • Pad the resized image to the exact size required – or resize to longest size
  • Change the padding colour or make it transparent
  • Convert the image to a particular type (jpg, gif, png, wbmp)
  • Specify the image quality/compression – jpg and png only
  • Option to upscale smaller images or not
  • Return options: true/false | array of image details | new filename | output image to browser | blob

USAGE

To UPLOAD a file just use:

<?php
if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // if a file has been posted then upload it
include('INCLUDE_CLASS_FILE_HERE.php');
$myImage = new _image;
// upload image
$myImage->uploadTo = 'uploads/'; // SET UPLOAD FOLDER HERE
$img = $myImage->upload($_FILES['file']);
if($img) {
echo 'Woo woo - looked like it worked OK';
}
?>
To RESIZE the uploaded file
<?php
if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // if a file has been posted then upload it
include('INCLUDE_CLASS_FILE_HERE.php');
$myImage = new _image;
// upload image
$myImage->uploadTo = 'uploads/'; // SET UPLOAD FOLDER HERE
$myImage->returnType = 'array'; // RETURN ARRAY OF IMAGE DETAILS
$img = $myImage->upload($_FILES['file']);
if($img) {
$myImage->source_file = $img['path'].$img['image']; // THIS IS AUTOMATICALLY SET BY UPLOAD - just here for reference
$myImage->newPath = 'uploads/thumbs/';
$myImage->padColour = '#FFCC00'; // SET THE BACKGROUND TO A HIDEOUS ORANGEY COLOUR
$myImage->newWidth = 100;
$myImage->newHeight = 75;
$i = $myImage->resize(); // creates small thumbnail
// process main image to reduce storage requirements
$myImage->namePrefix = '';
$myImage->newPath = 'uploads/';
$myImage->newWidth = 800;
$myImage->newHeight = 600;
$o = $myImage->resize(); // resizes originally uploaded image
// check the file was create OK and add the image name to the variable: $image
if(($i==true)&&(file_exists($img['path'].$img['image']))) {
$image = $img['image']; }
} else {
$image = ''; // or set $image to nothing
}
// INSERT INTO DB (example sql)
$sql = "UPDATE table SET image_field = '".$image."' WHERE table_id = wotevver";
}
?>

UPDATE – ADDED CROPPING SUPPORT…

I’m not sure how many of you (like) me have used JavaScript cropping scripts to allow your users to draw a crop box over an image and then want PHP to cut the image to that size – well I have used various jQuery image cropping scripts as well as some Mootools ones and most of them are very good but come with limited PHP image handling scripts – so I built a cropping method into this one. It is all available in the download link below but to use it you can just upload (and/resize) your image then call:

$myImage->crop($width,$height,$fromX,$fromY);

This will then crop your image to the height and width you specify starting from the X/Y positions you pass and return it in the format you request (array containing info, full path of image, boolean on success or blob). The script requires all 4 parameters (W/H/X/Y) in order to work and also requires the $myImage->source_file to be set – although this is automatically set via the upload or resize methods.

One other addition is that you can now also output a “blob” so you can store the raw image data in a database rather than to the filesystem.

————————————————————-

To use some of the other features just look through the code at the top of the class – it should be self explanatory.

You can DOWNLOAD THE SCRIPT HERE: PHP image upload and resize class – I have included a test form at the bottom so yo can quickly test it – but I would advise you remove it if you use it in a proper application.

I have also put together a little example demo script for uploading, cropping and resizing an image using jQuery and jCrop, and of course this script – you can view the demo here: http://cropscript.mjdigital.co.uk and download the source files from: http://mj7.co.uk/ajdy

As always I really appreciate feedback on these scripts – so if you like it and use it PLEASE comment below or retweet this post – you can also tweet me @mjdigital – it is the only way I know if I should keep adding these scripts.

About the Author

MJ7: aka Mark Jackson @ MJDIGITAL. Digital Consultant - primarily digital marketing, web app design and development, multimedia and SEO techniques. But will happily consult on most IT matters.