imponeer/smarty-image

Smarty plugin that adds some image related template syntax enchantments

v3.0.0 2025-06-16 23:21 UTC

README

License GitHub release PHP Packagist Smarty version requirement

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:

  1. Fork the repository
  2. Create a feature branch from main
  3. Make your changes following the coding standards
  4. Add or update tests as needed
  5. Run the test suite to ensure everything works
  6. 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.