Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email: hello@sanisoft.com

Blog

[CakePHP] How to re-size images at run-time

This is a helper + controller combo for CakePHP which I often use. It helps to re-size images at run-time and also aid display them. Oh yes! once the image is resized it also caches them.

First let’s look at helper method.

What above code does? If re-sized version of image already available then return it otherwise ‘/images/resize’ page will handle re-sizing.

So, how to use this? Set ‘My’ in $helpers array of your application controller or the needed controller and use it in view like

Now, you will wonder what ’75-s’ is. This is my way of handling various re-sizing methods, ’75-s’ means resize+crop to 75×75 (square) size. Also it has been assumed that ‘test.png’ exists in ‘[WWW_ROOT]/files/avatars’ directory.

Now let’s move to re-size part. The basic controller/action code contains

which re-sizes the image properly (thanks to Coppermine Photo Gallery from which I borrowed re-sizing code).

That’s all. Please try and let me know how it worked for you.

Thanks

This code is best used when you are not really sure about the target size of the images, even with caching this is more CPU expensive than linking directly to resized images

About the Author

Amit Badkas is Zend certified PHP5 and Zend Framework engineer, and has been working in SANIsoft for past 10 years, his present designation is 'Technical Manager'

7 comments

  1. Pingback: Tweets that mention [CakePHP] How to re-size images at run-time at SANIsoft – PHP for E Biz -- Topsy.com

  2. Pingback: CakePHP : signets remarquables du 26/08/2010 au 02/09/2010 | Cherry on the...

  3. Pingback: [How To] Use of image magick instead of GD for run-time image re-sizing at SANIsoft – PHP for E Biz

  4. Pingback: jCrop Demo using PHP at SANIsoft – PHP for E Biz

Leave a Reply