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

PHP css file background image downloader implementation code

Jul 25, 2016 am 08:52 AM

  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. ?>
复制代码


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles