Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return the contents of an image in php file?

Tags:

php

image

I really do not know any PHP but I'd love to do one simple thing:
I access a php page from within a <img src="/myhumbleimage.php" /> and I'd like to have an image returned from another URL.


I came up with:

<?php
header('Content-Type: image/png');
readfile('i' . rand(1,3) . '.png');
exit;

And it works:
Avatar selection http://vercas.webuda.com/img.php?.png
(Reload the page a few times!)

like image 800
Vercas Avatar asked Dec 20 '10 22:12

Vercas


3 Answers

Check out readfile().

The basic idea is you send the appropriate MIME type headers (using header()) then deliver the file contents using readfile().

For example

<?php
// myhumbleimage.php

// Do whatever myhumbleimage.php does before the image is delivered

header('Content-Type: image/jpeg');
readfile('path/or/url/of/image/file.jpg');
exit;
like image 177
Phil Avatar answered Nov 02 '22 20:11

Phil


Why not just reference the image directly then? If you are trying to hide the fact you are pulling an image from an external source, that external source will still be able to tell you are pulling their images.

Otherwise, pass a Content-Type header with the appropriate mime-type and echo the results of file_get_contents($imageUrl).

like image 1
simshaun Avatar answered Nov 02 '22 22:11

simshaun


Just generate or read, then output the image using PHP.

...get image data from file or dynamically...
header('Content-type: image/png'); //or whatever MIME type

print $imgdata;

Or check out this: http://php.net/manual/en/function.imagepng.php

like image 1
Robert Avatar answered Nov 02 '22 21:11

Robert