aHunter
aHunter

Reputation: 3530

Displaying a png image through a php script

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.

Upvotes: 3

Views: 23526

Answers (2)

blejzz
blejzz

Reputation: 3349

you have to put die(); at the end of your code. Otherwise you will output extra data which will result in errors in image.

Upvotes: 0

Jacek Kaniuk
Jacek Kaniuk

Reputation: 5229

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

Upvotes: 14

Related Questions