PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/code/get_css_image.php

https://github.com/monkeycraps/swoole_framework
PHP | 60 lines | 57 code | 3 blank | 0 comment | 3 complexity | 3ddc6409ae2d7c0cd512d5d7b8dc26c0 MD5 | raw file
  1. <?php
  2. if($argc < 5)
  3. {
  4. echo "php ".basename(__FILE__)," css文件目录 远程URL 目标文件夹 目标本地路径\n";exit;
  5. }
  6. $dir = $argv[1];
  7. $remote = $argv[2];
  8. $dest = $argv[3];
  9. $csspath = $argv[4];
  10. $files = scandir($dir);
  11. foreach($files as $f)
  12. {
  13. if(substr($f, -4, 4)=='.css')
  14. {
  15. parseCssFile($dir.'/'.$f);
  16. }
  17. }
  18. function parseCssFile($f)
  19. {
  20. global $remote, $dest, $csspath;
  21. $patten = '#url\s*\(\s*(.*)\s*\)#';
  22. $content = file_get_contents($f);
  23. $match = array();
  24. $n = preg_match_all($patten, $content, $match);
  25. if($n > 0)
  26. {
  27. foreach($match[1] as $_m)
  28. {
  29. $m = trim($_m, "'\" \r\n");
  30. if($m[0]!='/') $m = '/'.$m;
  31. $_fd = $dest.$m;
  32. $_fs = $remote.$m;
  33. if(!is_dir(dirname($_fd)))
  34. {
  35. mkdir(dirname($_fd), 0777, true);
  36. }
  37. if(is_file($_fd))
  38. {
  39. echo "file exists.[$_fd]\n";
  40. continue;
  41. }
  42. str_replace($m, $csspath.$m, $content);
  43. if(copy($_fs, $_fd))
  44. {
  45. echo "download remote file[$_fs] to [$_fd].\n";
  46. //var_dump($m, $csspath.$m);
  47. }
  48. else
  49. {
  50. echo "download fail.file[$_fs] to [$_fd].\n";
  51. }
  52. }
  53. }
  54. file_put_contents($f, $content);
  55. echo "parse ok. [$f]\n";
  56. }