Git Product home page Git Product logo

slim-image-resize's Introduction

Image Resize Middleware for Slim

This middleware implements automatic image resizing based on image filename.

Author Software License Build Status HHVM Status Coverage

Install

You can install latest version using composer.

$ composer require tuupola/slim-image-resize

Configuration

Configuration options are passed as an array. There are no mandatory parameters.

$app = new \Slim\Slim();
$app->add(new Slim\Middleware\ImageResize());

You can configure the allowed image extensions and cache folder. Cache folder must be writable by webserver process. Image quality applies only for jpg images. Example options shown below are also the default options used by the middleware.

$app = new \Slim\Slim();
$app->add(new Slim\Middleware\ImageResize([
    "extensions" => ["jpg", "jpeg", "png", "gif"],
    "cache" => "cache",
    "quality" => 90
]));

Caching

For caching to work you also must add the following to your .htaccess file. These rules should be added before Slim rewrite rules. Folder name must be the same you passed in as middleware configuration option. With caching rewrite rules in place only first request is served by PHP. All subsequent requests are served with static file from cache folder.

# Check for cached image in cache folder.
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{DOCUMENT_ROOT}/cache/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /cache/$1 [L,QSA]

If your Slim application is installed in to a subfolder use the following rewrite rule instead. This example assumes the subfolder is called example.

RewriteBase /example

# Check for cached image in cache folder.
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{DOCUMENT_ROOT}/example/cache/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /example/cache/example/$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Usage

With middleware configured you can create different sizes of images by altering the filename.

<!-- This is the original image -->
<img src="images/viper.jpg">
<!-- Images below will be resized -->
<img src="images/viper-400x200.jpg">
<img src="images/viper-x200.jpg">
<img src="images/viper-200x.jpg">
<img src="images/viper-100x100.jpg">

HTML above will produce the following images.

Original 400x200 x200 200x 100x100

Security

By default it is possible to create any size image. If images are also cached you should restrict which sizes middleware is allowed to create. Otherwise it is possible to make requests arbitary number of different sizes of images.

$app = new \Slim\Slim();
$app->add(new Slim\Middleware\ImageResize([
    "sizes" => ["400x200", "x200", "200x", "100x100"]
]));

If you have arbitary number of different sizes it is also possible to sign images with secret key.

$app->add(new Slim\Middleware\ImageResize([
    "secret" => "s11kr3t"
]));

You must include the signature in the image name.

<img src="images/viper-400x200-175ecbf97b7faebb.jpg">

Signature for above image was generated with following code.

$sha1 = sha1("400x200:s11kr3t");
$signature = substr($sha1, 0, 16);

slim-image-resize's People

Contributors

largo avatar tuupola avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.