-
- /**
- * Get the image address in CSS and save it locally
- */
- class getInCssImage
- {
- /**
- * Save the picture
- * @param $cssUrl css url address
- * @param $dir The directory where the picture is saved
- * @return void
- */
- static public function saveImage($cssUrl, $dir)
- {
- $content = file_get_contents($cssUrl) ;
- $patterns = '/images(.*).(jpg|gif|png)/'; //regex needs to be transformed 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.plcxue.com/".$image; //The address to be captured
- readfile($imageUrl);
- $img = ob_get_contents();
- ob_end_clean();
- $size = strlen ($img);
- $localImage = $dir. strchr($image, '/'); //Save the local image address
- $fp = fopen($localImage, 'a');
- fwrite($fp, $img);
- fclose($fp);
- }
- }
- }
- $content = getInCssImage::saveImage('/css/css.css', 'image');
Copy code
|