Image Upload and Manipulation in PHP using class.upload

Image upload and manipulation can be done easily using "class.upload". This is a PHP class written by the author of verot.net. It is very easy to use and has a lot of features such as convert, resize, watermark and crop etc... You can also apply filters to add borders, text, watermarks, etc.


Working

  • You must instantiate the upload class with the uploaded file field array from the form. This class will check if the file is uploaded. e.g. $handle = new upload($_POST[‘upload_file’]);
  • Then you can set the various process options available such as greyscaling the image, resizing and various other options that should be applied to the image. Set the allowed property to "image/*", so that only image files can be uploaded.
  • Finally call the process('path_to_uploads') method with the location of where the image should be placed when processed. This creates a new instance of the original file, so that the original image file is untouched and remains same for each process.
  • Use the processed property to check if the image is processed. You can repeat setting up a new set of processing variables, and calling process again as many times as you want.
  • Once finished with the process, call clean() method to delete the original uploaded file.

HTML Form Code

<form enctype="multipart/form-data" method="post" action="index.php">
    <label>Choose a file to upload <input type="file" name="image_field" /></label><br/>
    <input type="submit" name="Submit" value="upload" />
</form>

Initializing the class

$handle = new upload($_FILES['image_field']);

Following are some of the process and filters that you can apply to the image file uploaded. Some of the process variable are common to all the process shown below and won't be shown here. However you can view it by downloading the source code.

Resize Image

To resize the image, the main process variable to set is image_resize. This is a boolean value.

// Resize the image
$handle->image_resize = true;
$handle->image_x = '100';
$handle->image_y = '100';
$handle->process('uploads/');
if ($handle->processed) {
    echo '<h4>Resized Image</h4>';
    echo "<div><img style='border: 1px solid #333;' src='$handle->file_dst_pathname'/></div>";
} else {
    echo 'error :<br/>' . $handle->error;
}

Greyscale Image

To make an image greyscale, the only option to set is image_greyscale.

// Make a greyscale image
$handle->image_greyscale = true;
$handle->process('uploads/');
if ($handle->processed) {
    echo '<h4>Greyscale image created<h4/>';
    echo "<div><img style='border: 1px solid #333;' src='$handle->file_dst_pathname'/></div>";
} else {
    echo 'error :<br/>' . $handle->error;
}

Watermark Image

To create a watermark use the image_watermark property to specify the location of the image that should be placed as watermark. There are various options that can be used to position and modify the opacity of the image.

// Watermark image
$handle->image_watermark = 'img/codedodle.png';
$handle->process('uploads/');
if ($handle->processed) {
    echo '<h4>Watermarked Image<h4/>';
    echo "<div><img style='border: 1px solid #333;' src='$handle->file_dst_pathname'/></div>";
} else {
    echo 'error :<br/>' . $handle->error;
}

Image Relection

To create the image reflection the amount of the original image size that should be reflected must be specified in image_reflection_height property. It can be in pixels, %. You can also set the distance between the reflection and the image using image_reflection_space property.

// Reflect image
$handle->image_reflection_height = '20%';
$handle->image_reflection_space = 0;
$handle->process('uploads/');
if ($handle->processed) {
    echo '<h4>Reflected Image<h4/>';
    echo "<div><img src='$handle->file_dst_pathname'/></div>";
} else {
    echo 'error :<br/>' . $handle->error;
}

Text on Image

To place a text on the image, use the image_text property. Also, you can change the image color, font, opacity and position.

// Text on image
$handle->image_text = 'CodeDodle';
$handle->image_text_color = '#3b88c3';
$handle->image_text_opacity = 70;
$handle->image_text_position = 'TR';
$handle->process('uploads/');
if ($handle->processed) {
    echo '<h4>Text on Top Right Corner<h4/>';
    echo "<div><img src='$handle->file_dst_pathname'/></div>";
} else {
    echo 'error :<br/>' . $handle->error;
}

There are more other features like crop, rotate etc... For a full list of process options view the Documentation.