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










share|improve this question


















  • 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















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










share|improve this question


















  • 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













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










share|improve this question













(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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()