mapado / image-url-builder
从图像别名生成完整URL
v2.3.0
2024-09-22 13:55 UTC
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
从图像别名生成完整URL
安装
composer require mapado/image-url-builder
使用
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'
buildUrl
函数的第一个参数接受一个图像 "别名" 或完整的图像URL(以 https://img.mapado.net/
开头)
强制http(s)前缀
如果您想强制使用http前缀,可以在 withHttpPrefix()
或 withHttpsPrefix()
函数之前使用
$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...`
使用Twig
有一个可用的Twig扩展:Mapado\ImageUrlBuilder\Twig\UrlBuilderExtension
您需要在构造函数中注入 Mapado\ImageUrlBuilder\Builder
的一个实例。
如果您正在使用Symfony,以下配置可以正常工作
services: Mapado\ImageUrlBuilder\Builder: ~ Mapado\ImageUrlBuilder\Twig\UrlBuilderExtension: tags: - { name: twig.extension }
您可以这样使用过滤器
<img src="{{ imageSlug|imageUrl(width, height) }}" />