Hussein Harake

The Resize Helper to Resize Images On The Fly

Posted in CakePHP by harake on February 2, 2010

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

  1. 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 = …;
  2. Either in your configuration file (‘app\config\bootstrap.php’)
    • Configure::write(‘versions_store’, …);
    • Configure::write(‘versions_public’, …);
  3. 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

  1. wrap (fixed-ratio option)
    • $resize->wrap($filename, $destination_width, $destination_height);
  2. pad (complete the smaller destination box with empty pixels)
    • $resize->pad($filename, $destination_width, $destination_height, $x = 2, $y = 2);
  3. crop (rarely used, it crop-centered the smaller image)
    • $resize->crop($filename, $destination_width, $destination_height);

Remember that

  1. $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…
  2. $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

Advertisement

3 Responses

Subscribe to comments with RSS.

  1. Radu said, on February 3, 2010 at 6:27 am

    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!

  2. harake said, on February 3, 2010 at 6:58 pm

    @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

  3. Jamel said, on February 5, 2010 at 11:04 pm

    thanks, it is very useful


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.