Home > Backend Development > PHP Tutorial > How to collect remote images with image addresses in the content and save them_PHP Tutorial

How to collect remote images with image addresses in the content and save them_PHP Tutorial

WBOY
Release: 2016-07-13 09:59:40
Original
1003 people have browsed it

How to collect and save remote images with image addresses in php content

This article mainly introduces how php collects and saves remote images with image addresses in content. This method can realize the function of collecting and saving remote pictures. It is a very practical skill. Friends who need it can refer to it

The example in this article describes the method of collecting and saving remote images with image addresses in PHP content. Share it with everyone for your reference. The specific implementation method is as follows:

The code is as follows:

function my_file_get_contents($url, $timeout=30) {
if ( function_exists('curl_init') )
{
$ch = curl_init();
curl_setopt ($ch, curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )
{
$file_contents = @file_get_contents($url);
}
else
{
$file_contents = '';
}
return $file_contents;
}

The code is as follows:

function get_remote($body,$title){

$img_array = array();
$img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //Collect remote image storage address
//die($img_path);
$img_rpath='/upfile/news/'.date("y/m/d/"); //Set the access address
$body = stripslashes(strtolower($body));
preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array) ;
$img_array = array_unique($img_array[2]);
foreach ($img_array as $key => $value) {
$get_file = my_file_get_contents($value,60);
$filetime = time();
$filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);
if(emptyempty($get_file)){
@sleep(10);
$get_file = my_file_get_contents($value,30);
if(emptyempty($get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
continue;
}
}
if(!emptyempty($get_file) ){
if( mkdirs($img_path) )
{
$fp = fopen($img_path.$filename,"w");
if(fwrite($fp,$get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);
}
fclose($fp);
@sleep(6);
}
}

}
$body =str_replace(" return $body;

}

function mkdirs($dir)
{
if(!is_dir($dir)){
if(!mkdirs(dirname($dir))){
return false;}
if(!mkdir($dir,0777)){
return false;}
}
return true;
}
//Usage is as follows:

$str ='fasfsdafsa';
echo get_remote($str,'picture');

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975893.htmlTechArticleHow to collect and save remote images with image addresses in php content. This article mainly introduces how to collect content in php The method of saving remote pictures with picture addresses can realize collection...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template