Skip to content

renobrant/Image-Resizer-Component-for-CakePHP2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 

Repository files navigation

Image Resizer Component

Requires CakePHP2+

Installation

  • Open terminal and cd to your app folder: cd /path/to/app
  • Then run git clone git@github.com:mdunham/Image-Resizer-Component-for-CakePHP2.git Plugin/ImageResizer
  • In your controller add 'ImageResizer.ImageResizer' to the list of components
  • If you don't have CakePlugin::loadAll(); in your bootstrap.php then add CakePlugin::load('ImageResizer'); to it

Handeling Errors

This class defines three custom exceptions that all extend CakeException. You can use these to catch any errors and handle them in a nice manner.

  • ResizerNoSourceException - Gets thrown whenever file_exists() fail on source image path
  • ResizerFormatException - Gets thrown whenever the source file does not have an image/* mime type
  • ResizerOutputException - Gets thrown whenever file_put_contents fails on the output file

If you fail to catch these they will result in a default cake error message. Below are some examples of how to catch them.

Usage Exmaples

$succeed = $this->ImageResizer->resizeImage($inputFilePath, array(
	'output' => $outputFilePath,
	'maxHeight' => 120,
	'maxWidth' => 120
));

For full width and height use cropZoom

$succeed = $this->ImageResizer->resizeImage($inputFilePath, array(
	'output' => $outputFilePath,
	'cropZoom' => true,
	'maxHeight' => 120,
	'maxWidth' => 120
));

or for a plain simple example:

$succeed = $this->ImageResizer->resizeImage($inputFilePath);

In that example the $inputFilePath is used as the outputFilePath basically overriding itself. You can also setup the options before calling it.

$this->ImageResizer->maxHeight = 200;
$this->ImageResizer->maxWidth = 200;
$succeed = $this->ImageResizer->resizeImage($inputFilePath);

Error Handeling

try {
	$succeed = $this->ImageResizer->resizeImage($inputFilePath, array(
		'output' => $outputFilePath,
		'maxHeight' => 120,
		'maxWidth' => 120
	));
} catch (ResizerNoSourceException $error) {
	// Handel the source not found problem
} catch (ResizerFormatException $error) {
	// Handel the format problem
} catch (ResizerOutputException $error) {
	// Handel the file write problem
}

or if you only wish to handle one of them but dont want an ugly error:

try {
	$succeed = $this->ImageResizer->resizeImage($inputFilePath, array(
		'output' => $outputFilePath,
		'maxHeight' => 120,
		'maxWidth' => 120
	));
} catch (ResizerNoSourceException $error) {
	// Handel the source not found problem
} catch (CakeException $error) {
	// Handel the others
}

or handle them all at once:

try {
	$succeed = $this->ImageResizer->resizeImage($inputFilePath, array(
		'output' => $outputFilePath,
		'maxHeight' => 120,
		'maxWidth' => 120
	));
} catch (CakeException $error) {
	// Handel all problems
}

Available Options

  • maxWidth: The maximum width the image can have defaults to 200
  • maxHeight: The maximum height the image can have defaults to 200
  • allocatedMemory: If this is set it will temporarily increase the memory_limit of PHP defaults to 100M
  • output: The full path to where the new image should be saved if blank overwrites the source image
  • cropZomm: This will automatically crop the photo based on the maxWidth and maxHeight defaults to true
  • deleteSource: If set to true will delete the source image after successfully resizing defaults to false
  • ignoreSmallImages: If set to true will skip images that are smaller than your maxWidth and maxHeight defaults to true

Thanks

Any problems let me know happy baking.

About

CakePHP2 component for resizing/cropping images

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published