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;
...