This article describes the PHP implementation of the CSS file background image download code. Share to everyone for your reference. The implementation methods are as follows:
Download the CSS file inside the background picture is we these pirates for a long time to do things, download a CSS Picture downloader often appear all kinds of advertising bomb window, really can't carry. Here is a PHP version of the CSS file background image download to everyone.
Put the file into the PHP directory dos below php.exe cssimages.php 0 http://www.xxxx.com/css/style.css \images\
First in the PHP program directory to build a images folder, hehe, paste code:
Copy Code code as follows:
<?php
/**
*@ Tang 2013-4-6 22:19
*@ hehe
*/
Set_time_limit (0);
Error_reporting (E_error);
if ($ARGC <4) {
Print_r ('
+-------------------------------------------------+
usage:php '. $argv [0]. ' CSS path type (0 is remote, 1 is local) CSS file path picture save directory
Example:
Php.exe '. $argv [0]. ' 0 Http://www.xxx.com/index.css \images\
+-------------------------------------------------+
');
Exit ();
}
Remote CSS
if ($argv [1]==0) {
$host = Getparse ($argv [2], ' host ');
$savePath = Getsavepath ($argv [3]);
$images = Getcssimagesarray ($argv [2]);
Print_r ($images);
$imagesurls = Getimageslinks ($images, $argv [2]);
Imagesdowner ($imagesurls);
}
Local CSS Start
if ($argv [1]==1) {
Too lazy to write, oh, this doesn't make much sense
}
/*
* CSS Image analysis function
* $csspath CSS file path
*/
function Getcssimagesarray ($csspath) {
$cssFile = file_get_contents ($csspath);
$images = Array ();
Preg_match_all ("|url\ (. +) \) |i", $cssFile, $images);
$images = $images [1];
return $images;
}
/*
* CSS file relative directory processing function
* $path Path
*/
function Getnocsspath ($path) {
Global $host;
$tempLinkmages = ';
Considerable path acquisition
$tempPath = explode ('/', $path);
For ($i =1 $i < (count ($tempPath)-2); $i + +) {
$tempLinkmages. = $tempPath [$i]. ' /';
}
$xdImage = $host. $tempLinkmages;
return $xdImage;
Considerable path acquisition
}
/*
* Picture Connection Get function
* $images array all the images arrays that need to be fetched
* Csslink CSS File links
*/
function Getimageslinks ($imagesArray, $cssLink) {
Global $host;
$urlImages = Array ();
foreach ($imagesArray as $key => $value) {
if (Pathcheck ($value)) {
if ((!in_array ($host. $value), $urlImages)) {
$urlImages [$key] = $host. $value;
}
}else{
if ((!in_array (Getnocsspath (Getparse ($cssLink, ' path '). $value), $urlImages))
$urlImages [$key] = Getnocsspath (Getparse ($cssLink, ' path ')). $value;
}
}
}
return $urlImages;
}
/*
* Picture Acquisition
* $urlImages An array of pictures to download
*/
function Imagesdowner ($urlImages) {
Print_r ($urlImages);
foreach ($urlImages as $key => $value) {
$urlImagesOk [$key] = Str_replace ('//', '/', $value);
$urlImagesOk [$key] = Str_replace (' ", ', ' $urlImagesOk [$key]);
$urlImagesOk [$key] = Str_replace ("', '", ', $urlImagesOk [$key]);
$urlImagesOk [$key] = ' http://'. $urlImagesOk [$key];
if (Grabimage ($urlImagesOk [$key],basename ($urlImagesOk [$key])) {
Print_r (
BaseName ($urlImagesOk [$key]). ' File Download successful
');
}else{
Print_r (
BaseName ($urlImagesOk [$key]). ' Download failed
');
}
}
Print_r ($URLIMAGESOK);
}
/*
* Relative path absolute path decision function
* $IMAGEURL Image Link Array
* True to Absolute path
* False is quite a path
*/
function Pathcheck ($IMAGEURL) {
if (Preg_match (' |^ (\/) |, $imageUrl)) {
return true;
}else{
return false;
}
}
/*
* Picture Download function
* $url Picture Link
* $filename Picture Name
*/
function Grabimage ($url, $filename) {
Global $savePath;
if ($url = = ") {
return false; Returns False if the $url is empty;
}
$ext _name = STRRCHR ($url, '. '); Get the name of the picture extension
if ($ext _name!= '. gif ' && $ext _name!= '. jpg ' && $ext _name!= '. bmp ' && $ext _name '. png ') {
return false; Format is not in the allowed range
}
if ($filename = = ") {
return false; Name is not valid
}
Start capturing
Ob_start ();
if (ReadFile ($url)) {
$img _data = ob_get_contents ();
Ob_end_clean ();
$size = strlen ($img _data);
}else{
Ob_end_clean ();
return false;
}
if ($local _file = fopen ($savePath. $filename, ' a ") && (fwrite ($local _file, $img _data))
{
Fclose ($local _file);
return true;
}
}
/*
* Save Directory
*/
function Getsavepath ($savepath) {
$savePath = $savepath;
$savePath = DirName (__file__). $savePath;
return $savePath;
}
/*
* Parse URL
*/
function Getparse ($host, $type) {
$baseurl = Parse_url ($host);
return $baseurl [$type]. ' /';
Echo $baseurl;
}
?>
I hope this article will help you with your PHP program design.