How to extract images from an URL in PHP

Posted on August 30, 2008, under PHP,  Bookmark it

Here’s a piece of code that extracts images from an URL. This only works if the images are displayed using the IMG tag (not CSS).

This script only shows images. It doesn’t download any. The src attribute must have a full url in order for the images to show.

<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/

$url = 'http://www.microsoft.com/';

// Fetch page
$string = FetchPage($url);

// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);

$img_tag_array = $out[0];

echo "<pre>"; print_r($img_tag_array); echo "</pre>";

// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);

$images_url_array = $out[1];

echo "<pre>"; print_r($images_url_array); echo "</pre>";

// Fetch Page Function

function FetchPage($path)
{
$file = fopen($path, "r"); 

if (!$file)
{
exit("The was a connection error!");
} 

$data = '';

while (!feof($file))
{
// Extract the data from the file / url

$data .= fgets($file, 1024);
}
return $data;
}
?>

NOTE: You can use this script to extract images from local files too (not necessarily URLs).

Feel free to post any comments or suggestions regarding this script.

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

8 Replies to "How to extract images from an URL in PHP"

  1. Hi there thanks for the tutorial – only one problem though, this element on line 17

    preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);

    is not functioning properly so I removed it outright. was returning:

    Warning: preg_match_all() [function.preg-match-all]: Empty regular expression in /home/example/public_html/example/test-script.php on line 17

    seems to work fine without it.

  2. did you remove the WHOLE LINE where “PREG_PATTERN_ORDER” or just the word “PREG_PATTERN_ORDER” and the comma “,” (on both lines? or just line 17)
    thank you.

    1. I haven’t removed “PREG_PATTERN_ORDER”. What doesn’t fit in the code box is moved to the next line. As you can see the line 17 is actually made from 2 lines.

  3. Hi Gabriel, I tried it but it doesn’t work.. I saw that JAY has indicated the same problem and getting the code to work once he removed the certain parts.. so i was asking him which part exactly he removed to get the code working.. thank you..

  4. Hi,

    Remove the following lines, it will work properly without any warnings and notices:

    preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);

    $img_tag_array = $out[0];

    echo "<pre>"; print_r($img_tag_array); echo "</pre>";

  5. Hi Gabriel,
    Thank you very much for this valueable post. I have used this code and its perfectly working. but when I try to fetch images from http://www.asos.com/ then it craashing complete code. Its perfectly working on Localhost but when I am trying to upload this on sesrver its genrating warning messages
    “Warning: file_get_contents(http://www.asos.com/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\Inetpub\vhosts\allsii.com\httpdocs\dctest9\products1.php on line 39″

    Can you please help me for this ?
    Narinder.

  6. Interesting this script… But can anyone tell me how to display images instead of displaying the urls? I am creating a script that can fetch all images that are displayed on a certain url.

  7. $image_regex is supposed to be $image_regex_src_url. That will give you an array of images. This is awesome. Thank you!

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)