Support the ongoing development of Laravel.io →
posted 10 years ago
Requests
Last updated 1 year ago.
0

Not really sure but i think u have to do somthing like this

return Response::make(readfile("/images/2.png", 200)->header('Content-Type', 'image/png'));

Based on example

// Create a response and modify a header value
$response = Response::make($contents, 200);
$response->header('Content-Type', 'application/json');
return $response;
Last updated 1 year ago.
0

Apparently this is not exactly a unique problem, but none of the solutions I've seen anywhere work.

So this...

$response = Response::make(File::get("images/2.png"));
$response->header('Content-Type', 'image/png');
return response;

Results in the image being broken.

$response = Response::make(File::get("images/2.png"));
return response;

Results in binary data

	$headers = array();
		$headers['Content-type'] = 'image/png';
		$headers['Content-Transfer-Encoding'] = 'binary';
		$headers['Content-Disposition'] = 'inline filename="Hate you"';
	return Response::make(readfile("images/2.png"), 200, $headers);

Results in the headers not being changed and more binary data

Since a lot of the solutions seem to involve using a route, I tried that

Route::get('/images/125.png', function(){

	$headers = array();
		$headers['content-type'] = 'image/png';
		$headers['content-transfer-encoding'] = 'binary';
		$headers['content-disposition'] = 'inline filename="Hate you"';
		$headers['content-length'] = filesize('images/1.png');
		
		return Response::make(readfile('images/1.png'), 200, $headers);
});

Also results in binary data

However...

$response = Response::make(File::get("images/2.png"));
$response->header('Content-Type', 'image/png');
return $response;

results in a broken image/image contains errors (depending on the browser), with the content-type header correctly set. I can also see that the expected filesize is being passed to the browser (in this case, 115KB) Throwing in the other headers does not change anything. Once I convince Laravel to actually set the Content-Type header, it breaks.

Creating a normal PHP file and putting it into public works fine:


<?php
header("Content-Type: image/png");
readfile("images/2.png");
?>

I'm at the end of my rope with this because this is mission critical to my application. I'm hoping someone can spot the little thing I'm doing wrong.

Last updated 1 year ago.
0
$response = Response::make(File::get("images/2.png"));
$response->header('Content-Type', 'image/png');
return response;

Check if this response data begins with a UTF8 BOM. If this is the problem, Symfony could be causing it (that would explain why it works when you don't use Symfony response).

Last updated 1 year ago.
0

Well, this one was a complete pain to solve. And in the end, it was one tiny little thing...

I did a fresh install, created the image controller, created a route, uploaded a placeholder image and viola, it worked. Now to figure out what part of the other application made everything fall over.

Fortunately the culprit revealed itself quickly: my IOC file caused certain images to break. Upon verrry careful inspection I noticed there was a trailing line after the closing ?>.

Gah.

Last updated 1 year ago.
0

If you don't mind me asking, how come you have a closing php tag? In my work with Symfony and Laravel, I've never found myself using it in any php file.

Last updated 1 year ago.
0

popolla said:

If you don't mind me asking, how come you have a closing php tag? In my work with Symfony and Laravel, I've never found myself using it in any php file.

I don't mind the question. I've actually never really thought about it until you asked.

I guess the main reason is I am really bothered by not having properly closed tags. And that my IDEs have always included it. So not especially good reasons. :: shrug ::

Last updated 1 year ago.
0

Use file_get_contents instead.

0

I was having the same problem, and the post marked as solution did not work for me. I found the solution here. For those who don't want to read the page. Here's what I've done. Just add this line BEFORE returning the response


Greetings
0

Sign in to participate in this thread!

Eventy

Your banner here too?

littleum littleum Joined 22 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.