topical media & game development
alt-bot-lib-download-images.php / php
<?php
/*
#################################################################
Copyright 2007, Michael Schrenk
This software is designed for use with the book,
"Webbots, Spiders, and Screen Scarpers", Michael Schrenk, 2007 No Starch Press, San Francisco CA
W3CŪ SOFTWARE NOTICE AND LICENSE
This work (and included software, documentation such as READMEs, or other
related items) is being provided by the copyright holders under the following license.
By obtaining, using and/or copying this work, you (the licensee) agree that you have read,
understood, and will comply with the following terms and conditions.
Permission to copy, modify, and distribute this software and its documentation, with or
without modification, for any purpose and without fee or royalty is hereby granted, provided
that you include the following on ALL copies of the software and documentation or portions thereof,
including modifications:
1. The full text of this NOTICE in a location viewable to users of the redistributed
or derivative work.
2. Any pre-existing intellectual property disclaimers, notices, or terms and conditions.
If none exist, the W3C Software Short Notice should be included (hypertext is preferred,
text is permitted) within the body of any redistributed or derivative code.
3. Notice of any changes or modifications to the files, including the date changes were made.
(We recommend you provide URIs to the location from which the code is derived.)
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD
PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT
OF ANY USE OF THE SOFTWARE OR DOCUMENTATION.
The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the
software without specific, written prior permission. Title to copyright in this software and any associated
documentation will at all times remain with copyright holders.
#################################################################
*/
#################################################################
#
#
alt-bot-libdownload_images.php POP3 Mail Routines
#
# This library provides routines #
#
#-----------------------------------------------------------------------
# FUNCTIONS
#
# download_binary_file()
# A binary-safe download routine for images
#
# mkpath(
target)
# Downloads all images for a given target webpage
#
#-----------------------------------------------------------------------
include_once(<
alt-bot-lib-parse.php>); # include parse library
include_once(<
alt-bot-lib-http.php>); # include curl library
include_once(<
alt-bot-lib-resolve-addresses.php>); # include address resolution library
*********************************************************************
download_binary_file(ref)
-------------------------------------------------------------
DESCRIPTION:
Downloads an image file (binary safe)
INPUT:
ref Referer value sent to target server
OUTPUT:
The image file is returned as a string
*********************************************************************
function download_binary_file(ref)
{
# Open a PHP/CURL session
s, CURLOPT_URL, s, CURLOPT_RETURNTRANSFER, TRUE); // Return in string
curl_setopt(s, CURLOPT_REFERER, s, CURLOPT_SSL_VERIFYPEER, FALSE); // No certificate
curl_setopt(s, CURLOPT_MAXREDIRS, 4); // Limit redirections to four
# Execute the CURL command (Send contents of target web page to string)
s);
# Close PHP/CURL session
curl_close(downloaded_page;
}
*********************************************************************
mkpath(file Image file to download
path is created
This script is a modified version of a script originally posted on www.php.net
*********************************************************************
function mkpath(path=preg_replace('/(\/){2,}|(\\\){1,}/','/',dirs=explode("/",path="";
foreach (element)
{
element."/";
if(!is_dir(path); // Created if it doesn't exist
}
}
*********************************************************************
download_images_for_page(target Web page that references images
OUTPUT:
Downloads all images in the target web page and recreates the
original directory structure
*********************************************************************
function download_images_for_page(target\n";
# Download the web page
target, target = page_base=get_base_page_address(save_image_directory = "saved_images_".str_replace("http://", "", img_tag_array = parse_array(img_tag_array)==0)
{
echo "No images found at xx=0; img_tag_array); image_path = get_attribute(xx], image_path;
image_path, page_base) == get_base_domain_address(directory = substr(image_path, "/"));
directory );
image_path );
clearstatcache(); // clear cache to get accurate directory status
if(!is_dir(directory))
mkpath(directory);
# Download the image, report image size
image_url, this_image_file);
# Save the image
if(stristr(image_url, ".gif") || stristr(fp = fopen(image_path, "w");
fputs(this_image_file);
fclose(
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.