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
- 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
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';
}
?>
<?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";
}
?>
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.
As always I really apreciate feedback on these scripts – so if you like it and use it PLEASE just comment below or tweet me @mjdigital – it is the only way I know if I should keep adding these scripts.