Php: getting the image address in the CSS file, downloading and saving it to the local _ PHP Tutorial

Source: Internet
Author: User
Php obtains the image address in the CSS file, downloads it, and saves it to the local device. Copy the code as follows: *** get the image address in CSS and save it to the local * classgetInCssImage {*** save the image * @ param $ cssUrlcss url * @ param $ dir save
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.xx.com/". $ image; // This address was originally obtained by a program. Slack
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 ');

WaterMark/*** get the image address in CSS, and save it to the local */class getInCssImage {/*** Image save down * @ param $ cssUrl css url address * @ param $ dir save...

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.