Now it is very simple to use PHP to collect remote images, but we need to filter the collected data, which may require the use of regular expressions. The following program collects remote images and replaces the image address code.
Collect remote images and replace image address codes
/*
Now it is very simple to use php tutorial to collect remote images, but we need to filter the collected data, which may require the use of regular rules expression, the following program collects remote images and replaces the image address code.
*/
$dir_date=date('y/m/d');//Date folder
$dir_name="images/$dir_date";//Folder path
function mkdirm ($path) //Get the folder name
{
if (!file_exists($path)) //Determine whether it exists
{
mkdirm(dirname($path)); //Return Directory part of the path
mkdir($path, 0777); //Create folder and permissions
}
}
mkdirm($dir_name);//Create folder
// $files = file_get_contents('http://www.ywsp.com');
$files ='';
// Convert the image address $p=preg_replace('//image//', 'http://qq.ip138.com/image/', $pg[1] );
$reg = "/]*src="(http://(.+)/(.+).(jpg|gif|bmp|bnp))"/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 into a number such as 1,2,3,4
$image_new.="" ".$dir_name."/".mktime().$i.".jpg".""".","; //First change all new image addresses into a number such as 1,2,3,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 from the URL
$image_new=substr($image_new,0,(strlen($image_new)-1));//Remove the new URL Remove the last comma
//print_r($p);
$image_old=explode(",",$image_old);//Split into multiples
$image_new=explode("," ,$image_new);//Split multiple
$files=str_replace($image_old,$image_new,$files); //Call the URL and replace it with the new image URL
echo $files;