Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ldiebold's avatar

Displaying an image from the view with Intervention Image

I can display an image from a route with the following:

Route::get('/', function()
{

    $img = Image::make(file_get_contents('http://images.boomsbeat.com/data/images/full/979/justin-bieber-2013-3226-hdwallfan-jpg.jpg'));

    // create response and add encoded image data
    $response = Response::make($img->encode('png'));

    // set content-type
    $response->header('Content-Type', 'image/png');

    // output
    return $response;


});

But I can't get an image to display from a blade view. The following gives me a blank screen:

    <?php

        $img = Image::make(file_get_contents('http://images.boomsbeat.com/data/images/full/979/justin-bieber-2013-3226-hdwallfan-jpg.jpg'));

        // create response and add encoded image data
        $response = Response::make($img->encode('png'));

        // set content-type
        $response->header('Content-Type', 'image/png');

        // output
        return $response;

    ?>

I want to watermark images on the fly. I could save them with watermarks, but that means I'll loose flexibility because the user won't be able to change the watermark without every single image being altered.

I've also tried something similar by passing the image through the controller but that also didn't work :(

0 likes
17 replies
Kemito's avatar

Maybe it just don`t want to show Bieber picture :/

17 likes
ldiebold's avatar

Too hawt for Laravel?

I tried your code briedtag, but it gave me a whole bunch of random characters... �PNG  IHDR @��� pHYs���+ IDATx�Խے��%�@�#2��}������E?������؜y��UIʌp'1 HN���j�>MY*"�� �;A�����00

ldiebold's avatar

brlebtag, you legend! Now I just need to try and figure out what "It will embed the picture in html output" means :/.

Can finally go to bed with a smile on my face!!!

2 likes
brlebtag's avatar

it means, normally when you add a picture in html, the browser downloads HTML file and a picture file separately but now a picture is inside the html file so we have html + picture in the same file. But it's ok... no worries :D

2 likes
martinbean's avatar

@ldiebold I know you’ve already accepted an answer, but there’s a better way: Intervention comes with a response() method:

$img = Image::make(file_get_contents('http://images.boomsbeat.com/data/images/full/979/justin-bieber-2013-3226-hdwallfan-jpg.jpg' ));

echo $img->response('png', 70);

This will send the image and the correct headers.

If you’re resizing images on the fly, then you may want to check out the cache extension too: http://image.intervention.io/use/cache

ldiebold's avatar

@martinbean I tried this, but strangely enough I get a whole bunch of "�PNG  IHDR @��� pHYs���+ IDATx�Խے��%�@�#2��}������E?������؜y��UIʌp'1 HN���j�>MY*"�� �;A�����00"

It does however work directly from the route. This works:

Route::get('/', function()
{
    $img = Image::make(file_get_contents('http://images.boomsbeat.com/data/images/full/979/justin-bieber-2013-3226-hdwallfan-jpg.jpg' ));

    return $img->response('png', 70);
});
martinbean's avatar
Level 80

@ldiebold Yeah, you want to use ->response() to return an image from a route or controller action.

You don’t want to be resizing images on the fly within your scripts, because then the visitor has to wait for that operation to complete before the page can continue rendering. If it’s say, a 5 megapixel image being resized, then that’s going to take a couple of seconds, and if you have more than one, then your users are going to be waiting a long time for a page to render. It’s also going to put unnecessary strain on your web server.

2 likes
deadlockgB's avatar

from the Intervention Docs ( http://image.intervention.io/api/make ):

// create a new image directly from an url
$img = Image::make('http://example.com/example.jpg');

and creating Laravel responses ( http://image.intervention.io/use/http ):

Route::get('/', function()
{
    $img = Image::canvas(800, 600, '#ff0000');

    return $img->response();
});

You actually should be able to just do this:

Route::get('/', function()
{
    $img = Image::make('http://images.boomsbeat.com/data/images/full/979/justin-bieber-2013-3226-hdwallfan-jpg.jpg');
    return $img->response();
});
ldiebold's avatar

@martinbean What if the pictures are only around 100-200kb? It's only a small site for a photographer. I want them to be able to easily change the watermark for all pictures and figured that applying it on the fly would be the best way (unless I re-saved every single picture every time the watermark changed.)

users will then be able to buy photos and access the HD version.

when a picture is added, I currently save three versions: icon, web(usually around 150kb in size) and full HD.

Does this make sense? I'd appreciate any insight :)

martinbean's avatar

@ldiebold If your client’s site is on a shared hosting package, and you’re constantly using the CPU of that server to resize images on the fly, then your hosting provider may disable the site. Or if you’re paying for computing power by the hour (say, on a cloud environment like Heroku or Amazon Web Services) then you’re doing your client a disservice by inflating their computing costs.

It’s just bad practice to do things like thumbnail generation on the fly. That’s why things like caches were invented. What happens if your client is showcased in national press or media and there’s a sudden influx of visitors to the site? The site’s going to grind to a halt, that’s if it manages to stay online, whilst it’s grinding through those images for each and every request.

Just generate the thumbnails when the original image is uploaded and then store the thumbnails on disk. Then, on subsequent requests, just serve the appropriate-sized thumbnail image from disk.

If your client decides to update their watermark, have it trigger a queue job that will go through each original image and regenerate the thumbnails. Seriously, how often is your client going to change their watermark? It’s not going to be every 5 minutes, so you can afford to batch-regenerate thumbnails at that time.


Also, @deadlockgB, the edit in your post is not cool.

1 like
ldiebold's avatar

@martinbean Ahh I see, that makes sense. You're right, the client probably won't want to change the watermark, I was just trying to be unnecessarily fancy :p. I'll take your advice and learn about queuing, it shouldn't be too difficult.

Thanks for the detailed response.

razvysb93's avatar
$img = Image::make('http://images.boomsbeat.com/data/images/full/979/justin-bieber-2013-3226-hdwallfan-jpg.jpg');
    return response()->make($img->encode($img->mime()), 200, array('Content-Type' => $img->mime(),'Cache-Control'=>'max-age=86400, public'));

Please or to participate in this conversation.