Home > Backend Development > PHP Tutorial > PHP css file background image downloader implementation code

PHP css file background image downloader implementation code

WBOY
Release: 2016-07-25 08:52:28
Original
1004 people have browsed it
  1. /**
  2. *@ATANG 2013-4-6 22:19
  3. *@哈哈
  4. */
  5. set_time_limit(0);
  6. error_reporting(E_ERROR);
  7. if($argc<4){
  8. print_r('
  9. +----- --------------------------------------------------+
  10. Usage: php ' .$argv[0].' css path type (0 is remote, 1 is local) css file path image saving directory
  11. Example:
  12. php.exe '.$argv[0].' 0 http://www.xxx .com/index.css images
  13. +------------------------------------------------- --------+
  14. ');
  15. exit();
  16. }
  17. //Remote css
  18. if($argv[1]==0){
  19. $host = getParse($argv[2], 'host');
  20. $savePath = getSavePath($argv[3]);
  21. $images = getCssImagesArray($argv[2]);
  22. //print_r($images);
  23. $imagesurls = getImagesLinks($images,$ argv[2]);
  24. imagesDowner($imagesurls);
  25. }
  26. //Local css starts
  27. if($argv[1]==1){
  28. //Too lazy to write, haha, this doesn’t make much sense
  29. }
  30. /*
  31. * css image analysis function
  32. * $csspath css file path
  33. */
  34. function getCssImagesArray($csspath){
  35. $cssFile = file_get_contents($csspath);
  36. $images = array();
  37. preg_match_all("|url ((.+))|i",$cssFile,$images);
  38. $images = $images[1];
  39. return $images;
  40. }
  41. /*
  42. * css file relative directory processing function
  43. * $path path
  44. */
  45. function getNocssPath($path){
  46. global $host;
  47. $tempLinkmages='';
  48. //Get equivalent path
  49. $tempPath = explode('/',$path);
  50. for($i=1 ;$i<(count($tempPath)-2);$i++){
  51. $tempLinkmages .= $tempPath[$i].'/';
  52. }
  53. $xdImage = $host.$tempLinkmages;
  54. return $xdImage ;
  55. //Get equivalent path
  56. }
  57. /*
  58. * Image connection acquisition function
  59. * $images array All the images arrays that need to be obtained
  60. * cssLink css file links
  61. */
  62. function getImagesLinks($imagesArray,$cssLink){
  63. global $host;
  64. $urlImages = array();
  65. foreach($imagesArray as $key=>$value){
  66. if(pathCheck($value)){
  67. if((!in_array(($host.$ value),$urlImages))){
  68. $urlImages[$key] = $host.$value;
  69. }
  70. }else{
  71. if((!in_array((getNocssPath(getParse($cssLink,'path'))). $value),$urlImages))){
  72. $urlImages[$key] = getNocssPath(getParse($cssLink,'path')).$value;
  73. }
  74. }
  75. }
  76. return $urlImages;
  77. }
  78. / *
  79. * Image acquisition
  80. * $urlImages is the array of images that need to be downloaded
  81. */
  82. function imagesDowner($urlImages){
  83. //print_r($urlImages);
  84. foreach($urlImages as $key=>$value){
  85. $urlImagesOk[$key] = str_replace('//','/',$value);
  86. $urlImagesOk[$key] = str_replace('"','',$urlImagesOk[$key]);
  87. $urlImagesOk [$key] = str_replace("'",'',$urlImagesOk[$key]);
  88. $urlImagesOk[$key] = 'http://'.$urlImagesOk[$key];
  89. if(grabImage($ urlImagesOk[$key],basename($urlImagesOk[$key]))){
  90. print_r(
  91. basename($urlImagesOk[$key]).' The file was downloaded successfully
  92. ');
  93. }else{
  94. print_r(
  95. basename( $urlImagesOk[$key]).' Download failed
  96. ');
  97. }
  98. }
  99. //print_r($urlImagesOk);
  100. }
  101. /*
  102. * Relative path absolute path determination function
  103. * $imageUrl image link array
  104. * true is an absolute path
  105. * false is an equivalent path
  106. */
  107. function pathCheck($imageUrl){
  108. if(preg_match('|^(/)|',$imageUrl)){
  109. return true;
  110. }else{
  111. return false;
  112. }
  113. }
  114. /*
  115. * Image download function
  116. * $url image link
  117. * $filename image name
  118. */
  119. function grabImage($url, $filename){
  120. global $savePath;
  121. if($ url == '') {
  122. return false; //If $url is empty, return false;
  123. }
  124. $ext_name = strrchr($url, '.'); //Get the extension of the image
  125. if($ ext_name != '.gif' && $ext_name != '.jpg' && $ext_name != '.bmp' && $ext_name != '.png') {
  126. return false; //The format is not within the allowed range
  127. }
  128. if($filename == '') {
  129. return false; //Invalid name
  130. }
  131. //Start capturing
  132. ob_start();
  133. if(readfile($url)){
  134. $img_data = ob_get_contents();
  135. ob_end_clean();
  136. $size = strlen($img_data);
  137. }else{
  138. ob_end_clean();
  139. return false;
  140. }
  141. if(($local_file = fopen($savePath.$filename , 'a'))&&(fwrite($local_file, $img_data)))
  142. {
  143. fclose($local_file);
  144. return true;
  145. }
  146. }
  147. /*
  148. * 保存目录
  149. */
  150. function getSavePath($savepath){
  151. $savePath = $savepath;
  152. $savePath = dirname(__FILE__).$savePath;
  153. return $savePath;
  154. }
  155. /*
  156. * 解析url
  157. */
  158. function getParse($host,$type){
  159. $baseurl = parse_url($host);
  160. return $baseurl[$type].'/';
  161. //echo $baseurl;
  162. }
  163. ?>
复制代码


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template