Use PHP implementation to download images from CSS files _php instances

Source: Internet
Author: User
As a veteran and professional Mr Skinner staff, I have accumulated a wealth of Mr Skinner experience since I began to invest in the great Internet from third day. I believe that every programmer who does the web will have a similar experience.

In the Mr Skinner process, it is essential to download the picture in the style file. When you encounter a larger style file, which may have hundreds of images to download, it is most appropriate to use this little code.

< php/* more & Original php framwork Copyright (c) 2007-2008 ismole Inc author:kimi Documentation: Download the pictures in the style file, Water Special Mr Skinner tool *///note set PHP timeout set_time_limit (0)//note get style file content $stylefilecontent = file_get_contents (' Images/style.css //note matches the URL address that needs to be downloaded Preg_match_all ("/url\ ((. *) \)/", $styleFileContent, $imagesURLArray);//note loop needs to download the address, download $ Imagesurlarray = Array_unique ($imagesURLArray [1]); foreach ($imagesURLArray as $imagesURL) {file_put_contents ( BaseName ($imagesURL), file_get_contents ($imagesURL));}

And then we'll share a package of good classes.

/** * Get image address in CSS and save to local */class getincssimage{/** * Picture saved * @param $cssUrl CSS URL address * @param $dir the directory where the picture is saved * @return void* /static Public Function SaveImage ($CSSURL, $dir) {$content = file_get_contents ($CSSURL); $patterns = '/images (. *). jpg|gif|png)/'; The regular change Preg_match_all ($patterns, $content, $matches) according to the different addresses; $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 using a program. Lazy ReadFile ($IMAGEURL); $img = Ob_get_contents (); Ob_end_clean (); $size = strlen ($img); $localImage = $dir. STRCHR ($ Image, '/'); Save to local image address $fp = fopen ($localImage, ' a '); Fwrite ($fp, $img); fclose ($FP);} }}} $content = Getincssimage::saveimage ('/css/css.css ', ' image ');

Finally, I wish you a Mr Skinner in the process, a grilled to the end!

In addition are attached about file_put_contents definition and usage

The file_put_contents () function writes a string to the file.
Same as calling fopen (), fwrite (), and fclose () in turn.

Grammar

File_put_contents (File,data,mode,context)

Parameter description

File required. Specifies the file to write the data to. If the file does not exist, a new file is created.
Data is optional. Specifies the data to write to the file. Can be a string, an array, or a data stream.
Mode
Optional. Specifies how to open/write files. Possible values:
File_use_include_path
File_append
Lock_ex
Context
Optional. Specifies the environment for file handles.
The context is a set of options that modify the behavior of the stream. If NULL is used, it is ignored.

  • Related Article

    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.