Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a png image through a php script

Tags:

php

png

I would like to display an image through a PHP script so that you have a normal img in html but with a source of a php script. This script would then open an existing png or jpg image and display that image through it.

I have been trying this code with no luck at present.

$img = imagecreatefrompng("logo.png");

header("Content-type: image/png");

imagepng($img);
imagedestroy($img);

No errors the image output is broken.

Thanks in advance.

like image 979
aHunter Avatar asked Dec 05 '25 18:12

aHunter


1 Answers

header("Content-type: image/png");
readfile("$file");
exit;

it is good idea to add some headers, like:

header('Expires: 0');
header('Content-Length: ' . filesize($file));

look at discussion here: http://php.net/manual/en/function.readfile.php

like image 123
Jacek Kaniuk Avatar answered Dec 07 '25 15:12

Jacek Kaniuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!