日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

PHP 抓取网页图片并且另存为

發布時間:2023/12/15 综合教程 30 生活家
生活随笔 收集整理的這篇文章主要介紹了 PHP 抓取网页图片并且另存为 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面是源代碼,及其相關解釋

 1 <?php
2
3 //URL是遠程的完整圖片地址,不能為空, $filename 是另存為的圖片名字
4 //默認把圖片放在以此腳本相同的目錄里
5 function GrabImage($url, $filename=""){
6
7 //$url 為空則返回 false;
8 if($url == ""){return false;}
9 $ext = strrchr($url, ".");//得到圖片的擴展名
10 if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){echo "格式不支持!";return false;}
11 if($filename == ""){$filename = time()."$ext";}//以時間戳另起名
12 //開始捕捉
13 ob_start();
14 readfile($url);
15 $img = ob_get_contents();
16 ob_end_clean();
17 $size = strlen($img);
18 $fp2 = fopen($filename , "a");
19 fwrite($fp2, $img);
20 fclose($fp2);
21 return $filename;
22
23 }
24 //測試
25 GrabImage("http://www.66xing.com/UploadFile/200609082320515027.bmp", "as.gif");
26
27 ?>

相關描述:

ob_start : 打開輸出緩沖
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. (輸出是在內部緩沖儲存)
 //
readfile : 讀入一個文件并寫入到輸出緩沖
返回從文件中讀入的字節數。如果出錯返回 FALSE 并且除非是以 @readfile() 形式調用,否則會顯示錯誤信息。 
//

ob_get_contents : Return the contents of the output buffer(返回輸出緩沖的內容)
This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn't active. (如果輸出緩沖沒有活動(打開),則返回 FALSE)
//
ob_end_clean() : Clean (erase) the output buffer and turn off output buffering(清除輸出緩沖)
This function discards(丟棄) the contents of the topmost output buffer and turns off this output buffering.(丟棄并且關掉) If you want to further process the buffer's contents you have to call ob_get_contents() before ob_end_clean() as the buffer contents are discarded when ob_end_clean() is called. (如果要用緩沖內容,則在清理輸出緩沖之前要先調用 ob_get_contents())The function returns TRUE when it successfully discarded one buffer and FALSE otherwise. Reasons for failure are first that you called the function without an active buffer or that for some reason a buffer could not be deleted (possible for special buffer). 

總結

以上是生活随笔為你收集整理的PHP 抓取网页图片并且另存为的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。