Yagnik Detroja
Yagnik Detroja

Reputation: 929

How to Generate Thumbnails in Laravel5.2 When I display image

When i write a code to display image in blade file that time a image thumbnails made and i can give the every time height,width that time i need image that kind of image thumbnails generates so i can generate multiple thumb of same image in single site.

{{$page->image,100,100)}}

Or

<td><?php if ($page->image) { ?><img src="{{ url('/upload/pages/'.$page->image,100,200) }}"/><?php } ?></td>

Please Give the suggestion how to make this kind of Thumbnails Thanks In advance.

Upvotes: 2

Views: 2032

Answers (4)

Daniel Cintra
Daniel Cintra

Reputation: 91

Just for the record, if you are using Laravel, a good option is to use intervention/image and intervention/imagecache packages.

What you need is described on the "URL based image manipulation" section of Intervention Image Docs.

Upvotes: 1

Alex
Alex

Reputation: 521

You may resize that image on the fly with a microservice and cache the thumbnails in a CDN. Please have a look here

<img src="{{ imgProxy('https://your-microservice.com/your-image.jpg', 100, 200) }}"/>

Upvotes: 1

r&#252;ff0
r&#252;ff0

Reputation: 943

you need to require Intervention, then put something like this in your upload controller :

...
        $file = $request->file('files');
        $extension = $file->getClientOriginalExtension();
        Storage::disk('local')->put('/'.$file->getFilename().$extension,  File::get($file));

        $thumb1 = ImageManagerStatic::make($file->getRealPath())->resize(200, 200, function ($constraint) {
          $constraint->aspectRatio(); //maintain image ratio
        })->save('thumb1'.$extension);


        $thumb2 = ImageManagerStatic::make($file->getRealPath())->resize(400, 400, function ($constraint) {
          $constraint->aspectRatio(); //maintain image ratio
        })->save('thumb2'.$extension);

...

Upvotes: 1

Denis Mysenko
Denis Mysenko

Reputation: 6534

A rational way to do this would be:

  1. Create a controller and a method (or a method on existing controller) that accepts image name, width & height parameters. This method could use Intervention package that was mentioned in the comments. Logic should be - first check if image of specified dimensions exists, if it doesn't - create it (using Intervention, very simple). Then - output the image contents (don't forget to add correct header).

  2. Add this controller/method to routes.php, eg:

    Route::get('thumbnails/{image}', 'Controller@getThumbnail');

  3. In your Blade templates you would simply refer to images like '/path/image.jpg?width=200&height=100'. No need to care whether the file with these dimensions already exists or not.

PS. Facebook serves images this way. It's basically like a little proxy server (your method works as a proxy) between user and original image.

Upvotes: 1

Related Questions