How to extract images from an URL in PHP

Posted on August 30, 2008, Filled 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!

Sponsors

One Reply 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.

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>


  

CommentLuv Enabled