- Notifications
You must be signed in to change notification settings - Fork0
A PHP image framework to stack "on the fly" optimized thumbnail
License
quazardous/ImageStack
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
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.
- Symfony 4 bundle:Imagestack Bundle
- Silex provider:SilexImageStack
composer require quazardous/imagestack
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.
The path of the image that could come from the front controller.
SeeImageStack\Api\ImagePathInterface
.
The image content we are willing to serve.
SeeImageStack\Api\ImageInterface
.
Something that can provide image content.
SeeImageStack\Api\ImageBackendInterface
.
Service that can modify/transform/optimize the image content.
SeeImageStack\Api\ImageManipulatorInterface
.
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
.
Top of the iceberg class.
SeeImageStack\Api\ImageStackInterface
.
This pseudo controller creates an image stack that will:
- fetch image from
https://images.example.com/backend/
+$path
- optimize it with
jpegtran
- 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();}
A basic image stack.
SeeImageStack\ImageStack
SeeImageStack\ImageBackend\FileImageBackend
SeeImageStack\ImageBackend\HttpImageBackend
Add a cache layer before an image backend.
SeeImageStack\ImageBackend\CacheImageBackend
Sequentially fetch image from a queue of image backends and return the first match.
SeeImageStack\ImageBackend\SequentialImageBackend
Convert image type.
SeeImageStack\ImageManipulator\ConverterImageManipulator
Optimize image.
SeeImageStack\ImageManipulator\OptimizerImageManipulator
jpegtran
wrapper, seeImageStack\ImageOptimizer\JpegtranImageOptimizer
.
pngcrush
wrapper, seeImageStack\ImageOptimizer\PngcrushImageOptimizer
.
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'));
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'));
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
Tested againstPHP 7.1.
Current implementation usesImagine.
You will need eitherphp-gd
,php-gmagick
orphp-imagick
.
Wrappers for external optimizer:
- 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
MIT
About
A PHP image framework to stack "on the fly" optimized thumbnail