Heim  >  Artikel  >  Backend-Entwicklung  >  php提取并下载css文件中图片地址

php提取并下载css文件中图片地址

WBOY
WBOYOriginal
2016-07-25 08:52:141387Durchsuche
  1. /**  
  2. * 获取CSS中图片地址,并且保存到本地  
  3. */
  4. class getInCssImage
  5. {
  6. /**  
  7. *  图片保存下来
  8. * @param $cssUrl css的url地址
  9. * @param $dir 保存图片的目录
  10. * @return void
  11. */
  12. static public function saveImage($cssUrl, $dir)
  13. {
  14. $content = file_get_contents($cssUrl);   
  15. $patterns = '/images(.*).(jpg|gif|png)/'; //正则根据不同地址需要变换
  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; //要抓取的地址
  24. readfile($imageUrl);
  25. $img  = ob_get_contents();
  26. ob_end_clean();
  27. $size = strlen($img);
  28. $localImage = $dir. strchr($image, '/'); //存到本地的图片地址
  29. $fp = fopen($localImage, 'a');
  30. fwrite($fp, $img);
  31. fclose($fp);
  32. }
  33. }
  34. }
  35. $content = getInCssImage::saveImage('/css/css.css', 'image');
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn