Jump to content

trouble with displaying an image from a url


requiem31

Recommended Posts

I am having trouble with displaying an image from a url. In the following code if I echo out $test I get the url of the image. However, in the current code I am trying to display the image, but the only thing that gets displayed is a small broken image in the upper left. 

$html = file_get_contents($website.$criteria); 

$dom = new DOMDocument;

@$dom->loadHTML($html);

$links = $dom->getElementsByTagName('img');

header('Content-Type: image/png');
foreach ($links as $link){
    $test = $link->getAttribute('src');
	echo file_get_contents($test);
}

image that gets displayed: http://imgur.com/epfF3wJ

Link to comment
Share on other sites

Yes, which is how browsers work --- they make HTTP requests from the client to the server,  and the image resource is assembled on the client.

 

You are coding a PHP/Serverside script.  Are these images on your server?  If so, your script should access the image files directly.  

 

Strictly speaking file_get_contents can use the HTTP wrappers if that is turned on, but as it's a huge security hole, most servers do not have that feature enabled for these types of file oriented functions.

Link to comment
Share on other sites

If it's not on your server, make sure that you have the rights to do what you want to do.

 

The question: What do you want to do with those images? Display all of them on one HTML page? Download it on your server? Some kind of proxy that you can call on your server and it will display an image from a distant website?

 

But, if you just want to display all the images found in the distant HTML page:

  • Remove the line header('Content-Type: image/png');
  • In your "foreach", instead of echo file_get_contents($test); try: echo "<img src='$test' />";
  • Now, you'll have an HTML page that displays the images. Only your HTML page will miss the <HTML>, <HEAD>, <BODY> etc.. tags. But it should work if you only want to display the images for yourself. If it's for a public website, ensure that you have a valid HTML page.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.