ImageResize PHP class

This is a PHP class designed for making the commonly used image manipulations in your code to be extremely easy. The class helps to implement resize and crop operations and supports JPG, GIF and PNG formats with keeping of the transparency.

The features include:

  • Resizing with or without keeping of the original proportions;
  • Resizing based on only one known side (width, height, long or short sides);
  • Preventing the upscaling of the image;
  • Cropping the canvas;
  • Resizing to fit the canvas size;
  • Preserving transparency for GIF and PNG images;
  • Saving the result as JPG, GIF or PNG format.

Source code on GitHub

https://github.com/deniskomlev/php-imageresize

Usage

Using of ImageResize class is clear and easy. First, you need to include it to your script:

require_once('[path_to_class/]ImageResize.class.php');

Now you have the ability to use it to resize your images.


Example 1. Let's resize source image with size of 260x180 pixels to 180x140 pixels with keeping of proportions:

$image = new ImageResize('sample.jpg');
$image->width(180);
$image->height(140);
$image->resize();
$image->save('new.jpg');

Before After

The new image size becomes 180x124. It's not exact 180x140, because keeping of proportions is turned on by default and image has been resized to fit the canvas size.


Example 2. If we need stretch image to given width and height regardless to original proportions, we can pass FALSE parameter to resize() method:

$image = new ImageResize('sample.jpg');
$image->width(180);
$image->height(140);
$image->resize(false);
$image->save('new.jpg');

Before After

The new image size is exact 180x140 and image has been stretched by height.


Example 3. If we need keep proportions but fill exact canvas size with cropping the part which will be out, we can use resizeToFill() method:

$image = new ImageResize('sample.jpg');
$image->width(140);
$image->height(140);
$image->resizeToFill();
$image->save('new.jpg');

Before After

The image has been resized with preserving of proportions and cropped from center to 140x140 size.


Example 4. Image can be resized to only one known side. It can be width:

$image = new ImageResize('sample.jpg');
$image->width(160);
$image->resize();
$image->save('new.jpg');

Before After

Or height:

$image = new ImageResize('sample.jpg');
$image->height(160);
$image->resize();
$image->save('new.jpg');

Before After

We also can use long side:

$image = new ImageResize('sample.jpg');
$image->setLongSide(160);
$image->resize();
$image->save('new.jpg');

Or short side:

$image = new ImageResize('sample.jpg');
$image->setShortSide(160);
$image->resize();
$image->save('new.jpg');

And it will set width or height size depending on image orientation.


Example 5. Crop image from center:

$image = new ImageResize('sample.jpg');
$image->width(140);
$image->height(140);
$image->crop();
$image->save('new.jpg');

Before After

Crop image from right top corner:

$image = new ImageResize('sample.jpg');
$image->width(140);
$image->height(140);
$image->crop('right', 'top');
$image->save('new.jpg');

Before After

Crop image from left bottom corner:

$image = new ImageResize('sample.jpg');
$image->width(140);
$image->height(140);
$image->crop('left', 'bottom');
$image->save('new.jpg');

Before After

Available parameters for cropping positioning are: 'left', 'center', 'right' for horisontal positioning, and 'top', 'center', 'bottom' for vertical positioning. Also these parameters can be specified in pixels.