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

simplenotezy's avatar

Image is being thrown as a white blank image

I am experiencing a very strange error that is happening everywhere in my app where I return an image. When a image is being returned in browser, it looks like this white little square:

image

The code used to return this:

return response(File::get(public_path().'/images/my-image.png'), 200)->header('Content-Type', 'image/png');

I have ensured the image path is valid, that the image is valid (I can access it without issues if I open it directly from its public path), and checked any error logs (nginx + laravel.log).

If I die and dump the values of File::get(public_path().'/images/my-image.png') I get PNG encoded data.

If I cURL to the path, it looks like this:

image

image

It happens on both staging, production and local machine (3x local machines testet - 1 windows, 2x mac osx). It occurred suddenly - it used to work fine previously.

I am stuck with this bug. Any suggestions?

I have tried removing all vendor ServiceProviders out of desperation, but that does not change the issue

0 likes
11 replies
Snapey's avatar

I would check for something being output before the image stream. Something that would stop the first bytes of the PNG being recognised.

Maybe a stray space before an opening <?php tag.

Try returning a plain string and check if that is exactly what you get in the browser and not prefixed by some whitespace or other characters.

2 likes
rolandka's avatar

I'm having exactly the same problem. The result is a white little square or a corrupted png file. My response looks like this.

$path = storage_path('images/secure/licences/'.$filename); 
return response()->download($path, $filename, ["Content-Type: image/png"], 'inline');
rolandka's avatar

This could be a bug in Symfony's BinaryFileResponse or not sure. I could not get it to work. I have a quick solution which is to clean the output buffer before sending the response. I know it is very ugly but at least works. See below. Let me know if anyone has a better solution.

$path = storage_path('images/secure/licences/'.$filename); 
$response = response()->download($path, null, []);
ob_end_clean();
return $response;
    
1 like
Snapey's avatar

This would indicate something along the lines of what I indicated earlier. A stray character in the output that for html pages would go unnoticed.

1 like
simplenotezy's avatar

@Snapey you are defiantly right. It must be that. But this character output could be anywhere. Any advice for me how to find it?

ohffs's avatar

If you are purely checking for files with stray whitespace at the start you can run :

gawk 'FNR>1 {nextfile} /^\s/ { print FILENAME ; nextfile }' *.php

(assuming in your controllers directory or where-ever)

1 like
simplenotezy's avatar

ohffs thanks. Is there a way where I can run this in my whole project, recursively? Maybe its a package that adds the whitespace, maybe something else.

simplenotezy's avatar

@ohffs I have figured out that if I simply return ''; in a route, and inspect the source, I see a "tab" character. Now I need to figure out where this tab is

Snapey's avatar

Its likely to be a file you have changed since it was working before. You might be able to narrow it down by looking at modified files since a known good date.

ohffs's avatar

If you have a test route outside of any middleware etc, try hitting that and see if you get a tab char - if not then you can narrow it down to a controller/middleware at least. Tab is, sadly, a fairly common character to have in your code to easily search for :-/ You could try :

grep -R '\t' app

But I suspect it'll come back with quite a few hits ;-)

Edit: this might help more :

grep -R $'[a-z0-9][a-z0-9]*\t' app
simplenotezy's avatar

Thanks. Issue is solved. I noticed a tab before a PHP opening tab.

The following regex search solved it for me:

\t<\?php

(in sublime text)

1 like

Please or to participate in this conversation.