Examples » Bulk Image Downloader

Posted June 16th, 2009 by admin

Here is another simple example. The script below will download linked images from a page, just like Firefox's DownloadHelper add-ons. But custom script like this gives us more controls. In this case, we'll download this girl photos which size is <= 800KB.

<?php
/**
 * Download JPEG files, where size is <= 800KB
 */

include 'phpWebHacks.php';

$url = 'http://www.teenshose.com/models/ksenia1/ksenia1-photos.htm';

$h = new phpWebHacks;

/* open the page */
$page = $h->get($url);

/* get all hyperlinks */
preg_match_all('/<a[^>]+href="([^"]+)"[^>]*>/siU', $page, $matches);

/* download only JPEGs less than 800KB */
foreach($matches[1] as $url)
{
    $head = $h->head($url);
   
    if ($head['Content-Type'] == 'image/jpeg' &&
        $head['Content-Length'] <= 800000) {
        file_put_contents('images/'.basename($url), $h->get($url));
    }
}
?>

The script above use head method to retrieve the resource's info. The method is useful for you to make decisions with the requested resource.


« Prev
Access Del.icio.us Web Service API
Next »
Upload Images to TinyPic and Retrieve the URLs

Leave a note

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000
captcha
Enter the code above: