Home > Backend Development > PHP Tutorial > How to get the image address in the CSS file and download it locally in php_PHP tutorial

How to get the image address in the CSS file and download it locally in php_PHP tutorial

WBOY
Release: 2016-07-13 10:12:37
Original
918 people have browsed it

How to use php to get the image address in the CSS file and download it locally,

The example in this article describes how php obtains the image address in the CSS file and downloads it locally. Share it with everyone for your reference.

The specific implementation code is as follows:

Copy code The code is as follows:

/**
* Get the image address in CSS and save it locally
*/
class getInCssImage
{ 
/**
* Save the picture
* @param $cssUrl css url address
* @param $dir Directory to save pictures
* @return void
                     */
          static public function saveImage($cssUrl, $dir)
                                                                                                    $content = file_get_contents($cssUrl);                                                       $patterns = '/images(.*).(jpg|gif|png)/'; //Regular needs to be transformed according to different addresses
                 preg_match_all($patterns, $content, $matches);
                 $imagesUrls = $matches[0];
If (!is_dir($dir))
                       mkdir(dirname(__FILE__). '/'. $dir, 0777);
               foreach($imagesUrls as $image)
                                                                                           ob_start();
                     $imageUrl = "http://www.xxxx.com/".$image; //Fill in the address you want to crawl with this address
                     read file($imageUrl);
$img = ob_get_contents();
                        ob_end_clean();
                     $size = strlen($img);
                          $localImage = $dir. strchr($image, '/'); //Save the local image address
                       $fp = fopen($localImage, 'a');
                            fwrite($fp, $img);
                                                                                                                                                                                                                                                                  through fclose($fp);
                                                                                                                                             }
$content = getInCssImage::saveImage('/css/css.css', 'image');



I hope this article will be helpful to everyone’s PHP programming design.

http://www.bkjia.com/PHPjc/920612.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/920612.htmlTechArticlephp method to get the image address in the CSS file and download it to the local. This article describes the example of php getting the image in the CSS file. address and download to local method. Share it with everyone for your reference. ...
Related labels:
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