Home >Backend Development >PHP Tutorial >PHP extracts and downloads the image address in the css file

PHP extracts and downloads the image address in the css file

WBOY
WBOYOriginal
2016-07-25 08:52:141452browse
  1. /**
  2. * Get the image address in CSS and save it locally
  3. */
  4. class getInCssImage
  5. {
  6. /**
  7. * Save the picture
  8. * @param $cssUrl css url address
  9. * @param $dir The directory where the picture is saved
  10. * @return void
  11. */
  12. static public function saveImage($cssUrl, $dir)
  13. {
  14. $content = file_get_contents($cssUrl) ;
  15. $patterns = '/images(.*).(jpg|gif|png)/'; //regex needs to be transformed according to different addresses
  16. preg_match_all($patterns, $content, $matches);
  17. $imagesUrls = $matches [0];
  18. if (!is_dir($dir))
  19. mkdir(dirname(__FILE__). '/'. $dir, 0777);
  20. foreach($imagesUrls as $image)
  21. {
  22. ob_start();
  23. $ imageUrl = "http://www.plcxue.com/".$image; //The address to be captured
  24. readfile($imageUrl);
  25. $img = ob_get_contents();
  26. ob_end_clean();
  27. $size = strlen ($img);
  28. $localImage = $dir. strchr($image, '/'); //Save the local image address
  29. $fp = fopen($localImage, 'a');
  30. fwrite($fp, $img);
  31. fclose($fp);
  32. }
  33. }
  34. }
  35. $content = getInCssImage::saveImage('/css/css.css', 'image');
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn