lsantosc / cakephp-glide

CakePHP 3.x plugin for using Glide image manipulation library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CakePHP Glide

Build Status Coverage Status Total Downloads License

CakePHP 3.x plugin to help using Glide image manipulation library.

The plugin consists of a CakePHP dispatch filter, view helper and a Glide response class.

Installation

Install the plugin through composer:

composer require admad/cakephp-glide

Load the plugin in config/bootstrap.php above DispatcherFactory::add() calls:

Plugin::load('ADmad/Glide');

Configuration

The plugin expects config variable Glide to be set with appropriate setting. An example is shown below:

Configure::write('Glide', [
    // Value of 'serverConfig' is passed as argument to Glide's ServerFactory::create() call.
    // http://glide.thephpleague.com/1.0/config/setup/
    'serverConfig' => [
        // Path or League\Flysystem adapter instance to read images from.
        // http://glide.thephpleague.com/1.0/config/source-and-cache/
        'source' => WWW_ROOT . 'uploads/',

        // Path or League\Flysystem adapter instance to write cached images to.
        'cache' => WWW_ROOT . 'cache',

        // Optional: URL part to be omitted from source path
        // http://glide.thephpleague.com/1.0/config/source-and-cache/#set-a-base-url
        'base_url' => '/images/',

        // Optional: Response class for serving images. You normally don't need
        // to change this. By default an instance of \ADmad\Glide\Responses\CakeResponseFactory()
        // will be used.
        // http://glide.thephpleague.com/1.0/config/responses/
        'response' => null,
    ],

    // Optional: Use secure URLs to prevent URL parameter manipulation.
    // http://glide.thephpleague.com/1.0/config/security/
    'secureUrls' => true,

    // Optional: Cache duration. This makes GlideFilter set appropriate cache headers.
    'cache' => '+1 days',

    // Optional: Any response headers you may want to set
    'headers' => [
        'X-Custom' => 'some-value',
    ]
]);

Usage

Dispatcher

In your app's bootstrap setup the dispatch filter GlideFilter which intercepts requests and serves images generated by Glide. For e.g.:

DispatcherFactory::add('ADmad/Glide.Glide', ['for' => '/images']);

Ideally the above statement should be above the calls to setup dispatcher filters Asset and Routing so that GliderFilter runs before them.

The above will make CakePHP run the dispatch filter for URLs staring with /images.

For the example config shown above, for URL like domain.com/images/user/profile.jpg the source image should be under webroot/uploads/user/profile.jpg.

Note: Make sure the image URL does not directly map to image under webroot. Otherwise as per CakePHP's default URL rewriting rules the image will be served by webserver itself and request won't reach your CakePHP app.

Helper

The provided GlideHelper helps creating URLs and image tags for generating images. You can load the helper using $this->loadHelper('ADmad/Glide.Glide') in your AppView::initialize() method. Here are the available methods:

    /**
     * Creates a formatted IMG element.
     *
     * @param string $path Image path.
     * @param array $params Image manipulation parameters.
     * @param array $options Array of HTML attributes and options.
     *   See `$options` argument of `Cake\View\HtmlHelper::image()`.
     * @return string Complete <img> tag.
     */
    GlideHelper::image($path, array $params = [], array $options = [])

    /**
     * URL with query string based on resizing params.
     *
     * @param string $path Image path.
     * @param array $params Image manipulation parameters.
     * @return string Image URL.
     */
    GlideHelper::url($path, array $params = [])

The main benefit of using this helper is depending on the value of secureUrls config, the generated URLs will contain a token which will be verified by the dispatch filter. The prevents modification of query string params.

About

CakePHP 3.x plugin for using Glide image manipulation library

License:MIT License


Languages

Language:PHP 100.0%