mapado / image-url-builder
Generate a full url from an image slug
Installs: 7 976
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
Requires (Dev)
- mapado/php-cs-fixer-config: ^2.1
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.3
- twig/twig: ^2.4 || ^3.0.5
Suggests
- twig/twig: For twig integration via the Mapado\ImageUrlBuilder\Twig\UrlBuilderExtension extension
README
Generate a full url from an image slug
Installation
composer require mapado/image-url-builder
Usage
use Mapado\ImageUrlBuilder\Builder; $builder = new Builder(); $width = 800; $height = 600; $url = $builder->buildUrl('2018/01/foo.jpg', $width, $height); // will output '//img.mapado.net/2018/01/foo_thumbs/800-600.jpg'
The first parameter of the buildUrl
function accept an image "slug" or a full image url (starting with https://img.mapado.net/
)
Force http(s) prefix
If you want to force http prefix, you can use the withHttpPrefix()
or withHttpsPrefix()
function before :
$httpUrl = $builder ->withHttpPrefix() ->buildUrl($slug, $width, $height); // will output `http://img.mapado.net/xxxx...` $httpsUrl = $builder ->withHttpsPrefix() ->buildUrl($slug, $width, $height); // will output `https://img.mapado.net/xxxx...`
With Twig
A Twig extension is available : Mapado\ImageUrlBuilder\Twig\UrlBuilderExtension
You need to inject an instance of Mapado\ImageUrlBuilder\Builder
to the constructor.
If you are using Symfony, the following configuration do work fine:
services: Mapado\ImageUrlBuilder\Builder: ~ Mapado\ImageUrlBuilder\Twig\UrlBuilderExtension: tags: - { name: twig.extension }
You can use the filter like this:
<img src="{{ imageSlug|imageUrl(width, height) }}" />