stevenberg / responsible-images
A PHP library for generating responsive image URLs and HTML tags
Installs: 1 162
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- php-ds/php-ds: ^1.5
Requires (Dev)
- laravel/pint: ^1.17.1
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^1.11.8
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-phpunit: ^1.4
- phpstan/phpstan-strict-rules: ^1.6
- phpunit/phpunit: ^11.2.9
- psy/psysh: ^0.12.4
- roave/security-advisories: dev-master
README
A PHP for generating responsive image URLs and HTML tags.
Installation
Install with composer:
composer require stevenberg/responsible-images
Use Cloudinary images with stevenberg/responsible-images-cloudinary:
composer require stevenberg/responsible-images-cloudinary
Example
Here's an example of using the library in a Laravel project:
// app/Providers/ResponsibleImagesServiceProvider.php namespace App\Providers; use Illuminate\Support\ServiceProvider; use StevenBerg\ResponsibleImages\Urls\Cloudinary; use StevenBerg\ResponsibleImages\Urls\Maker; class ResponsibleImagesServiceProvider extends ServiceProvider { public function boot() { Maker::registerDefaultMaker(new Cloudinary); } }
// app/Image.php namespace App; use Illuminate\Database\Eloquent\Model; use StevenBerg\ResponsibleImages\Image as ResponsibleImage; use StevenBerg\ResponsibleImages\Urls\Cloudinary; use StevenBerg\ResponsibleImages\Values\Gravity; use StevenBerg\ResponsibleImages\Values\Shape; class Image extends Model { protected $fillable = ['gravity', 'name', 'shape']; public function getGravityAttribute($value) { return Gravity::from($value); } public function setGravityAttribute(Gravity $value) { $this->attributes['gravity'] = $value->value; } public function getShapeAttribute($value) { return new Shape($value); } public function setShapeAttribute(Shape $value) { $this->attributes['shape'] = $value->value; } public function getResponsiveImageAttribute() { return ResponsibleImage::fromShape( $this->shape, $this->name, ['gravity' => $this->gravity] ); } }
// app/helpers.php use App\Image; use StevenBerg\ResponsibleImages\SizeRange; function responsive_image_tag( Image $image, int $min, int $max, int $step, array $attributes) { $range = SizeRange::from($min, $max, $step); echo $image->responsive_image->tag($range, $range->first(), $attributes); }