The Resize Helper to Resize Images On The Fly
The class
Download The Resize Helper From http://github.com/harake
Copy it to your ‘app\views\helpers’ folder and rename it as resize.php
The Resize Helper requirements
The $helpers array member variable of your controller or your AppController, should look like this
var $helpers = array (..., ..., ..., 'Resize');
The class tries to construct its two public member variables $versions_store and $versions_public where $versions_store is the directory path of the current original image (usually outside the webroot) and $versions_public is the directory path of the current resized image (usually inside the webroot)
How does the class construct them?
Priority of detection
- Either you set them explicitly in the view because they are public
- 1.2 style or before, should be an absolute path, the trailing DS is optional
- $resize->versions_store = APP . ‘uploads’ . DS;
- $resize->versions_store = APP . ‘uploads’;
- 1.2 style or before, the webroot itself (default)
- $resize->versions_public = ”;
- 1.2 style or before, relative path to the webroot, the trailing DS is optional
- $resize->versions_public = ‘files’ . DS . ‘versions’ . DS;
- $resize->versions_public = ‘files’ . DS . ‘versions’;
- 1.2 style or before, absolute path, the trailing DS is optional
- $resize->versions_public = WWW_ROOT . ‘files’ . DS . ‘versions’ . DS;
- $resize->versions_public = WWW_ROOT . ‘files’ . DS . ‘versions’;
- 1.3 style or higher
- $this->Resize->versions_public = …;
- $this->Resize->versions_store = …;
- 1.2 style or before, should be an absolute path, the trailing DS is optional
- Either in your configuration file (‘app\config\bootstrap.php’)
- Configure::write(‘versions_store’, …);
- Configure::write(‘versions_public’, …);
- Or by defining two constants in your configuration file(‘app\config\bootstrap.php’)
- define(‘VERSIONS_STORE’, …);
- define(‘VERSIONS_PUBLIC’, …);
The Features In The Resize Helper
When resizing every original image to a smaller one, we have to choose one of the three options
- wrap (fixed-ratio option)
- $resize->wrap($filename, $destination_width, $destination_height);
- pad (complete the smaller destination box with empty pixels)
- $resize->pad($filename, $destination_width, $destination_height, $x = 2, $y = 2);
- crop (rarely used, it crop-centered the smaller image)
- $resize->crop($filename, $destination_width, $destination_height);
Remember that
- $filename is the relative path to the $versions_store cited above, example ‘image1.jpg’, ‘folder1′ . DS . ‘image1.jpg’, ‘folder1′ . DS . ‘folder2′ . DS . ‘image1.jpg’, etc…
- $destination_width and $destination_height are strictly positive integer
Please leave a comment, If you like this article or have one or more remark(s) on it
Great article – I am not sure it there is such a helper in cake3 though. Meantime I am downloading your code and use it – seems stratight forward. Thank you!
@Radu: Thanks! you are trying the class. I wish it will be usefull
In fact I did not touch the cake 3 (lithium) yet. But I think the integration is easy since it is just a normal class.
Very few changes makes the class resusable in even other PHP frameworks or CMS
thanks, it is very useful