LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 03-29-2007, 03:31 PM   #1
whysyn
Member
 
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:
Code:
<?php
$fp = fsockopen("12.34.56.78", 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: foo.bar.com\r\n";
    $out .= "Connection: Close\r\n\r\n";

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

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Try adding
Code:
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 04:12 PM.
 
Old 03-29-2007, 04:19 PM   #3
Errotan
LQ Newbie
 
Registered: Apr 2006
Location: Szeged, Hungary
Distribution: Debian "squeeze"
Posts: 27

Rep: Reputation: 15
Hi.
Here is a simple code.

<?php
$file=file("http://12.34.56.78/2-day-count.png");
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, 04:25 PM   #4
MajinCline
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:
<?php
$fp 
fsockopen("12.34.56.78"80$errno$errstr30);
if (!
$fp) {
    echo 
"$errstr ($errno)<br />\n";
} else {
    
$out "GET /2-day-count.png HTTP/1.1\r\n";
    
$out .= "Host: foo.bar.com\r\n";
    
$out .= "Connection: Close\r\n\r\n";

    
fwrite($fp$out);
    while (!
feof($fp)) {
        
$output .= fread($fp26);
    }
    
fclose($fp);

    
$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.
        
header($value);
    }
    echo 
$output[1]; // Output the image portion;

}
?>
 
Old 03-29-2007, 04:39 PM   #5
whysyn
Member
 
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!
 
  


Reply


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 11:52 PM
xscreensaver flipscreen3d doesn't grab desktop image danahata Slackware 10 08-01-2005 02:07 PM
can I use grabWidget to grab an image from video kodaku Programming 0 08-22-2004 08:12 AM
how to use local displayer as remote server's display? htm Linux - Software 4 04-22-2004 06:24 AM
How to grab the pixel image output by OpenGL phil123 Linux - General 1 02-26-2004 08:51 AM


All times are GMT -5. The time now is 10:23 PM.

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