Home / Extract images from a web page with PHP and the Simple HTML DOM Parser

Extract images from a web page with PHP and the Simple HTML DOM Parser

After posting about how to get the meta tags from an HTML web page with PHP I was asked how to get the images from an HTML page with PHP like how Facebook does when a link is posted. This post looks at how to get the image URLs from a page using the Simple HTLM DOM Parser library and in a later post I’ll look at how to download the images and make thumbnails.

PHP Simple HTML DOM Parser

The PHP Simple HTML DOM Parser makes it easy to find particular elements within an HTML page in a similar way to jQuery. It can be downloaded from http://simplehtmldom.sourceforge.net/ where there are also several examples.

Getting the images from an HTML page

The following PHP code will echo a list of images on the page at the time of writing this post.

Note that you need to change ‘/path/to/simple_html_dom.php’ to the path where you saved the simple_html_dom.php file which you downloaded from sourceforge using the link above.

$html = file_get_html('https://www.electrictoolbox.com/php-get-meta-tags-html-file/');
foreach($html->find('img') as $element) {
    echo $element->src, "n";

As at the time of writing this post, the output would be as follows:


Note that the actual path for the image as it is in the HTML is returned and that paths are not resolved to be absolute.

Resolving the paths

So that it’s possible to download the images, the relative URLs need to be turned into absolute URLs. I found a library to do this from the blog for Nadeau Software Consulting but their site no longer appears to be available, so have made the library available for download here.

Download and extract the zipped file url_to_absolute.zip which contains three PHP files. The url_to_absolute.php file requires the other two files.

Here’s the modified version of the above code which will now resolved all image URLs to absolute URLs which can then be used to download the image:


$url = 'https://www.electrictoolbox.com/php-get-meta-tags-html-file/';

$html = file_get_html($url);
foreach($html->find('img') as $element) {
    echo url_to_absolute($url, $element->src), "n";

This will now output the following from the same page, with absolute URLs:


Downloading the images

Instead of echoing out the image URLs as shown in the above example, they could be stored into an array, put into a database, etc etc. In a later post I will look at how to download the images and make thumbnails from them.