3
votes

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.

2
Is there any other output in your script? Is there any whitespace before <?php? - Oliver Charlesworth
No errors the image output is broken. - aHunter

2 Answers

13
votes
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

0
votes

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