PHP 文章中的遠(yuǎn)程圖片采集到本地的代碼
更新時(shí)間:2009年07月30日 01:23:41 作者:
今天寫了一個(gè)這個(gè)功能, 拿出來跟朋友一起分享,可以獲取遠(yuǎn)程圖片并保存到本地,其實(shí)大家可以參考很多php管理系統(tǒng)都有這個(gè)功能。
第一步. 先從文章中把所有<img ...> 用正則 摳出來.
$message //文章內(nèi)容
//正則(這個(gè)還不是)
$reg = "/<img[^>]*src=\"(http:\/\/(.+)\/(.+)\.(jpg|gif|bmp|bnp))\"/isU";
//把摳出來的 img 地址存放到 $img_array 變量中
preg_match_all($reg, $message, $img_array, PREG_PATTERN_ORDER);
//過濾重復(fù)的圖片
$img_array = array_unique($img_array[1]);
第二步. 把$img_array 數(shù)組循環(huán)一下. 做圖片保存和文章位置替換
foreach ($img_array as $img){
//判斷是否是自己網(wǎng)站上的 圖片
if('xxx.com' != get_domain($img)){// 如果這個(gè)圖片不是自己服務(wù)器上的
//讀取圖片文件
$Gimg = new GetImage();
$Gimg->source = $img;
$Gimg->save_to = './data/temp/';
$FILE = $Gimg->download(); //圖片移動(dòng)到本地
//保存到相冊(cè) 得到圖片保存的位置
$img_path = pic_save($FILE,0,'');
//文本路徑替換
$message = str_replace($img, $img_path, $message);
}
}
....這時(shí)候 $message 里面已經(jīng)圖片已經(jīng)替換為自己服務(wù)器本地的地址,并且圖片也保存到自己的服務(wù)器上.
//下面一個(gè)函數(shù) 和 類是從網(wǎng)絡(luò)上找的.
//從url中獲得域名
function get_domain($url){
$pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn)(\.(cn|hk))*/";
preg_match($pattern, $url, $matches);
if(count($matches) > 0) {
return $matches[0];
}else{
$rs = parse_url($url);
$main_url = $rs["host"];
if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
return $main_url;
}else{
$arr = explode(".",$main_url);
$count=count($arr);
$endArr = array("com","net","org","3322");//com.cn net.cn 等情況
if (in_array($arr[$count-2],$endArr)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}// end if(!strcmp...)
}// end if(count...)
}// end function
// 從遠(yuǎn)程吧圖片載到服務(wù)器本地 的 類
class GetImage {
var $source;
var $save_to;
var $quality;
function download($method = 'curl') {
$info = @GetImageSize($this->source);
$mime = $info['mime'];
// What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type){
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
// Best Quality: 100
$quality = isSet($this->quality) ? $this->quality : 100;
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
// Compression Level: from 0 (no compression) to 9
$quality = isSet($this->quality) ? $this->quality : 0;
break;
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break;
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break;
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break;
default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}
if(isSet($this->set_extension)){
$ext = strrchr($this->source, ".");
$strlen = strlen($ext);
$new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
}else{
$new_name = basename($this->source);
}
$save_to = $this->save_to."/blog_insert_temp_".time().mt_rand(1,99).".".$new_image_ext;
//輸出對(duì)象 組成跟$_FILE變量一樣 得到后自己和平常圖片上傳處理一樣了
$img_info['name'] = basename($this->source);
$img_info['type'] = $mime;
$img_info['size'] = 1000;
$img_info['tmp_name'] = $save_to;
$img_info['error'] = 0;
if($method == 'curl'){
$save_image = $this->LoadImageCURL($save_to);
}elseif($method == 'gd'){
$img = $image_create_func($this->source);
if(isSet($quality)){
$save_image = $image_save_func($img, $save_to, $quality);
}else{
$save_image = $image_save_func($img, $save_to);
}
}
return $img_info;
}
function LoadImageCURL($save_to){
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");
// set URL and other appropriate options
$options = array(CURLOPT_FILE => $fp,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
復(fù)制代碼 代碼如下:
$message //文章內(nèi)容
//正則(這個(gè)還不是)
$reg = "/<img[^>]*src=\"(http:\/\/(.+)\/(.+)\.(jpg|gif|bmp|bnp))\"/isU";
//把摳出來的 img 地址存放到 $img_array 變量中
preg_match_all($reg, $message, $img_array, PREG_PATTERN_ORDER);
//過濾重復(fù)的圖片
$img_array = array_unique($img_array[1]);
第二步. 把$img_array 數(shù)組循環(huán)一下. 做圖片保存和文章位置替換
復(fù)制代碼 代碼如下:
foreach ($img_array as $img){
//判斷是否是自己網(wǎng)站上的 圖片
if('xxx.com' != get_domain($img)){// 如果這個(gè)圖片不是自己服務(wù)器上的
//讀取圖片文件
$Gimg = new GetImage();
$Gimg->source = $img;
$Gimg->save_to = './data/temp/';
$FILE = $Gimg->download(); //圖片移動(dòng)到本地
//保存到相冊(cè) 得到圖片保存的位置
$img_path = pic_save($FILE,0,'');
//文本路徑替換
$message = str_replace($img, $img_path, $message);
}
}
....這時(shí)候 $message 里面已經(jīng)圖片已經(jīng)替換為自己服務(wù)器本地的地址,并且圖片也保存到自己的服務(wù)器上.
復(fù)制代碼 代碼如下:
//下面一個(gè)函數(shù) 和 類是從網(wǎng)絡(luò)上找的.
//從url中獲得域名
function get_domain($url){
$pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn)(\.(cn|hk))*/";
preg_match($pattern, $url, $matches);
if(count($matches) > 0) {
return $matches[0];
}else{
$rs = parse_url($url);
$main_url = $rs["host"];
if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
return $main_url;
}else{
$arr = explode(".",$main_url);
$count=count($arr);
$endArr = array("com","net","org","3322");//com.cn net.cn 等情況
if (in_array($arr[$count-2],$endArr)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}// end if(!strcmp...)
}// end if(count...)
}// end function
// 從遠(yuǎn)程吧圖片載到服務(wù)器本地 的 類
class GetImage {
var $source;
var $save_to;
var $quality;
function download($method = 'curl') {
$info = @GetImageSize($this->source);
$mime = $info['mime'];
// What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type){
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
// Best Quality: 100
$quality = isSet($this->quality) ? $this->quality : 100;
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
// Compression Level: from 0 (no compression) to 9
$quality = isSet($this->quality) ? $this->quality : 0;
break;
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break;
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break;
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break;
default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}
if(isSet($this->set_extension)){
$ext = strrchr($this->source, ".");
$strlen = strlen($ext);
$new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
}else{
$new_name = basename($this->source);
}
$save_to = $this->save_to."/blog_insert_temp_".time().mt_rand(1,99).".".$new_image_ext;
//輸出對(duì)象 組成跟$_FILE變量一樣 得到后自己和平常圖片上傳處理一樣了
$img_info['name'] = basename($this->source);
$img_info['type'] = $mime;
$img_info['size'] = 1000;
$img_info['tmp_name'] = $save_to;
$img_info['error'] = 0;
if($method == 'curl'){
$save_image = $this->LoadImageCURL($save_to);
}elseif($method == 'gd'){
$img = $image_create_func($this->source);
if(isSet($quality)){
$save_image = $image_save_func($img, $save_to, $quality);
}else{
$save_image = $image_save_func($img, $save_to);
}
}
return $img_info;
}
function LoadImageCURL($save_to){
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");
// set URL and other appropriate options
$options = array(CURLOPT_FILE => $fp,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
您可能感興趣的文章:
- PHP多線程批量采集下載美女圖片的實(shí)現(xiàn)代碼(續(xù))
- PHP批量采集下載美女圖片的實(shí)現(xiàn)代碼
- php curl簡單采集圖片生成base64編碼(并附curl函數(shù)參數(shù)說明)
- PHP CURL采集百度搜尋結(jié)果圖片不顯示問題的解決方法
- php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法
- PHP遠(yuǎn)程采集圖片詳細(xì)教程
- PHP采集類Snoopy抓取圖片實(shí)例
- phpphp圖片采集后按原路徑保存圖片示例
- php采集文章中的圖片獲取替換到本地(實(shí)現(xiàn)代碼)
- PHP下載采集圖片到本地的方法詳解【可忽略ssl認(rèn)證】
相關(guān)文章
ThinkPHP實(shí)現(xiàn)支付寶接口功能實(shí)例
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)支付寶接口功能的方法,實(shí)例講述了支付寶接口的下載及二次開發(fā)方法,以及對(duì)應(yīng)的ThinkPHP開發(fā)技巧,需要的朋友可以參考下2014-12-12如何使用PHP file_exists函數(shù)檢查文件是否存在
這篇文章主要為大家介紹了PHP函數(shù)file_exists檢查文件是否存在實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01PHP實(shí)現(xiàn)簡易用戶登錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)簡易用戶登錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07PHP的Laravel框架中使用消息隊(duì)列queue及異步隊(duì)列的方法
這篇文章主要介紹了PHP的Laravel框架中使用消息隊(duì)列queue及異步隊(duì)列的方法,針對(duì)Laravel 5.0后的版本,示例環(huán)境為Linux系統(tǒng),需要的朋友可以參考下2016-03-03Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法
這篇文章主要介紹了Laravel使用scout集成elasticsearch做全文搜索的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11TP5(thinkPHP5框架)實(shí)現(xiàn)顯示錯(cuò)誤信息及行號(hào)功能的方法
這篇文章主要介紹了TP5(thinkPHP5框架)實(shí)現(xiàn)顯示錯(cuò)誤信息及行號(hào)功能的方法,結(jié)合實(shí)例形式分析了thinkPHP5顯示錯(cuò)誤信息及行號(hào)的相關(guān)配置文件與設(shè)置修改方法,需要的朋友可以參考下2019-06-06PHP+ajaxfileupload+jcrop插件完美實(shí)現(xiàn)頭像上傳剪裁
在做項(xiàng)目的時(shí)候,經(jīng)常需要一些會(huì)員系統(tǒng)相關(guān)的內(nèi)容,比如頭像的上傳與裁剪等等,下面將這塊內(nèi)容分享給大家2014-06-06