php css文件背景图片下载器实现代码

WBOY
Freigeben: 2016-07-25 08:52:28
Original
973 Leute haben es durchsucht
  1. /**
  2. *@阿唐 2013-4-6 22:19
  3. *@呵呵
  4. */
  5. set_time_limit(0);
  6. error_reporting(E_ERROR);
  7. if($argcprint_r('
  8. +-------------------------------------------------+
  9. Usage: php '.$argv[0].' css路径类型(0为远程、1为本地) css文件路径 图片保存目录
  10. Example:
  11. php.exe '.$argv[0].' 0 http://www.xxx.com/index.css \images\
  12. +-------------------------------------------------+
  13. ');
  14. exit();
  15. }
  16. //远程css
  17. if($argv[1]==0){
  18. $host = getParse($argv[2],'host');
  19. $savePath = getSavePath($argv[3]);
  20. $images = getCssImagesArray($argv[2]);
  21. //print_r($images);
  22. $imagesurls = getImagesLinks($images,$argv[2]);
  23. imagesDowner($imagesurls);
  24. }
  25. //本地css开始
  26. if($argv[1]==1){
  27. //懒得写了,呵呵,这个没多大意义
  28. }
  29. /*
  30. * css图片分析函数
  31. * $csspath css文件路径
  32. */
  33. function getCssImagesArray($csspath){
  34. $cssFile = file_get_contents($csspath);
  35. $images = array();
  36. preg_match_all("|url\((.+)\)|i",$cssFile,$images);
  37. $images = $images[1];
  38. return $images;
  39. }
  40. /*
  41. * css文件相对目录处理函数
  42. * $path 路径
  43. */
  44. function getNocssPath($path){
  45. global $host;
  46. $tempLinkmages='';
  47. //相当路径获取
  48. $tempPath = explode('/',$path);
  49. for($i=1;$i $tempLinkmages .= $tempPath[$i].'/';
  50. }
  51. $xdImage = $host.$tempLinkmages;
  52. return $xdImage;
  53. //相当路径获取
  54. }
  55. /*
  56. * 图片连接获取函数
  57. * $images array 所有需要获取的images数组
  58. * cssLink css文件链接
  59. */
  60. function getImagesLinks($imagesArray,$cssLink){
  61. global $host;
  62. $urlImages = array();
  63. foreach($imagesArray as $key=>$value){
  64. if(pathCheck($value)){
  65. if((!in_array(($host.$value),$urlImages))){
  66. $urlImages[$key] = $host.$value;
  67. }
  68. }else{
  69. if((!in_array((getNocssPath(getParse($cssLink,'path')).$value),$urlImages))){
  70. $urlImages[$key] = getNocssPath(getParse($cssLink,'path')).$value;
  71. }
  72. }
  73. }
  74. return $urlImages;
  75. }
  76. /*
  77. * 图片获取
  78. * $urlImages需要下载的图片数组
  79. */
  80. function imagesDowner($urlImages){
  81. //print_r($urlImages);
  82. foreach($urlImages as $key=>$value){
  83. $urlImagesOk[$key] = str_replace('//','/',$value);
  84. $urlImagesOk[$key] = str_replace('"','',$urlImagesOk[$key]);
  85. $urlImagesOk[$key] = str_replace("'",'',$urlImagesOk[$key]);
  86. $urlImagesOk[$key] = 'http://'.$urlImagesOk[$key];
  87. if(grabImage($urlImagesOk[$key],basename($urlImagesOk[$key]))){
  88. print_r(
  89. basename($urlImagesOk[$key]).' 文件下载成功
  90. ');
  91. }else{
  92. print_r(
  93. basename($urlImagesOk[$key]).' 下载失败
  94. ');
  95. }
  96. }
  97. //print_r($urlImagesOk);
  98. }
  99. /*
  100. * 相对路径绝对路径判定函数
  101. * $imageUrl 图片链接数组
  102. * true 为绝对路径
  103. * false 为相当路径
  104. */
  105. function pathCheck($imageUrl){
  106. if(preg_match('|^(\/)|',$imageUrl)){
  107. return true;
  108. }else{
  109. return false;
  110. }
  111. }
  112. /*
  113. * 图片下载函数
  114. * $url 图片链接
  115. * $filename图片名字
  116. */
  117. function grabImage($url, $filename){
  118. global $savePath;
  119. if($url == '') {
  120. return false; //如果 $url 为空则返回 false;
  121. }
  122. $ext_name = strrchr($url, '.'); //获取图片的扩展名
  123. if($ext_name != '.gif' && $ext_name != '.jpg' && $ext_name != '.bmp' && $ext_name != '.png') {
  124. return false; //格式不在允许的范围
  125. }
  126. if($filename == '') {
  127. return false; //名称无效
  128. }
  129. //开始捕获
  130. ob_start();
  131. if(readfile($url)){
  132. $img_data = ob_get_contents();
  133. ob_end_clean();
  134. $size = strlen($img_data);
  135. }else{
  136. ob_end_clean();
  137. return false;
  138. }
  139. if(($local_file = fopen($savePath.$filename , 'a'))&&(fwrite($local_file, $img_data)))
  140. {
  141. fclose($local_file);
  142. return true;
  143. }
  144. }
  145. /*
  146. * 保存目录
  147. */
  148. function getSavePath($savepath){
  149. $savePath = $savepath;
  150. $savePath = dirname(__FILE__).$savePath;
  151. return $savePath;
  152. }
  153. /*
  154. * 解析url
  155. */
  156. function getParse($host,$type){
  157. $baseurl = parse_url($host);
  158. return $baseurl[$type].'/';
  159. //echo $baseurl;
  160. }
  161. ?>
复制代码


Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!