PHP code example of stevenberg / responsible-images

1. Go to this page and download the library: Download stevenberg/responsible-images library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stevenberg / responsible-images example snippets


// 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);
}