There is a lot of PHP classes for upload, resize and crop images. But I created this one for many reasons as mentioned below:

  • Ease of use.
  • Flexibility.
  • PHP version: 5 or later.
  • Can upload, resize and crop image.. All in one.

After knowing the reasons for making this class, let’s know how it works.

Let me explain first how to manage an image to be uploaded and then to be cropped or resized.
$uploader = new uploader();
// Setting properties then Uploading the image
$uploader->source = $_FILES[‘field_image’];
$uploader->destDir = "images/";
// =====================
// Or you may use this too
$uploader->destDir = "images/";
Note: upload() returns the file name uploaded.

To get error messages, use this code:
echo $uploader->getError();
// This function will return all errors occured while uploading the image. But it’s not printing it to print it where ever you want.
And to get Information messages use this code:
echo $uploader->getInfo();
// This function will return all information to let the user print it where ever he wants.

After uploading this image, we may resize it. To do that use the following code:
$uploader->newWidth = 75; // in Pexels.
$uploader->newHeight = 75;
$uploader->resizeDir = "images/resized/";
You may also use this syntax:
// $uploader->resize($file,$width,$height);
// $file could has a value of "" (nothing), in this case we use the last uploader file.
// if the $file has a String value (file url) it will resize this new file.
$uploader->resizeDir = "images/resized/";
// Or you may upload and resize in the same line:
$uploader->resizeDir = "images/resized/";

Crop this image after uploading it:
$uploader->cropDir = "images/cropped/";
$uploader->newWidth = 75;
$uploader->newHeight = 75;
$uploader->top = 20; // Default is ZERO.. This used to set the cropping top location from the original image.
$uploader->left = 40; // Default is ZERO.. This used to set the cropping left location from the original image.
// You may also use this:
// $uploader->crop($file,$width,$height,$top,$left);
$uploader->cropDir = "images/cropped/";

I hope this is helpfull.



7 Replies to “PHP image class (upload, resize, crop)”

  1. Hi thanks for this code.
    I want to do video processing in php is it possoble?
    means i have to track object in video.

    thanks in adv.

  2. Nice class, Just want to let you know though that there is a bug in the cropping of images.

    You use this code to crop:

    imagecopy($normal, $src, 0, 0, $this->top, $this->left, $this->newWidth, $this->newHeight);

    The official PHP documentation however states that the 5th parameter is src_x and the 6th is src_y, so the other way around.

    I had issues with cropping images, they all turned black. But it turned out the left and top were switched.

    Just wanted to let you know, Cheers 😉

Leave a Reply

Your email address will not be published. Required fields are marked *