Image Resize Middleware for Slim Framework

Overview

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.

["jpg", "jpeg", "png", "gif"], "cache" => "cache", "quality" => 90 ]));">
$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.

">

<img src="images/viper.jpg">

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

["400x200", "x200", "200x", "100x100"] ]));">
$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.

"s11kr3t" ]));">
$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);
You might also like...
🌄 Perceptual image hashing for PHP
🌄 Perceptual image hashing for PHP

ImageHash A perceptual hash is a fingerprint of a multimedia file derived from various features from its content. Unlike cryptographic hash functions

Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.

Image Optimizer This library is handy and very easy to use optimizer for image files. It uses optipng, pngquant, jpegoptim, svgo and few more librarie

:racehorse: find the size of an image without downloading the whole file. Supports batch requests.

FasterImage FasterImage finds the dimensions or filetype of a remote image file given its uri by fetching as little as needed, based on the excellent

Extract colors from an image like a human would do.

ColorExtractor Extract colors from an image like a human would do. Install Via Composer $ composer require league/color-extractor:0.3.* Usage require

php-gd based image templates
php-gd based image templates

gdaisy A highly experimental image templating system based on PHP-GD to dynamically generate image banners and covers. Installation 1. Require erikahe

 An open source image hosting service powered by Laravel
An open source image hosting service powered by Laravel

Limg An open source image hosting service powered by Laravel Features Upload your image via file, url or ShareX ! Manage your image (custom title, pub

A simple page view counter that store data as text and shows data as a PNG image

Image Counter A simple page view counter that store data as text and shows the counter as a PNG image.

Grabs the dominant color or a representative color palette from an image. Uses PHP and GD, Imagick or Gmagick.

Color Thief PHP A PHP class for grabbing the color palette from an image. Uses PHP and GD or Imagick libraries to make it happen. It's a PHP port of t

image sharing site made in PHP just for fun and freetime
image sharing site made in PHP just for fun and freetime

2bart image sharing site made in PHP just for fun and freetime To-do list: upload system [DONE] ✔️ views system [DONE] ✔️ image list system [DONE] ✔️

Comments
  • Resizing fails if Slim installed in subdirectory

    Resizing fails if Slim installed in subdirectory

    Hi, I struggled to get this working first time becasue it was looking in the wrong place for the images.

    I had to change line 53 of ImageResize.php to...

    $target   = $request->getRootUri().$request->getResourceUri();
    

    was I doing something wrong?

    opened by mulhoon 8
  • It is not working with new version of slim

    It is not working with new version of slim

    Your requirements could not be resolved to an installable set of packages.

    Problem 1 - Root composer.json requires tuupola/slim-image-resize ^0.6.1 -> satisfiable by tuupola/slim-image-resize[0.6.1]. - tuupola/slim-image-resize 0.6.1 requires slim/slim ~2.3 -> found slim/slim[2.3.0, ..., 2.6.3] but it conflicts with your root composer.json require (^4.1).

    Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

    opened by Netmisha 1
  • Does not handle filepaths with multiple '-' in them

    Does not handle filepaths with multiple '-' in them

    Filepaths such as file-name-something-400x300.jpeg group original would only match up to something

    Possible fix to allow regex group original to capture such paths involves changing the regex in DefaultMutator to the below:

    protected static $regexp = "/(?<original>[^-].+)-(?<size>(?<width>\d*)x(?<height>\d*))-?(?<signature>[0-9a-z]*)/";
    

    This matches paths in much less time than the old regex too. This is just a quick and simple fix I implemented, I am by no means a regex expert.

    opened by rigred 0
Releases(0.6.0)
Owner
Mika Tuupola
Mika Tuupola
PHP library to easily edit image with GD extension. Resize, crop, merge, draw, and many more options !

PHP Image Editor PHP library to easily edit image with GD extension. Resize, crop, merge, draw, and many more options ! ✨ Supporting ⭐ Star this repos

Franck Alary 17 Nov 13, 2022
Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file, move it to a new directory, and returns the new source for the image.

NO LONGER MAINTAINED!!! Image Cache v. 1.0.0 Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file,

Erik Nielsen 455 Dec 30, 2022
PHP library to resize, scale and crop images.

PHP library to resize, scale and crop images.

Gumlet 1.1k Jan 3, 2023
A Laravel Gravatar package for retrieving gravatar image URLs or checking the existance of an image.

Gravatar for Laravel 5.x, 6, 7 and 8 Installation First, pull in the package through Composer via the command line: composer require creativeorange/gr

Creativeorange 477 Dec 1, 2022
This plugin adds a new image style for the Core Image block.

This plugin adds a new image style for the Core Image block. Introduction How to use? Go to Gutenberg Editor and add a image block. e.g. Add new image

Mahesh Waghmare 3 Feb 17, 2022
This is an image manipulation REST API written in PHP Laravel Framework

Laravel Image Manipulation REST API Demo Here is fully working Demo: https://www.lobiimages.com/ You have to register first in order to generate acces

TheCodeholic 42 Dec 15, 2022
Image manager extension for the Yii PHP framework.

yii-imagemanager Image manager extension for the Yii PHP framework. Introduction I started this project to reduce the need for boilerplate code when w

Christoffer Niska 23 Aug 28, 2020
PHP Image Manipulation

Intervention Image Intervention Image is a PHP image handling and manipulation library providing an easier and expressive way to create, edit, and com

null 13k Jan 3, 2023
PHP 5.3 Object Oriented image manipulation library

Imagine Tweet about it using the #php_imagine hashtag. Image manipulation library for PHP 5.3 inspired by Python's PIL and other image libraries. Requ

Bulat Shakirzyanov 4.3k Jan 6, 2023
Wonderfully easy on-demand image manipulation library with an HTTP based API.

Glide Glide is a wonderfully easy on-demand image manipulation library written in PHP. Its straightforward API is exposed via HTTP, similar to cloud i

The League of Extraordinary Packages 2.4k Dec 19, 2022