Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Simple image manipulation

KentoMoriwaki edited this page Jan 25, 2014 · 1 revision

You can automatically manipulate images.

To single size

In your Eloquent model,

class FooBar extends Eloquent
{
    protected static function boot()
    {
        parent::boot();

        $observer = S3Observer::setUp('FooBar');
        // or $observer = App::make('s3-observer')->setUp('FooBar');
        $observer->setFields('profile');
        // config {YOUR_FIELD_NAME}.image to resize.
        $observer->config('profile.image', array(
            'width' => 150,
            'height' => 150
        ));
        static::observe($observer);
    }

}

And your controller or route as usual,

$foobar = FooBar::find($id);
if (Input::hasFile('profile')) {
    $foobar->profile = Input::file('profile');
}
$foobar->save();

Now your profile image is resized to 150x150 square.

To multiple sizes

You can resize image into multiple sizes.

protected static function boot()
{
    parent::boot();

    $observer = S3Observer::setUp('FooBar');
    $observer->setFields('image_full', 'image_medium', 'image_thumb');
    $observer->config('image_medium.image', array(
        'width' => 600,
    ));
    $observer->config('image_thumb.image', array(
        'width' => 100,
        'height' => 100,
    ));
    static::observe($observer);
}

and in your controller,

$foobar = FooBar::find($id);
if (Input::hasFile('image')) {
    $foobar->image_fill = $foobar->image_medium = $foobar->image_thumb = Input::file('image');
}
$foobar->save();

Configurations

Available config attributes are

$observer->config('profile.image', array(
    'width' => null,
    'height' => null,
    'callback' => function($image){}
));

If either width or height is set, a image is resized up to the width or height. If both width and height is set, a image is resized to just size in center position. (Like CSS background-size: cover;) If neighter width nor height is set, a image is never resized.

Callback is called after resized. $image is instance of Intevention/image.

Here is a example of callback turn a image into grayscale.

$observer->config('profile_gray.image', array(
    'width' => 150,
    'height' => 150,
    'callback' => function(Intevention\Image\Image $image) {
        // You don't need to call save method!
        return $image->grayscale();
    }
));
Clone this wiki locally