PHP Gets the image address in the CSS file and downloads it to the local method _php tips

Source: Internet
Author: User

This article illustrates how PHP gets the image address in a CSS file and downloads it locally. Share to everyone for your reference.

The specific implementation code is as follows:

Copy Code code as follows:

/**
* Get the image address in CSS and save it to local
*/
Class Getincssimage
{
/**
* Picture saved
* @param $cssUrl CSS URL address
* @param $dir the directory where the pictures are saved
* @return void
*/
static public Function SaveImage ($CSSURL, $dir)
{
$content = file_get_contents ($CSSURL);
$patterns = '/images (. *). (jpg|gif|png)/'; You need to change your address 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; This address fills in the address you want to crawl
ReadFile ($IMAGEURL);
$img = Ob_get_contents ();
Ob_end_clean ();
$size = strlen ($img);
$localImage = $dir. STRCHR ($image, '/'); Save to local picture address
$fp = fopen ($localImage, ' a ');
Fwrite ($fp, $img);
Fclose ($FP);
}
}
}
$content = Getincssimage::saveimage ('/css/css.css ', ' image ');

I hope this article will help you with your PHP program design.

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.