Images

Introduction

The Streams platform comes with a fluid and highly extensible image handling and manipulation tool that leans heavily on the fantastic Intervention Image.

Reading Images

To get started, use the Images facade to create a new image for working with.

use Streams\Core\Support\Facades\Images;

$image = Images::make('img/foo.jpg');

The facade is aliased for use in views as well:

{!! Images::make('resources/img/foo.jpg') !!}

Image Sources

The first and only argument should be the source image to display. The following sources are supported out of the box:

Paths in the Filesystem

Any image path relative to the application root may be used.

{!! Images::make('resources/img/foo.jpg') !!}

Configured Storage Disks

You may use any configured storage location as an image source.

{!! Images::make('public://img/foo.jpg') !!}

If the file is not found relative to the base path of your application, the default public disk will be attempted.

{!! Images::make('img/foo.jpg') !!}

Remote URLs

The URL of a remote image may also be used. The allow_url_fopen PHP directive must be enabled to use remote image sources.

{!! Images::make('https://example.com/img/foo.jpg') !!}

Remote images are cached locally. To use remote images without caching locally just use regular <img> tags.

Named Images

@todo Finish

Registering Images

Specifying Named Images

Editing Images

After you initiat a new image instance with Images::make(), you can use the whole palette of manipulation methods on the instance.

Modification methods return the image instance, so you are able to chain methods.

use Streams\Core\Support\Facades\Images;

$image = Images::make('img/foo.jpg')->fit(300, 500)->quality(60);
{!! Images::make('resources/img/foo.jpg')->fit(300, 500)->quality(60) !!}

Resizing Images

Use the following methods to resize images.

Adjusting Images

Use the following methods to adjust various aspects of images.

quality()

Additionally, you may use the quality method to adjust the quality alone of JPG images.

Images::make('img/foo.jpg')->quality(60);

Applying Effects

Use the following methods to apply effects to images.

Drawing

Use the following methods to draw on images.

Macros

Macros are a basic method of extending the Streams platform.

Defining Macros

You can define macros in a service provider.

use Streams\Core\Image\Image;

Image::macro('thumbnail', function () {
    return $this->fit(148)->encode('jpg', 50);
});

Applying Macros

$thumbnail = Images::make('img/foo.jpg')->thumbnail();

Outputting Images

Use output methods to display image data from an image object. The img method is used by default.

img()

Use the img method to return an <img> tag.

{!! Images::make('img/foo.jpg') !!}

The first parameter can be an alt tag or array of attributes. If an alt tag is provided, the attributes can still be provided as a second parameter. Note this is the default output method when used in Blade.

Images::make('img/foo.jpg')->img('Foo Bar Image', ['width' => '100'])

Note that unmatched methods will pass through to set attribute values.

Images::make('img/foo.jpg')->width(100)->img('Foo Bar Image')

url()

Use the url method to output a URL to the image. The first argument may be an array of query string parameters to append. The second argument can be used to force secure URLs. If not specified, the URLs will use the protocol of the request. If

Images::make('img/foo.jpg')->url()

// Append a manual version query parameter.
Images::make('img/foo.jpg')->url(['version' => 'v1'])

inline()

Use the inline method to return an <img> tag with a base64 encoded src.

{!! Images::inline('img/foo.jpg') !!}

The first parameter can be an alt tag or array of attributes. If an alt tag is provided, the attributes can still be provided as a second parameter. Note this is the default output method when used in Blade.

Images::make('img/foo.jpg')->inline('Foo Bar')

base64()

Use the base64 method to return a base64 encoded string.

<img src="{!! Images::make('img/foo.jpg')->base64() !!}">

css()

Use the css method to return a url() string for use in CSS backgrounds.

<div style="background: {!! Images::make('img/foo.jpg')->css() !!};">

data()

The data method will return the contents of the image as a string.

echo Images::make('img/foo.jpg')->data()

Responsive Images

srcsets()

Images::make('img/foo.jpg')
    ->srcsets([
        '1x' => [
            'resize'  => 400,
            'quality' => 60
        ],
        '2x' => [
            'resize'  => 800,
            'quality' => 90
        ],
        '640w' => [
            'resize'  => 800,
            'quality' => 90
        ]
    ]);

sources()

Images::make('img/foo.jpg')
    ->srcsets([
        '(min-width: 600px)' => [
            'resize'  => 400,
            'quality' => 60
        ],
        '(min-width: 1600px)' => [
            'resize'  => 800,
            'quality' => 90
        ],
        'fallback' => [
            'resize'  => 1800
        ]
    ]);

picture()

Images::make('img/foo.jpg')
    ->sources([
        '(min-width: 600px)' => [
            'resize'  => 400,
            'quality' => 60
        ],
        '(min-width: 1600px)' => [
            'resize'  => 800,
            'quality' => 90
        ],
        'fallback' => [
            'resize'  => 1800
        ]
    ])->picture();