Jump to content

Extracting an image from a website


mattspriggs28

Recommended Posts

I'm wanting to extract an image from an external website and save to a location on my web server automatically.

 

The scenario:

 

1. A user has an order form on our website with a field to paste the URL of a website in;

2. The user visits an external website page, which includes an image;

3. The user copies the URL out of the address bar, goes back to our website and pastes the link into the order form;

4. The user clicks next, and this extracts the image from the external website and uploads it to our website so the image can be seen alongside the order.

 

My questions are:

 

a. Can this be done?

b. If it can't physically download the image and copy to my web server, can I read the html source of the external website and grab the full URL to use to show the image on my website?

 

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/215857-extracting-an-image-from-a-website/
Share on other sites

a. Sometimes. However, more servers are using protection to avoid having scripts download images. You'll need to emulate a browser well to make it more reliable.

b. Sometimes. Again, some servers will not serve an image to an external source unless various restrictions are met. For instance, my server will not serve images unless the referrer is my server. I could hack around that using curl, but most people wouldn't know or bother.

OK, so I've now worked out how to extract the HTML from an external website. Now what I want to do is find a specific string or part of the URL in the page:

 

1. I want to find a string in the code that contains 'http://www.mysite.com/products/images/'

2. My code has found <img src="http://www.mysite.com/products/images/123.jpg" alt="">

 

So now what I want to do is grab the whole URL of the image, so everything after 'src="' and before '" alt="">'.

 

Can someone please advise the easiest way to do this?

 

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.