Requires CakePHP2+
- 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
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.
$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
}
- 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
Any problems let me know happy baking.