Collect remote image images and replace the image address. code _ PHP Tutorial

Source: Internet
Author: User
Collect remote image images and replace the image address code. It is very easy to use php to collect remote images, but we need to filter the collected data. I am afraid we have to use regular expressions, the following program is used to collect remote images. Currently, it is very easy to use php to collect remote images. However, we need to filter the collected data, so I am afraid we have to use regular expressions, the following code is used to collect remote image images and replace the image address.

Code for collecting remote image images and replacing the image address
/*
It is very easy to use php tutorials to collect remote images, but we need to filter the collected data. I am afraid it will be necessary to use regular expressions, the following code is used to collect remote image images and replace the image address.
*/
$ Dir_date = date ('Y/m/D'); // date folder
$ Dir_name = "images/$ dir_date"; // folder path
Function mkdirm ($ path) // Obtain the folder name
{
If (! File_exists ($ path) // determines whether the object exists.
{
Mkdirm (dirname ($ path); // return the directory part of the path.
Mkdir ($ path, 0777); // create folders and permissions
}
}
Mkdirm ($ dir_name); // create a folder
// $ Files = file_get_contents ('http: // www.ywsp.com ');
$ Files = '';
// Convert the image address to $ p = preg_replace ('// image //', 'http: // qq.ip138.com/image/', $ pg [1]).
$ Reg = "/] * src =" (http: // (. +)/(. +). (jpg | gif | bmp | bnp en) "/isu ";
$ Img = preg_match_all ($ reg, $ files, $ imgs );

For ($ I = 0; $ I {

// Echo "". $ imgs [1] [$ I]."
N ";
// Save the image
$ Opts = array (
'Http' => array (
'Header' => "referer: http://www.bkjia.com/rn"
)
);
$ Context = stream_context_create ($ opts );
Header ('content-type: image/jpeg ');
$ Fileresource = @ file_get_contents ($ imgs [1] [$ I], false, $ context );
$ Image_old. = "". $ imgs [1] [$ I]. "". ","; // first, change all URLs to one, such as 1, 2, 3, 4.
$ Image_new. = """. $ dir_name. "/". mktime (). $ I. ". jpg ". """. ","; // first, change all the new image addresses to one address, for example, 1, 2, 4.
File_put_contents ($ dir_name. "/". mktime (). $ I. ". jpg", $ fileresource );
// Save the image
}
$ Image_old = substr ($ image_old, 0, (strlen ($ image_old)-1); // remove the last comma of the URL
$ Image_new = substr ($ image_new, 0, (strlen ($ image_new)-1); // remove the last comma of the new website
// Print_r ($ p );

$ Image_old = explode (",", $ image_old); // separate multiple
$ Image_new = explode (",", $ image_new); // separate multiple

$ Files = str_replace ($ image_old, $ image_new, $ files); // call and replace the URL with the new image URL
Echo $ files;


...

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.