This article mainly introduces how to obtain the image address in the CSS file and download it to a local place in php, which can capture and download the image from the css file on the website to a local place, it is a very practical technique. if you need it, you can refer to the following example to describe how php obtains the image address in the CSS file and downloads it to a local place. Share it with you for your reference.
The specific implementation code is as follows:
The code is as follows:
/**
* Obtain the image address in CSS and save it to the local device.
*/
Class getInCssImage
{
/**
* Save the image
* @ Param $ css url
* @ Param $ dir: Directory for saving images
* @ Return void
*/
Static public function saveImage ($ cssUrl, $ dir)
{
$ Content = file_get_contents ($ cssUrl );
$ Patterns = '/images (. *). (jpg | gif | png)/'; // the regular expression needs to be changed based on different addresses.
Preg_match_all ($ patterns, $ content, $ matches );
$ ImagesUrls = $ matches [0];
If (! Is_dir ($ dir ))
Mkdir (dirname (_ FILE _). '/'. $ dir, 0777 );
Foreach ($ imagesUrls as $ image)
{
Ob_start ();
$ ImageUrl = "http://www.xxxx.com/". $ image; // enter the address you want to crawl
Readfile ($ imageUrl );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
$ LocalImage = $ dir. strchr ($ image, '/'); // The image address saved locally.
$ Fp = fopen ($ localImage, 'A ');
Fwrite ($ fp, $ img );
Fclose ($ fp );
}
}
}
$ Content = getInCssImage: saveImage ('/css/css.css', 'image ');
I hope this article will help you with PHP programming.