php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法
更新時(shí)間:2015年01月03日 15:57:39 投稿:shichen2014
這篇文章主要介紹了php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法,可實(shí)現(xiàn)采集并保存遠(yuǎn)程圖片的功能,是非常實(shí)用的技巧,需要的朋友可以參考下
本文實(shí)例講述了php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
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;
}
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;
}
復(fù)制代碼 代碼如下:
function get_remote($body,$title){
$img_array = array();
$img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集遠(yuǎn)程圖片保存地址
//die($img_path);
$img_rpath='/upfile/news/'.date("y/m/d/"); //設(shè)置訪問地址
$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("<img","<img ",$body);
return $body;
}
function mkdirs($dir)
{
if(!is_dir($dir)){
if(!mkdirs(dirname($dir))){
return false;}
if(!mkdir($dir,0777)){
return false;}
}
return true;
}
//用法如下:
$str ='fasfsdafsa<img src=http://filesimg.xxxx.com/2010/03/2010062300391582.jpg />';
echo get_remote($str,'圖片');
$img_array = array();
$img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集遠(yuǎn)程圖片保存地址
//die($img_path);
$img_rpath='/upfile/news/'.date("y/m/d/"); //設(shè)置訪問地址
$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("<img","<img ",$body);
return $body;
}
function mkdirs($dir)
{
if(!is_dir($dir)){
if(!mkdirs(dirname($dir))){
return false;}
if(!mkdir($dir,0777)){
return false;}
}
return true;
}
//用法如下:
$str ='fasfsdafsa<img src=http://filesimg.xxxx.com/2010/03/2010062300391582.jpg />';
echo get_remote($str,'圖片');
希望本文所述對大家的php程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- PHP多線程批量采集下載美女圖片的實(shí)現(xiàn)代碼(續(xù))
- PHP批量采集下載美女圖片的實(shí)現(xiàn)代碼
- php curl簡單采集圖片生成base64編碼(并附curl函數(shù)參數(shù)說明)
- PHP CURL采集百度搜尋結(jié)果圖片不顯示問題的解決方法
- PHP遠(yuǎn)程采集圖片詳細(xì)教程
- PHP采集類Snoopy抓取圖片實(shí)例
- phpphp圖片采集后按原路徑保存圖片示例
- php采集文章中的圖片獲取替換到本地(實(shí)現(xiàn)代碼)
- PHP 文章中的遠(yuǎn)程圖片采集到本地的代碼
- PHP下載采集圖片到本地的方法詳解【可忽略ssl認(rèn)證】
相關(guān)文章
php設(shè)計(jì)模式 Mediator (中介者模式)
用一個(gè)中介對象來封裝一系列的對象交互,使各對象不需要顯式地相互引用從而使其耦合松散,而且可以獨(dú)立地改變它們之間的交互2011-06-06