If you need to save resized image version while uploading the file, like generate a thumbnail for the photo, all you need to do is add this method registerMediaConversions() to your Model:

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\Models\Media;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class NewsItem extends Model implements HasMedia
{
use HasMediaTrait;

public function registerMediaConversions(Media $media = null)
{
    $this->addMediaConversion('thumb')
          ->width(150)
          ->height(100);
}

}
Also, don’t forget to add use Spatie\MediaLibrary\Models\Media; to the top.

This method will generate a resized thumbnail with maximum 150px width and maximum 100px height (resized proportionally) and put it in storage/app/public/[media_id]/conversions folder:

In database media table, it will be saved with custom_properties of registered media name(s).

So, if your Media Collection name is named cover, you can view the thumbnail image like this:

Submit a Comment

Your email address will not be published.