B
B
BarneyGumble2019-01-15 22:22:06
Laravel
BarneyGumble, 2019-01-15 22:22:06

How to shrink images on the fly in Laravel?

Good evening
Made a small online store on Laravel. I used Voyager as an admin panel. I uploaded products via Voyager and now I have the following problem: the photos of the products were initially large (about 3-4mb each) and now on the catalog page of 30 positions I have a page weighing >
100mb rewrite significant code?
Picked in the direction of Image Intervention, but it's not very clear how to make friends with Voyager I display the
pictures like this:
products.blade.php

<a class="card__piclink" href="{{ route('shop.show', $product->slug) }}">
       <img data-ripple class="card__img" data-src="{{ productImage($product->image) }}" alt="">
</a>

helpers.php
function productImage($path)
{
    return $path && Storage::disk('local')->exists('public', $path) ? Storage::disk('local')->url($path) : asset('img/not-found.jpg');
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
svitix, 2019-01-16
@svitix

I did not use Voyager, maybe there are some features. But judging by your helper. You still need to fully understand the intervention. Create filter ProductThumbFilter image.intervention.io/use/filters.
Add it to config/imagecache.php:

'templates' => array(
        'productthumb' => 'App\ImageFilters\ProductThumbFilter',

And already in the template or in the helper get the image url:
route('imagecache', ['template' => 'productthumb', 'filename' => $product->image ])
All thumbnails missing for the image will be generated on the fly and save to cache.

P
Pavel Omelchenko, 2019-01-16
@pOmelchenko

Queue and tasks to help you
When you upload a large image, the compression (and resize) will go in the background and "drop" in the course of being ready for the product that you have. You don't need to press on the fly. Store finished images. Save server resources

A
Aleksandr Petrovskyi, 2019-01-16
@sashalenz

Add thumbnails for all images and only show them in the list?

A
Antonio Solo, 2019-01-16
@solotony

make a preview output, when accessing it, if the file is missing, Laravel calls which generates it from the source file and puts it on disk.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question