If you want to use SleepingOwl Admin image column or form element you must prepare your model:

  • Implement SleepingOwl\Models\Interfaces\ModelWithImageFieldsInterface in your model.
  • Add SleepingOwl\Models\Traits\ModelWithImageOrFileFieldsTrait trait (if your model extends SleepingOwlModel you dont have to add the trait).
  • Add public function getImageFields().

Example

<?php

use SleepingOwl\Models\Interfaces\ModelWithImageFieldsInterface;
use SleepingOwl\Models\Traits\ModelWithImageOrFileFieldsTrait;

class Monument extends \Eloquent implements ModelWithImageFieldsInterface
{
    use ModelWithImageOrFileFieldsTrait;

    public function getImageFields()
    {
        return [
            'image' => 'monuments/',
            'photo' => '',
            'other' => ['other_images/', function($directory, $originalName, $extension)
            {
                return $originalName;
            }]
        ];
    }
}

Provide Image Fields

getImageFields() must return array, where keys is model image fields and values is path to directory within imagesDirectory (see configuration).

If path is empty images will be stored in imagesDirectory.

Custom File Naming Function

Array value can be either string or array:

  • string — directory to upload images to, filename generates randomly.
  • array — first item in array is directory to upload images to, second item is naming function closure (function($directory, $originalName, $extension){}). Closure must return new filename for the uploaded file.
    • $directory — absolute path to the upload directory
    • $originalName — uploaded file original name
    • $extension — uploaded file extension

Image Cropping and Resizing

You can change uploaded image by adding method setImage($field, $image) to your model:

public function setImage($field, $image)
{
    parent::setImage($field, $image);
    $file = $this->$field;
    if ( ! $file->exists()) return;
    $path = $file->getFullPath();

    // you can use Intervention Image package features to change uploaded image
    Image::make($path)->resize(10, 10)->save();
}

Field Usage

Accessing fields of your model

$monument->image

will return instance of SleepingOwl\Models\Attributes\Image.

You can use

$monument->image->thumbnail('original')

to get frontend url to image with original size.

You can create your own image resizing templates in app/config/packages/intervention/imagecache/config.php. For details see Intervention Imagecache.