Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A PHP image framework to stack "on the fly" optimized thumbnail

License

NotificationsYou must be signed in to change notification settings

quazardous/ImageStack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A PHP image serving framework.

The main goal is to provide a robust framework to create an "on the fly" image thumbnailer generator similar toimagecache /image style inDrupal.

This project is only the framework part. The framework is brick designed so that you can put together your own image serving project and add special bricks.

Framework integration:

Installation

composer require quazardous/imagestack

Concept

Typical flow

This describes the typical processing we want to achieve.

When thefront controller handles an image HTTP request:

  • thefront controller will callimage stack with theimage path.
  • theimage stack will fetch the image using theimage backend.
  • theimage stack will applyimage manipulator.
  • theimage stack will store the image using thestorage backend.
  • theimage stack will return the image to thefront controller.
  • thefront controller will serve the image.

Ideally the image file will be stored so that the next HTTP request will be statically served.

Lexicon / bricks

Image path

The path of the image that could come from the front controller.

SeeImageStack\Api\ImagePathInterface.

Image

The image content we are willing to serve.

SeeImageStack\Api\ImageInterface.

Image backend

Something that can provide image content.

SeeImageStack\Api\ImageBackendInterface.

Image manipulator

Service that can modify/transform/optimize the image content.

SeeImageStack\Api\ImageManipulatorInterface.

Storage backend

Something where we can store the image content.
Typically we could store it onto the file system so that next HTTP query will be statically serve.

SeeImageStack\Api\StorageBackendInterface.

Image stack

Top of the iceberg class.

SeeImageStack\Api\ImageStackInterface.

Usage

Code example

This pseudo controller creates an image stack that will:

  • fetch image fromhttps://images.example.com/backend/ +$path
  • optimize it withjpegtran
  • store it on'/var/www/my/local/image/storage/' +$path
  • serve it
functionmyImageController($path){$stack =new \ImageStack\ImageStack(new \ImageStack\ImageBackend\HttpImageBackend('https://images.example.com/backend/'),new \ImageStack\StorageBackend\FileStorageBackend('/var/www/my/local/image/storage/'));$oim =new \ImageStack\ImageManipulator\OptimizerImageManipulator();$oim->registerImageOptimizer(new \ImageStack\ImageOptimizer\JpegtranImageOptimizer());$stack->addImageManipulator($oim);$image =$stack->stackImage(new \ImageStack\ImagePath($path));return$image->getBinaryContent();}

Implementation

Image stack

A basic image stack.

SeeImageStack\ImageStack

Image backends

File image backend

SeeImageStack\ImageBackend\FileImageBackend

HTTP image backend

SeeImageStack\ImageBackend\HttpImageBackend

Cache image backend

Add a cache layer before an image backend.

SeeImageStack\ImageBackend\CacheImageBackend

Sequential image backend

Sequentially fetch image from a queue of image backends and return the first match.

SeeImageStack\ImageBackend\SequentialImageBackend

Image manipulators

Converter image manipulator

Convert image type.

SeeImageStack\ImageManipulator\ConverterImageManipulator

Optimizer image manipulator

Optimize image.

SeeImageStack\ImageManipulator\OptimizerImageManipulator

jpegtran wrapper, seeImageStack\ImageOptimizer\JpegtranImageOptimizer.

pngcrush wrapper, seeImageStack\ImageOptimizer\PngcrushImageOptimizer.

Thumbnailer image manipulator

Create an image thumbnail with (path) rules.

SeeImageStack\ImageManipulator\ThumbnailerImageManipulator

Path pattern rule, seeImageStack\ImageManipulator\ThumbnailRule\PatternThumbnailRule

You can associate path patterns to thumbnail formats:

useImageStack\ImageManipulator\ThumbnailerImageManipulator;useImagine\Gd\Imagine;useImageStack\ImagePath;useImageStack\ImageManipulator\ThumbnailRule\PatternThumbnailRule;...$rules = ['|^images/styles/big/|' =>'<500x300',// resize to fit in 500x300 box'|^images/styles/box/|' =>'200x200',// resize/crop to 200x200 box'|^images/styles/list/|' =>'100',// resize/crop to 100x100 box'|^images/([0-9]+)x([0-9]+)/|' =>function ($matches) {return"{$matches[1]}x{$matches[2]}"; },// custom resize/crop'|.*|' =>false,// trigger an image not found exception if nothing matches];$tim =newThumbnailerImageManipulator(newImagine());foreach ($rulesas$pattern =>$format) {$tim->addThumbnailRule(newPatternThumbnailRule($pattern,$format));}// this will resize the given image to fit in a 500x300 box$tim->manipulateImage($image,newImagePath('images/styles/big/photo.jpg'));// this will resize/crop the given image to a 200x200 box$tim->manipulateImage($image,newImagePath('images/200x150/photo.jpg'));// this will rise a 404$tim->manipulateImage($image,newImagePath('bad/path/photo.jpg'));
Watermark image manipulator

Add watermark to images.

SeeImageStack\ImageManipulator\WatermarkImageManipulator

useImageStack\ImageManipulator\WatermarkImageManipulator;useImagine\Gd\Imagine;useImageStack\ImagePath;...// repeat the watermark$wim =newWatermarkImageManipulator(newImagine(),'/path/to/little-watermark.png', ['repeat' => WatermarkImageManipulator::REPEAT_ALL,]);$wim->manipulateImage($image,newImagePath('protected_image.jpg'));// reduce and anchor a big the watermark$wim =newWatermarkImageManipulator(newImagine(),'/path/to/huge-watermark.png', ['reduce' => WatermarkImageManipulator::REDUCE_INSET,'anchor' => WatermarkImageManipulator::ANCHOR_BOTTOM|WatermarkImageManipulator::ANCHOR_RIGHT,]);$wim->manipulateImage($image,newImagePath('protected_image.jpg'));

Tests

git clone git@github.com:quazardous/ImageStack.gitcd ./ImageStackcp tests/config-dist.php tests/config.php

Edit/adapttests/config.php.

composer.phar update -devphpunit.phar

Dependencies / support

Tested againstPHP 7.1.

Current implementation usesImagine.

You will need eitherphp-gd,php-gmagick orphp-imagick.

Wrappers for external optimizer:

History

  • 1.5: add animated GIFs support in thumbnail and watermark (Imagick only)
  • 1.4: add optimize gifsicle
  • 1.3: allow backend to have image manipulator
  • 1.2: add watermark
  • 1.1: add path rules
  • 1.0: current version
  • 0.9: pre version mainly a raw copy/paste from an old project

Credits

quazardous.

License

MIT

About

A PHP image framework to stack "on the fly" optimized thumbnail

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp