cakephp-glide 0,0 editorconfig scrutinizer styleci travis-ci Packagist phpunit

CakePHP 3.x plugin for using Glide image manipulation library

3 years after

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 middlware, view helper and a Glide response class.

Requirements

  • CakePHP 3.3+ (For 3.2 and below use 1.x)

Installation

Install the plugin through composer:

composer require admad/cakephp-glide

Load the plugin in config/bootstrap.php:

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

Usage

Middleware

In your Application::middleware() setup the GlideMiddleware which intercepts requests and serves images generated by Glide. For e.g.:

$middleware->push(new ADmad\Glide\Middleware\GlideMiddleware([
    // Run this filter only for URLs matching specified value. If unset the
    // value of `server.base_url` config mentioned below will be used.
    // http://book.cakephp.org/3.0/en/development/dispatch-filters.html#conditionally-applying-filters
    'scope' => null,

    // Either an instance of League\Glide\Server or config array to be used to
    // create server instance.
    // http://glide.thephpleague.com/1.0/config/setup/
    'server' => [
        // 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',

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

        // Response class for serving images. If unset (default) an instance of
        // \ADmad\Glide\Responses\PsrResponseFactory() will be used.
        // http://glide.thephpleague.com/1.0/config/responses/
        'response' => null,
    ],

    // http://glide.thephpleague.com/1.0/config/security/
    'security' => [
        // Boolean indicating whether secure URLs should be used to prevent URL
        // parameter manipulation. Default false.
        'secureUrls' => false,

        // Signing key used to generate / validate URLs if `secureUrls` is `true`.
        // If unset value of Cake\Utility\Security::salt() will be used.
        'signKey' => null,
    ],

    // If set to `true` exception generated when trying to get image from Glide
    // will be ignored and request will be allowed to proceed. Default false.
    'ignoreException' => false,

    // Cache duration. Default '+1 days'.
    'cacheTime' => '+1 days',

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

Ideally the above statement should be above the calls to setup middlewares for AssetMiddleware and RoutingMiddleware so that GlideMiddleware runs before them.

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 in your AppView::initialize() as shown in example below:

public function initialize()
{
    // All option values should match the corresponding options for `GlideFilter`.
    $this->loadHelper('ADmad/Glide.Glide', [
        // Base URL.
        'baseUrl' => '/images/',
        // Whether to generate secure URLs.
        'secureUrls' => false,
        // Signing key to use when generating secure URLs.
        'signKey' => null,
    ]);
}

Here are the available methods of GlideHelper:

    /**
     * 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.

Related Repositories

awesome-php

awesome-php

A curated list of amazingly awesome PHP libraries, resources and shiny things. ...

awesome-cakephp

awesome-cakephp

A curated list of amazingly awesome CakePHP plugins, resources and shiny things. ...

awesome-php-zh_CN

awesome-php-zh_CN

一个PHP资源列表,内容包括:库、框架、模板、安全、代码分析、日志、第三方库、配置工具、Web 工具、书籍、电子书、经典博文等等 ...

awesome-php

awesome-php

A curated list of amazingly awesome PHP libraries, resources and shiny things. ...

awesome-php

awesome-php

A curated list of amazingly awesome PHP libraries, resources and shiny things. ...


Top Contributors

ADmad Spriz josegonzalez

Dependencies

package version
cakephp/cakephp ~3.3
league/glide ~1.1
dev phpunit/phpunit <6.0

Releases

-   2.0.0 zip tar
-   1.0.3 zip tar
-   1.0.2 zip tar
-   1.0.1 zip tar
-   1.0.0 zip tar