This article illustrates how PHP gets the image address in a CSS file and downloads it locally. Share to everyone for your reference.
The specific implementation code is as follows:
Copy Code code as follows:
/**
* Get the image address in CSS and save it to local
*/
Class Getincssimage
{
/**
* Picture saved
* @param $cssUrl CSS URL address
* @param $dir the directory where the pictures are saved
* @return void
*/
static public Function SaveImage ($CSSURL, $dir)
{
$content = file_get_contents ($CSSURL);
$patterns = '/images (. *). (jpg|gif|png)/'; You need to change your address according to 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; This address fills in the address you want to crawl
ReadFile ($IMAGEURL);
$img = Ob_get_contents ();
Ob_end_clean ();
$size = strlen ($img);
$localImage = $dir. STRCHR ($image, '/'); Save to local picture address
$fp = fopen ($localImage, ' a ');
Fwrite ($fp, $img);
Fclose ($FP);
}
}
}
$content = Getincssimage::saveimage ('/css/css.css ', ' image ');
I hope this article will help you with your PHP program design.