imponeer / smarty-image
Smarty plugin that adds some image related template syntax enchantments
Installs: 20 029
Dependents: 1
Suggesters: 1
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^8.3
- ext-gd: *
- ext-json: *
- intervention/image: ^3
- psr/cache: ^1.0|^2.0|^3.0
- smarty/smarty: ^5.0
Requires (Dev)
- bentools/cartesian-product: ^1.3
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.7
- symfony/cache: ^5.0|^6.0|^7.0
- symfony/dom-crawler: ^5.0|^6.0|^7.0
- symfony/polyfill-php80: ^1.27
This package is auto-updated.
Last update: 2025-06-16 23:25:29 UTC
README
Smarty Image
A modern Smarty extension that provides image resizing capabilities with built-in caching. This extension allows you to resize images directly from your Smarty templates using the resized_image
function.
Installation
To install and use this package, we recommend to use Composer:
composer require imponeer/smarty-image
Otherwise, you need to include manually files from src/
directory.
Setup
Basic Setup
For Smarty v5 and newer, use the new extension system:
$smarty = new \Smarty\Smarty(); // For $psrCacheAdapter value use PSR-6 cache adapter, for example Symfony\Component\Cache\Adapter\ArrayAdapter $smarty->addExtension( new \Imponeer\Smarty\Extensions\Image\SmartyImageExtension($psrCacheAdapter) );
For older Smarty use v2.0 version of this plugin.
Using with Symfony Container
When using Symfony's dependency injection container, you can register the extension as a service:
# config/services.yaml services: Imponeer\Smarty\Extensions\Image\SmartyImageExtension: arguments: $cache: '@cache.app' tags: - { name: 'smarty.extension' }
Then inject it into your Smarty instance:
// In your controller or service public function __construct( private Smarty $smarty, private SmartyImageExtension $imageExtension ) { $this->smarty->addExtension($this->imageExtension); }
Using with PHP-DI
With PHP-DI container, configure the extension in your container definitions:
use Psr\Cache\CacheItemPoolInterface; use Imponeer\Smarty\Extensions\Image\SmartyImageExtension; use Symfony\Component\Cache\Adapter\FilesystemAdapter; return [ CacheItemPoolInterface::class => \DI\create(FilesystemAdapter::class), SmartyImageExtension::class => \DI\create() ->constructor(\DI\get(CacheItemPoolInterface::class)), Smarty::class => \DI\factory(function (SmartyImageExtension $imageExtension) { $smarty = new Smarty(); $smarty->addExtension($imageExtension); return $smarty; }) ];
Using with League Container
With League Container, register the services like this:
use League\Container\Container; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Imponeer\Smarty\Extensions\Image\SmartyImageExtension; $container = new Container(); $container->add(CacheItemPoolInterface::class, ArrayAdapter::class); $container->add(SmartyImageExtension::class) ->addArgument(CacheItemPoolInterface::class); $container->add(Smarty::class) ->addMethodCall('addExtension', [SmartyImageExtension::class]);
Usage
To resize images from Smarty templates, you can use the resized_image
function:
{resized_image file="/images/image.jpg" height=70}
This function supports such arguments:
Argument | Required | Default value | Description |
---|---|---|---|
file |
yes | Image file to resize | |
width |
if height is not specified |
Resized image width | |
height |
if width is not specified |
Resized image height | |
fit |
no | outside |
Method used for resize. Supported fill , inside , outside |
href or link |
no | if specified and return is set to image , will output generated HTML as image with link to this specific location |
|
basedir |
no | $_SERVER['DOCUMENT_ROOT'] | Base dir where to look for image files |
return |
no | image |
Returns result as HTML tag if value is image , or as resized image URI if value is url . |
All extra arguments will be rendered into image tag, if return mode is image
.
Development
This project uses modern PHP development tools and practices:
Running Tests
composer test
Code Style
The project follows PSR-12 coding standards. Check code style with:
composer phpcs
Fix code style issues automatically:
composer phpcbf
Static Analysis
Run PHPStan for static code analysis:
composer phpstan
Documentation
API documentation is automatically generated and available in the project's wiki. For more detailed information about the classes and methods, please refer to the project wiki.
How to contribute?
We welcome contributions! If you want to add functionality or fix bugs:
- Fork the repository
- Create a feature branch from
main
- Make your changes following the coding standards
- Add or update tests as needed
- Run the test suite to ensure everything works
- Submit a pull request with a clear description of your changes
For bug reports or feature requests, please use the issues tab and provide as much detail as possible.