Php: how to obtain the image address in the CSS file and download it to a local device

Source: Internet
Author: User
This article mainly introduces how to obtain the image address in the CSS file and download it to a local place in php, which can capture and download the image from the css file on the website to a local place, it is a very practical technique. if you need it, you can refer to the following example to describe how php obtains the image address in the CSS file and downloads it to a local place. Share it with you for your reference.

The specific implementation code is as follows:

The code is as follows:


/**
* Obtain the image address in CSS and save it to the local device.
*/
Class getInCssImage
{
/**
* Save the image
* @ Param $ css url
* @ Param $ dir: Directory for saving images
* @ Return void
*/
Static public function saveImage ($ cssUrl, $ dir)
{
$ Content = file_get_contents ($ cssUrl );
$ Patterns = '/images (. *). (jpg | gif | png)/'; // the regular expression needs to be changed based on 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; // enter the address you want to crawl
Readfile ($ imageUrl );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
$ LocalImage = $ dir. strchr ($ image, '/'); // The image address saved locally.
$ Fp = fopen ($ localImage, 'A ');
Fwrite ($ fp, $ img );
Fclose ($ fp );
}
}
}
$ Content = getInCssImage: saveImage ('/css/css.css', 'image ');

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.