9

I'm trying to display a png image in PHP (Laravel)

So far I've tried

$response =  Response::make(readfile(public_path() . 
"/img/$image_name.png", 200))->header('Content-Type', 'image/png');
return $response;

and this

$file = public_path() . "/img/$image_name.png";
$im = imagecreatefrompng($file);
header('Content-Type: image/png');
imagePNG($im);
imagedestroy($im);

I always get something like this instead of an actual image

�PNG  IHDR:�c PLTE����>tRNS�* �< pHYs���+uIDAT(�eұ� AJs *́[P૒��@8Ҍ��Y�:����s�A�"D�!B�"D�!B�"D�!C~����}��Q��N�+'��bP�.a&^O)%5Y\�L����.ޜ9��IEND�B`�

When I use jpeg header and jpeg image it starts showing the actual image in browser, but for some reason it doesn't work for png images. Have anyone faced a similar problem?

4
  • 1
    I've never seen that JPEG/PNG issue with the headers, I'd be confirming the image format of the files you're testing with. Commented Nov 20, 2016 at 21:53
  • just remove the header line to make sure the file is the right format, or even a image file what is being output it by the code Commented Nov 20, 2016 at 22:09
  • yeah, it might be something to do with the image file (I've been creating them with Photoshop); I downloaded some test png image from the internet and it worked correctly. Thank you for your comments. Commented Nov 20, 2016 at 22:54
  • Why are you trying to stream an image that’s already in the public directory? Just serve it directly and you then don’t have the overhead of creating an unnecessary PHP process! {{ asset("img/{$image_name}.png") }}. If you have a page with a lot of images on, serving them through PHP processes is going to create a lot of overhead. Commented Jan 10, 2017 at 9:38

4 Answers 4

19

Try it! Keep it simple :)

$img = file_get_contents(public_path('yourimage.jpg'));
return response($img)->header('Content-type','image/png');
Sign up to request clarification or add additional context in comments.

6 Comments

Exactly. This is it.
Since Laravel 5 you can keep it even simpler: return response()->file($filepath);
@Adam How would you be able to go about doing that with multiple images?
@Cyfer multiple images? What you trying to archive? Either download them as zip, or if you want to display multiple images, each of them need their own endpoint.
@Adam I have an Image Uploader element that I made in Vue for Laravel. It uploads multiple fine, and saves the image URLs to the database, but now I want to make it possible for the user to edit their post by adding more images or removing images, where they can preview the already uploaded images, and then append the new images to the existing images. So I'm trying to get an array of Files sent to my Vue Element after an axios get request.
|
6

You should find the mime dynamicaly using laravel default file facade File::mimeType($path) because as you mentioned that it is working fine with another image type like jpeg. I think this may happen because sometime we change file extension for testing or may be file have some issue with its header etc. You can use the following code :

Method 1 use Response::stream:

$path = public_path() . "/img/$image_name.png";
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::stream(function() use($file) {
  echo $file;
}, 200, ["Content-Type"=> $type]);

Method 2 use Response::make:

$path = public_path() . "/img/$image_name.png";
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;

Hope it will help.

Comments

0

perhaps you forgot install php-gd extension on your server

sudo apt-get install php-gd

or

sudo yum install php-gd

4 Comments

OP would not be getting the �PNG IHDR:�c PLTE����>tRNS�* � output they describe in that situation. They'd see a blank page or an error message.
pls have a try then recomment
I'm not the OP. The fact remains - lacking GD would cause totally different behavior than the OP has indicated they're getting.
Although it might not be the actual solution to the OP's esoteric conundrum, it is a worthwhile answer on a page that would be found by someone trying to work out why their image isn't showing.
-1

I suggest you use this plugin: http://image.intervention.io/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.