CropResizer

CropResizer is a php class which's using GD library to resize and crop images. There are the features available :

  1. Resize image by specified width.
  2. Resize image by specified height.
  3. Crop and resize an image.

Requirement

Firstly, before you use this class please make sure your GD library (version2) is enabled.
http://php.net/manual/en/image.installation.php

And there are some links may help if you don't get that:

  1. How do I install GD on my windows server version of PHP
  2. How to install PHP GD2 library on Ubuntu
  3. CentOS: Enabling GD Support in PHP Installation

How to use

Step 1 : Load CropResizer class file

Including the CropResizer class file CropResizer.php before you use it:

include "CropResizer.php";

Step 2 : Create a CropResizer Object

Then create a CropResizer Object with source image path as argument, for example, source image 3105589762.jpg to be handled is located at samples folder in this sample project so the argument is samples/3105589762.jpg.

$cropResizer = new CropResizer("samples/3105589762.jpg");

*Beware the path is sys path but not img url

Step 3 : Call resize or crop functions

There are 3 functions to do resize and cropping, they are resizeKeepRatioByWidth(), resizeKeepRatioByHeight() and cropImg().

Methods to resize image

$cropResizer->resizeKeepRatioByWidth(250, "samples/3105589762_keepratio_by_w.jpg");
$cropResizer->resizeKeepRatioByHeight(250, "samples/3105589762_keepratio_by_h.jpg");

resizeKeepRatioByWidth($dst_w, $resultDirPath)

$dst_w is target width of your resized output image.
$resultDirPath is directory path of your resized image.

resizeKeepRatioByHeight($dst_h, $resultDirPath)

$dst_h is target height of your resized output image.
$resultDirPath is directory path of your resized image.

Methods to crop image

$cropResizer->cropImg(250,123,100,100,"samples/3105589762_crop.jpg");

cropImg($dst_w, $dst_h, $src_x, $src_y, $resultDirPath)

$dst_w is target width of your cropped output image
$dst_h is target height of your cropped output image
$dst_x is X-coordinate of the starting point to be cropped (X-coordinate of top-left corner) in your original image.
$dst_y is Y-coordinate of the starting point to be cropped (Y-coordinate of top-left corner) in your original image.
$resultDirPath is the output path of your cropped image.

Samples

There are a demo.php and a folder named samples as the example and 2 sample images named 3105589762.jpg and butterfly-wallpaper.jpeg was added inside. Resized or cropped images should be added in this directory after you run the demo.php :

Tested enviroment

  1. Windows 8, PHP Version 5.6.15 with GD Version bundled (2.1.0 compatible), FreeType Version 2.5.5
  2. CentOS 7, PHP Version 5.4.16 with GD Version bundled (2.1.0 compatible), FreeType Version 2.4.11

Update