A php class to resize and crop image.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples
CropResizer.php
LICENSE
README.md
demo.php

README.md

CropResizer

CropResizer is a php class which's using GD library to resize and crop images, supporting images in bmp, gif, jpg and png format. 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:

###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

##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 :

  • 3105589762_crop.jpg
  • 3105589762_keepratio_by_h.jpg
  • 3105589762_keepratio_by_w.jpg
  • 3105589762_crop_overflow1.jpg
  • 3105589762_crop_overflow2_right.jpg
  • 3105589762_crop_overflow2_bottom.jpg
  • butterfly_crop.jpg
  • butterfly_h150_keepratio_by_h.jpg
  • butterfly_w600_keepratio_by_w.jpg
  • butterfly_crop_overflow1.jpg
  • butterfly_crop_overflow2_right.jpg
  • butterfly_crop_overflow3_bottom.jpg

##Updates 11/07/2016 - Fix bugs and update: Update incorrect formula to resize image by height

  1. Add support to handle gif and png images.
  2. Fix bugs and update: Update incorrect formula to resize image by height

14/09/2016 - Correct Bug

  1. Remove duplicated switch statement which make the script can't run.
  2. Add the case if selected crop-area bigger then the maximum x or y-coordinate of source image, cut overflowed crop area and keep cropped image ratio same as required ratio.

##Offical page: For detailed information and latest doc updates, you can visit our page:
http://tool.soyosolution.com/php_crop_resizer/