This article mainly introduces the css file background image Download Code implemented by php, involving operations on files and URLs. It is a very practical technique. For more information, see
This article mainly introduces the css file background image Download Code implemented by php, involving operations on files and URLs. It is a very practical technique. For more information, see
This example describes the css file background image Download Code implemented by php. Share it with you for your reference. The specific implementation method is as follows:
Downloading background images in a css file is a long-term task of these pirated users. Downloading a css image is often prone to various advertisement pop-up windows. Here we provide a background image download tool for the php css file.
Put the file in the dos directory of the php program php.exe cssImages. php 0 \ images \
Create an images folder in the php program directory:
The Code is as follows:
<? Php
/**
* @ 2013-4-6
* @ Haha
*/
Set_time_limit (0 );
Error_reporting (E_ERROR );
If ($ argc <4 ){
Print_r ('
+ ------------------------------------------------- +
Usage: php '. $ argv [0]. 'css path type (0 indicates remote, 1 indicates local). css file path image save directory
Example:
Php.exe '. $ argv [0]. '0 \ 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 );
}
// Start with local css
If ($ argv [1] = 1 ){
// I am too lazy to write, huh, it doesn't make much sense
}
/*
* Css image analysis functions
* $ 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
*/
Function getNocssPath ($ path ){
Global $ host;
$ TempLinkmages = '';
// Obtain the equivalent path
$ TempPath = explode ('/', $ path );
For ($ I = 1; $ I <(count ($ tempPath)-2); $ I ++ ){
$ TempLinkmages. = $ tempPath [$ I]. '/';
}
$ XdImage = $ host. $ tempLinkmages;
Return $ xdImage;
// Obtain the equivalent path
}
/*
* Function for retrieving image links
* $ Images array all the images arrays to be obtained
* Css Link
*/
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;
}
/*
* Image Retrieval
* $ UrlImages: array of images to be downloaded
*/
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]). 'The file is successfully downloaded.
');
} Else {
Print_r (
Basename ($ urlImagesOk [$ key]). 'download failed
');
}
}
// Print_r ($ urlImagesOk );
}
/*
* Relative Path absolute path determination Function
* $ ImageUrl: Image link Array
* True indicates the absolute path.
* False indicates the equivalent path.
*/
Function pathCheck ($ imageUrl ){
If (preg_match ('| ^ (\/) |', $ imageUrl )){
Return true;
} Else {
Return false;
}
}
/*
* Image download function
* $ Url image Link
* $ Filename image name
*/
Function grabImage ($ url, $ filename ){
Global $ savePath;
If ($ url = ''){
Return false; // If $ url is null, false is returned;
}
$ Ext_name = strrchr ($ url, '.'); // obtain the image extension.
If ($ ext_name! = '.Gif '& $ ext_name! = '.Jpg '& $ ext_name! = '.Bmp '& $ ext_name! = '.Png '){
Return false; // The format is not in the permitted range.
}
If ($ filename = ''){
Return false; // The name is invalid.
}
// Start capture
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 the 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 PHP programming.
,