Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-29-2007, 02:31 PM   #1
Registered: Jun 2003
Location: Cleveburg, OH
Distribution: mostly Fedora
Posts: 154

Rep: Reputation: 30
Question can i use php to grab remote image and display as if local

I'm looking for a way to use PHP on my main webserver to grab an image off of a private webserver and return it as if it resided on the main server.

Here's what I have so far, but it returns screens full of garbage (in text) instead of an image:
$fp = fsockopen("", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET /2-day-count.png HTTP/1.1\r\n";
    $out .= "Host:\r\n";
    $out .= "Connection: Close\r\n\r\n";

    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fread($fp, 26);
Old 03-29-2007, 03:11 PM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Try adding
header('Content-Type: image/png' );
before the image data is echoed.

Or, store the file on your webserver, and echo a link to the image as an inline element.

--- rod.

Last edited by theNbomr; 03-29-2007 at 03:12 PM.
Old 03-29-2007, 03:19 PM   #3
LQ Newbie
Registered: Apr 2006
Location: Szeged, Hungary
Distribution: Debian "squeeze"
Posts: 27

Rep: Reputation: 15
Here is a simple code.

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-type: image/png');
for ($t=0;$file[$t];$t++) { echo $file[$t]; }

Save this script as picture.php and insert this line in your webpage <img src="picture.php">
Old 03-29-2007, 03:25 PM   #4
LQ Newbie
Registered: Mar 2007
Distribution: Ubuntu Feisty
Posts: 5

Rep: Reputation: 0
Http traffic can be broken into 2 sections, headers and the content. The headers contain the content type and such and are in ascii while the content's type depends on what's being sent. The headers tell the browser to display the content as an image in this case and need to be outputted separately from the content as a header. Just adding "header('Content-Type: image/png' );" will not work because after getting the image the headers are clumped onto the top corrupting the images. We fix them by breaking the 2 sections apart and outputting them separately. This script (just a modified version of yours) will refer the headers and content so it should work for any type of content.

PHP Code:
if (!
$fp) {
"$errstr ($errno)<br />\n";
} else {
$out "GET /2-day-count.png HTTP/1.1\r\n";
$out .= "Host:\r\n";
$out .= "Connection: Close\r\n\r\n";

    while (!
feof($fp)) {
$output .= fread($fp26);

$output explode("\r\n\r\n"$output2); // Break the image revieved into it's http headers and the content.
foreach(explode("\n"$output[0]) as $key => $value) { // Break the headers into individual headers and give each of them to the browser.
$output[1]; // Output the image portion;

Old 03-29-2007, 03:39 PM   #5
Registered: Jun 2003
Location: Cleveburg, OH
Distribution: mostly Fedora
Posts: 154

Original Poster
Rep: Reputation: 30
Thanks much! Working like a charm now. After thinking on it some more, I figured I was getting HTML headers mixed with the data, but didn't know an elegant way to separate them out.

Thanks again!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple script to grab an image from a web page and set background stardotstar Programming 43 09-11-2006 10:52 PM
xscreensaver flipscreen3d doesn't grab desktop image danahata Slackware 10 08-01-2005 01:07 PM
can I use grabWidget to grab an image from video kodaku Programming 0 08-22-2004 07:12 AM
how to use local displayer as remote server's display? htm Linux - Software 4 04-22-2004 05:24 AM
How to grab the pixel image output by OpenGL phil123 Linux - General 1 02-26-2004 07:51 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:21 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration