https://github.com/mapado/image-url-builder
Generate a full url from an image slug
https://github.com/mapado/image-url-builder
Last synced: about 1 year ago
JSON representation
Generate a full url from an image slug
- Host: GitHub
- URL: https://github.com/mapado/image-url-builder
- Owner: mapado
- License: mit
- Created: 2018-07-11T09:10:20.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2024-09-22T14:00:36.000Z (over 1 year ago)
- Last Synced: 2025-04-11T14:49:53.473Z (about 1 year ago)
- Language: PHP
- Size: 71.3 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# image-url-builder
Generate a full url from an image slug
## Installation
```sh
composer require mapado/image-url-builder
```
## Usage
```php
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 :
```php
$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:
```yaml
services:
Mapado\ImageUrlBuilder\Builder: ~
Mapado\ImageUrlBuilder\Twig\UrlBuilderExtension:
tags:
- { name: twig.extension }
```
You can use the filter like this:
```twig
```