2017 © Pedro Peláez
 

cakephp-plugin imagine

image

simkimsia/imagine

  • Saturday, April 13, 2013
  • by kimsia
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 28 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Imagine Plugin for CakePHP

Setup

Add the external Imagine Lib

You need to init the git submodule of imagine, (*1)

git submodule update --init

Or get it from https://github.com/avalanche123/Imagine, (*2)

Copy Imagine into the plugins vendor folder Vendor/Imagine, the root of the Imagine package should be inside this folder. Vendor/Imagine/README.md should be present if you placed the code correctly., (*3)

Salt

You need to configure a salt for Imagine security functions., (*4)

Configure::write('Imagine.salt', 'your-salt-string-here');

We do not use Security.salt on purpose because we do not want to use the same salt here for security reasons., (*5)

Load plugin

Load the imagine plugin in your bootstrap file, remember to use plugin bootstrap, like this, (*6)

CakePlugin::load('Imagine' => array('bootstrap' => true));

Imagine Helper

The helper will generate image urls with named params to get thumbnails or whatever else operation is wanted and a hashes the url., (*7)

The hash can be checked using the Imagine Component to avoid that people try to bring your page down by incrementing the size of a requested thumbnail to generate thousands of images on your server., (*8)

$url = $this->Imagine->url(
    array(
        'controller' => 'images',
        'action' => 'display',
        1),
    array(
        'thumbnail' => array(
            'width' => 200,
            'height' => 150)));
echo $this->Html->image($url);

Special note for high traffic sites

You should not generate images on the fly on high traffic sites, it might get your server locked up because of the many many requests!, (*9)

The first request will hit your server and start generating the image while others try to do that at the same time causing the site become locked up in the worst case., (*10)

It is better to generate the needed versions after an image was uploaded and if other versions are needed later, generate them by a shell script., (*11)

For this purpose there is a method in the ImagineBehavior that will turn the image operation array into a string, see ImagineBehavior::paramsAsFilestring();, (*12)

Suffix your image with the string generated by this method to be able to batch delete a file that has versions of it cached. The intended usage of this is to store the files as my_horse.thumbnail+width-100-height+100.jpg for example., (*13)

So after upload store your image meta data in a db, give the filename the id of the record and suffix it with this string and store the string also in the db. In the views, if no further control over the image access is needed, you can simply link the image like $this->Html->image('/images/05/04/61/my_horse.thumbnail+width-100-height+100.jpg'); directly., (*14)

Imagine Component

The Imagine component does the following:, (*15)

  • If configured for actions it validates automatically if a valid hash from ImagineHelper was passed as named param within the url
  • Gets the hash from the url: getHash()
  • Validates the has based on the named params: checkHash()
  • Automatically unpacks the packed named args: unpackParams()

Imagine Behavior

The behavior interacts with the component and will process a given image file with a set of operations that should be applied to it. See ImagineBehavior::processImage()., (*16)

Imagine instance

Makes an Imagine instance available to the model. Get it by calling, (*17)

$this->imagineObject();

or directly through the behavior, (*18)

$this->Behaviors->Imagine->Imagine

Caching and Storage

This plugin does not take care of how you store the images or how you cache them but it will offer you some helping methods for caching images based on a hash or a unique string., (*19)

This is a design decision that was made because everyone likes to implement the file storage a little different. So it is up to you how you store the generated images., (*20)

The Versions

13/04 2013

dev-master

9999999-dev

  Sources   Download

The Requires

 

13/04 2013

dev-develop

dev-develop

  Sources   Download

The Requires