Laravel 5: Show image in View with Intervention resize() method

Dark Cyber picture Dark Cyber · Oct 8, 2015 · Viewed 7.3k times · Source

I can show resized image with this code inside route,

Route::get('/create', function(){
    $img = Image::make('assets/tes.jpg')->resize(200, 200);
    return $img->response('jpg');
}

But how I can return resized image from Controler to View ?

Here is my Controller

publc function show($id){
$image = /*What code here ? resize assets/tes.jpg to 200x200*/
return view('some', compact('image'));
}

Here is my View some.blade.php

<img src="$image">

or should I resize on upload and save it, instead of resize on the fly ?

Thanks, any help appreciated.

Answer

Emeka Mbah picture Emeka Mbah · Oct 8, 2015

should I re-size on upload and save it, instead of re-size on the fly

Yes, If you are worried about the split seconds (but noticeable time) it takes to re-size and render images on the fly then you are better off with re-sizing on upload and saving various sizes.

Coming to how to display re-sized image in the view:

You can simply specify your route URL:

I am using public_path("/assets/$img") because I assume your images are located in public\assets

Route:

Route::get('/assets/{img}',function($img){
    return \Image::make(public_path("/assets/$img"))->resize(200, 200)->response('jpg');
});

You can even play with the sizes:

Route::get('/assets/{img}/{h}/{w}',function($img, $h=200, $w=200){
        return \Image::make(public_path("/assets/$img"))->resize($h, $w)->response('jpg');
 });

Later in the View:

<img src="{!! url('assets', ['tes.jpg']) !}}">