How to assure an image has been served right in Laravel 5?
up vote
2
down vote
favorite
(If some editor helps me to title this question better, I'll be more than thankful)
I have a controller which has a method that serves a PNG image in a Storage, basically it says:
public function background($id) {
$campaign = Campaign::find($id);
$file = Storage::get($campaign->background);
return new Response($file, 200);
}
In the routes I have:
Route::get('/campaign/background/{id}',
'CampaignController@background')->name('campaign.background');
So, if I call the route inside an <img ...>
HTML tag, it works perfectly:
<img src="{{ route('campaign.background', [id => 1]) }}" class="...">
It will give me the background image for the class Campaign with the id 1
Now, my problem is when I try to load several imgs, like in a Bootstrap 4 carousel, I have:
<div id="caro" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '4']) }}?auto=yes" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '14']) }}?auto=yes" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '2']) }}?auto=yes" alt="Third slide">
</div>
</div>
</div>
Only the first image (id 4) is shown correctly, id's 14 and 2 sometimes loads correctly, but most times, they don't, they only show the ALT.
I think it must be something related to the cache, if I reload the page several times, all the imgs appear.
I'm using Laravel 5.7, PHP 7.2.10, Apache 2.4.35 over Windows 10
php laravel-5.7
add a comment |
up vote
2
down vote
favorite
(If some editor helps me to title this question better, I'll be more than thankful)
I have a controller which has a method that serves a PNG image in a Storage, basically it says:
public function background($id) {
$campaign = Campaign::find($id);
$file = Storage::get($campaign->background);
return new Response($file, 200);
}
In the routes I have:
Route::get('/campaign/background/{id}',
'CampaignController@background')->name('campaign.background');
So, if I call the route inside an <img ...>
HTML tag, it works perfectly:
<img src="{{ route('campaign.background', [id => 1]) }}" class="...">
It will give me the background image for the class Campaign with the id 1
Now, my problem is when I try to load several imgs, like in a Bootstrap 4 carousel, I have:
<div id="caro" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '4']) }}?auto=yes" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '14']) }}?auto=yes" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '2']) }}?auto=yes" alt="Third slide">
</div>
</div>
</div>
Only the first image (id 4) is shown correctly, id's 14 and 2 sometimes loads correctly, but most times, they don't, they only show the ALT.
I think it must be something related to the cache, if I reload the page several times, all the imgs appear.
I'm using Laravel 5.7, PHP 7.2.10, Apache 2.4.35 over Windows 10
php laravel-5.7
1
Does the dev tool in your browser (Console or Network) give you any more information? Is it a 404 error or something else?
– Jachinair
Nov 6 at 18:43
No, it's not. If I load the id's 14 and 2 separately it works okay, even if I change the order to display say 14, 2 and 4 in this example, 14 will show while 2 and 4, in most cases won't. If I reload (ctrl-R) the page several times, the three images will show correctly. I got no idea why.
– luisfer
Nov 6 at 19:37
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
(If some editor helps me to title this question better, I'll be more than thankful)
I have a controller which has a method that serves a PNG image in a Storage, basically it says:
public function background($id) {
$campaign = Campaign::find($id);
$file = Storage::get($campaign->background);
return new Response($file, 200);
}
In the routes I have:
Route::get('/campaign/background/{id}',
'CampaignController@background')->name('campaign.background');
So, if I call the route inside an <img ...>
HTML tag, it works perfectly:
<img src="{{ route('campaign.background', [id => 1]) }}" class="...">
It will give me the background image for the class Campaign with the id 1
Now, my problem is when I try to load several imgs, like in a Bootstrap 4 carousel, I have:
<div id="caro" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '4']) }}?auto=yes" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '14']) }}?auto=yes" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '2']) }}?auto=yes" alt="Third slide">
</div>
</div>
</div>
Only the first image (id 4) is shown correctly, id's 14 and 2 sometimes loads correctly, but most times, they don't, they only show the ALT.
I think it must be something related to the cache, if I reload the page several times, all the imgs appear.
I'm using Laravel 5.7, PHP 7.2.10, Apache 2.4.35 over Windows 10
php laravel-5.7
(If some editor helps me to title this question better, I'll be more than thankful)
I have a controller which has a method that serves a PNG image in a Storage, basically it says:
public function background($id) {
$campaign = Campaign::find($id);
$file = Storage::get($campaign->background);
return new Response($file, 200);
}
In the routes I have:
Route::get('/campaign/background/{id}',
'CampaignController@background')->name('campaign.background');
So, if I call the route inside an <img ...>
HTML tag, it works perfectly:
<img src="{{ route('campaign.background', [id => 1]) }}" class="...">
It will give me the background image for the class Campaign with the id 1
Now, my problem is when I try to load several imgs, like in a Bootstrap 4 carousel, I have:
<div id="caro" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '4']) }}?auto=yes" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '14']) }}?auto=yes" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="{{ route('campaign.background', ['id' => '2']) }}?auto=yes" alt="Third slide">
</div>
</div>
</div>
Only the first image (id 4) is shown correctly, id's 14 and 2 sometimes loads correctly, but most times, they don't, they only show the ALT.
I think it must be something related to the cache, if I reload the page several times, all the imgs appear.
I'm using Laravel 5.7, PHP 7.2.10, Apache 2.4.35 over Windows 10
php laravel-5.7
php laravel-5.7
asked Nov 6 at 18:40
luisfer
313314
313314
1
Does the dev tool in your browser (Console or Network) give you any more information? Is it a 404 error or something else?
– Jachinair
Nov 6 at 18:43
No, it's not. If I load the id's 14 and 2 separately it works okay, even if I change the order to display say 14, 2 and 4 in this example, 14 will show while 2 and 4, in most cases won't. If I reload (ctrl-R) the page several times, the three images will show correctly. I got no idea why.
– luisfer
Nov 6 at 19:37
add a comment |
1
Does the dev tool in your browser (Console or Network) give you any more information? Is it a 404 error or something else?
– Jachinair
Nov 6 at 18:43
No, it's not. If I load the id's 14 and 2 separately it works okay, even if I change the order to display say 14, 2 and 4 in this example, 14 will show while 2 and 4, in most cases won't. If I reload (ctrl-R) the page several times, the three images will show correctly. I got no idea why.
– luisfer
Nov 6 at 19:37
1
1
Does the dev tool in your browser (Console or Network) give you any more information? Is it a 404 error or something else?
– Jachinair
Nov 6 at 18:43
Does the dev tool in your browser (Console or Network) give you any more information? Is it a 404 error or something else?
– Jachinair
Nov 6 at 18:43
No, it's not. If I load the id's 14 and 2 separately it works okay, even if I change the order to display say 14, 2 and 4 in this example, 14 will show while 2 and 4, in most cases won't. If I reload (ctrl-R) the page several times, the three images will show correctly. I got no idea why.
– luisfer
Nov 6 at 19:37
No, it's not. If I load the id's 14 and 2 separately it works okay, even if I change the order to display say 14, 2 and 4 in this example, 14 will show while 2 and 4, in most cases won't. If I reload (ctrl-R) the page several times, the three images will show correctly. I got no idea why.
– luisfer
Nov 6 at 19:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Maybe some headers are missing and the browser doesn't know what kind of file you're serving?
You could try this:
public function background($id) {
$campaign = Campaign::find($id);
return Storage::response($campaign->background);
}
Storage::response()
should automatically set Content-Type, Content-Disposition, size, and maybe a few others I'm forgetting about. These headers will help browsers determine what kind of content was in the response.
I'll try that and let you know. What puzzles me is that sometimes it works, but most of the time it doesn't.
– luisfer
Nov 8 at 16:41
Worked perfectly, so yeah, must be something about the headers as you said
– luisfer
Nov 9 at 17:40
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Maybe some headers are missing and the browser doesn't know what kind of file you're serving?
You could try this:
public function background($id) {
$campaign = Campaign::find($id);
return Storage::response($campaign->background);
}
Storage::response()
should automatically set Content-Type, Content-Disposition, size, and maybe a few others I'm forgetting about. These headers will help browsers determine what kind of content was in the response.
I'll try that and let you know. What puzzles me is that sometimes it works, but most of the time it doesn't.
– luisfer
Nov 8 at 16:41
Worked perfectly, so yeah, must be something about the headers as you said
– luisfer
Nov 9 at 17:40
add a comment |
up vote
1
down vote
accepted
Maybe some headers are missing and the browser doesn't know what kind of file you're serving?
You could try this:
public function background($id) {
$campaign = Campaign::find($id);
return Storage::response($campaign->background);
}
Storage::response()
should automatically set Content-Type, Content-Disposition, size, and maybe a few others I'm forgetting about. These headers will help browsers determine what kind of content was in the response.
I'll try that and let you know. What puzzles me is that sometimes it works, but most of the time it doesn't.
– luisfer
Nov 8 at 16:41
Worked perfectly, so yeah, must be something about the headers as you said
– luisfer
Nov 9 at 17:40
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Maybe some headers are missing and the browser doesn't know what kind of file you're serving?
You could try this:
public function background($id) {
$campaign = Campaign::find($id);
return Storage::response($campaign->background);
}
Storage::response()
should automatically set Content-Type, Content-Disposition, size, and maybe a few others I'm forgetting about. These headers will help browsers determine what kind of content was in the response.
Maybe some headers are missing and the browser doesn't know what kind of file you're serving?
You could try this:
public function background($id) {
$campaign = Campaign::find($id);
return Storage::response($campaign->background);
}
Storage::response()
should automatically set Content-Type, Content-Disposition, size, and maybe a few others I'm forgetting about. These headers will help browsers determine what kind of content was in the response.
answered Nov 8 at 2:58
Travis Britz
1,01946
1,01946
I'll try that and let you know. What puzzles me is that sometimes it works, but most of the time it doesn't.
– luisfer
Nov 8 at 16:41
Worked perfectly, so yeah, must be something about the headers as you said
– luisfer
Nov 9 at 17:40
add a comment |
I'll try that and let you know. What puzzles me is that sometimes it works, but most of the time it doesn't.
– luisfer
Nov 8 at 16:41
Worked perfectly, so yeah, must be something about the headers as you said
– luisfer
Nov 9 at 17:40
I'll try that and let you know. What puzzles me is that sometimes it works, but most of the time it doesn't.
– luisfer
Nov 8 at 16:41
I'll try that and let you know. What puzzles me is that sometimes it works, but most of the time it doesn't.
– luisfer
Nov 8 at 16:41
Worked perfectly, so yeah, must be something about the headers as you said
– luisfer
Nov 9 at 17:40
Worked perfectly, so yeah, must be something about the headers as you said
– luisfer
Nov 9 at 17:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53178021%2fhow-to-assure-an-image-has-been-served-right-in-laravel-5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Does the dev tool in your browser (Console or Network) give you any more information? Is it a 404 error or something else?
– Jachinair
Nov 6 at 18:43
No, it's not. If I load the id's 14 and 2 separately it works okay, even if I change the order to display say 14, 2 and 4 in this example, 14 will show while 2 and 4, in most cases won't. If I reload (ctrl-R) the page several times, the three images will show correctly. I got no idea why.
– luisfer
Nov 6 at 19:37