Getting files and thumbnails

Media Module

Getting files

Thanks to the MediaRelation trait it's easy to find the files linked to your entities.

The MediaRelation trait has 2 main methods for you to leverage. files() and filesByZone(). Both are a morphToMany relation, the second being easier to use to day to day usage.

Let's say you have products, that have a featured images. You want an easy way to grab those images. The long way would be something like this:

// In a controller
$images = $product->files()->where('zone', 'featured_images')->get();
// Same as
$images = $product->filesByZone('featured_images')->get();

This is fine, but isn't really pretty to use in views. To avoid this issue, we can leverage laravel's accessor feature. Let's add one on our Product entity.

class Product extends Model
    public function getFeaturedImagesAttribute()
        return $this->filesByZone('featured_images')->get();

With this you can now call this as an attribute: $product->featured_images.

Of course, this is not limited to images, but any type of file. This can also be a single file, by using first() instead of get();


MediaPath is a value object that will be returned when you get a file from the previous methods. Usually you won't even notice.

We want to display our product images:

// In a blade view
@foreach($products as $product)
    @foreach($product->featured_images as $featuredImage)
        <img src="{{ $featuredImage->path }}" alt=""/>

If a product had a single file, lets say a specification pdf:

<a href="{{ $product->specification_pdf->path }}">Download pdf</a>

Getting thumbnails

If you want to get a specific thumbnail of an image you can have 2 choices. Either you're in a view, then you'll use the Facade, if not use class/method injection.

Blade Directive

<img src="@thumbnail('/assets/media/original-image-name.png', 'smallThumb')" alt="" />

Or more realistically:

<img src="@thumbnail($product->featured_image->path, 'smallThumb')" alt="" />


In views, you can use the facade for ease of use. Simply send as first argument which image you want and which thumbnail:

<img src="{{ Imagy::getThumbnail('/assets/media/original-image-name.png', 'smallThumb') }}" alt="" />

Of course, your paths will usually not be referenced to manually but rather coming from an entity:

<img src="{{ Imagy::getThumbnail($product->featured_image->path, 'smallThumb') }}" alt="" />

Dependency injection

use Modules\Media\Image\Imagy; /** * @var Imagy */ private $imagy; public function __construct(Imagy $imagy) { $this->imagy = $imagy; } public function index() { $thumbnail = $this->imagy->getThumbnail('/assets/media/original-image-name.png', 'smallThumb'); }

From app() Service Container

// simply get imagy from Service Container $imagy = app('imagy'); // get smallThumb $imagy->getThumbnail($this->entity->avatar->first()->path, 'smallThumb');

Mobile Analytics