1

After scanning through nearly every Google and SO hit, I still can't fix this.

In my app, a user can upload a picture. Because the user's data is pretty sensitive, I save everything in the storage folder and use php to serve the image.

View

<img src="{{ asset('logo/' . $organization->logo_thumbnail_path) }}" alt="">

Route

Route::get('logo/{logo}', function($logo) {

    $image = Image::make(Storage::disk('logo-image')->get($logo));
    $image->response();

});

The route looks in storage/app/img/logo for the corresponding filename. If I echo Storage::disk('logo-image')->get($logo), I get an immense page of weird characters which leads me to believe it does get the actual .jpg image.

However the image isn't displayed, even if I visit the route directly.

Also, if I change the route to take me back to the homepage and I open the view in which the image should be rendered, nothing happens. Which leads me to believe the route is never called from within the <img> tag.

Do any of you know more of this? I'm quite stuck and hours of searching hasn't done anything.

EDIT: Manually setting all the headers and stuff in the response, makes sure the image gets displayed.

Loek
  • 4,037
  • 19
  • 35
  • I'm taking a wild guess here, but I assume the headers are correct? – Andrei Aug 11 '16 at 14:27
  • I'm using the intervention/image library, which states that the response() method I'm calling should give all the right headers. Also, if I echo the `$image->response`, I'm getting another huge page of weird characters. http://image.intervention.io/api/response – Loek Aug 11 '16 at 14:28
  • Possible duplicate of [Laravel 5 - How to access image uploaded in storage within View?](http://stackoverflow.com/questions/30191330/laravel-5-how-to-access-image-uploaded-in-storage-within-view) – aynber Aug 11 '16 at 14:30
  • Yeah that's literally exactly what I'm doing in my question, except it doesn't work. – Loek Aug 11 '16 at 14:31
  • This is not a duplicate since he's using third party code which is literally in no way related to that question. – Andrei Aug 11 '16 at 14:32
  • True, but the answer there might be a better fit than what op is trying. – aynber Aug 11 '16 at 14:32
  • As said, I'm literally doing what the answer says and it doesn't work. Symlinking is out of the question, by client request. – Loek Aug 11 '16 at 14:33
  • Just for consistency's sake, capture the response of the `Storage::disk('logo-image')->get($logo)` to a variable, manually set all the right headers, echo the ouput and do a `exit;`, see if at least the image is displayed, might as well narrow down the problem. – Andrei Aug 11 '16 at 14:35
  • Okay, that somehow does work... Seems like it is an error withing the intervention/image library then? – Loek Aug 11 '16 at 14:44
  • Just a wild guess since I've never used that, but shouldn't you `return` the response from your route? – Drown Aug 11 '16 at 15:32
  • Hmm that seems like a valid point, let me check it! – Loek Aug 11 '16 at 20:31

1 Answers1

2

Seems like the answer was rather simple, I didn't return the $image->response()

So return $image-response(); and everything works.

Thanks @Drown!

Loek
  • 4,037
  • 19
  • 35
  • There is a way to check if the requests comes from an external domain? I mean, return the image for external requests, but show a view if a user enter the route in the browser directly. – JCarlosR May 26 '17 at 17:04
  • What do you mean exactly? Every request is external as long as it doesn't come from the same server your code is executed. – Loek May 26 '17 at 17:07
  • For example: ```https://media.giphy.com/media/3ohzdNzYZoPbJwLHtC/giphy.gif``` this gif can be used in the ```img``` tag and the image is loaded. But, if we visit the link directly in the browser, a page is show (not only the image). – JCarlosR May 26 '17 at 17:09
  • 1
    Pretty cool question, I don't know how giphy has solved this. I can imagine they are parsing the request headers to see the user agent and then use javascript to show html around the image. Might be worth asking a new question for this, I couldn't find any tutorials for doing what you want. (Nice gif btw, hehe) – Loek May 26 '17 at 17:15
  • Probably you want to help me with some ideas: https://stackoverflow.com/questions/44209808/how-to-serve-gif-or-html-based-on-the-request-origin Thank you. – JCarlosR May 26 '17 at 20:40