PHP 抓取網(wǎng)頁(yè)圖片并且另存為的實(shí)現(xiàn)代碼
更新時(shí)間:2010年03月24日 20:15:09 作者:
URL是遠(yuǎn)程的完整圖片地址,不能為空, $filename 是另存為的圖片名字 默認(rèn)把圖片放在以此腳本相同的目錄里
下面是源代碼,及其相關(guān)解釋
<?php
//URL是遠(yuǎn)程的完整圖片地址,不能為空, $filename 是另存為的圖片名字
//默認(rèn)把圖片放在以此腳本相同的目錄里
function GrabImage($url, $filename=""){
//$url 為空則返回 false;
if($url == ""){return false;}
$ext = strrchr($url, ".");//得到圖片的擴(kuò)展名
if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){echo "格式不支持!";return false;}
if($filename == ""){$filename = time()."$ext";}//以時(shí)間戳另起名
//開(kāi)始捕捉
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2 = fopen($filename , "a");
fwrite($fp2, $img);
fclose($fp2);
return $filename;
}
//測(cè)試
GrabImage("http://www.dbjr.com.cn/images/logo.gif", "as.gif");
?>
ob_start : 打開(kāi)輸出緩沖
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. (輸出是在內(nèi)部緩沖儲(chǔ)存)
//
readfile : 讀入一個(gè)文件并寫(xiě)入到輸出緩沖
返回從文件中讀入的字節(jié)數(shù)。如果出錯(cuò)返回 FALSE 并且除非是以 @readfile() 形式調(diào)用,否則會(huì)顯示錯(cuò)誤信息。
//
ob_get_contents : Return the contents of the output buffer(返回輸出緩沖的內(nèi)容)
This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn't active. (如果輸出緩沖沒(méi)有活動(dòng)(打開(kāi)),則返回 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.(丟棄并且關(guān)掉) 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. (如果要用緩沖內(nèi)容,則在清理輸出緩沖之前要先調(diào)用 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).
復(fù)制代碼 代碼如下:
<?php
//URL是遠(yuǎn)程的完整圖片地址,不能為空, $filename 是另存為的圖片名字
//默認(rèn)把圖片放在以此腳本相同的目錄里
function GrabImage($url, $filename=""){
//$url 為空則返回 false;
if($url == ""){return false;}
$ext = strrchr($url, ".");//得到圖片的擴(kuò)展名
if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){echo "格式不支持!";return false;}
if($filename == ""){$filename = time()."$ext";}//以時(shí)間戳另起名
//開(kāi)始捕捉
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2 = fopen($filename , "a");
fwrite($fp2, $img);
fclose($fp2);
return $filename;
}
//測(cè)試
GrabImage("http://www.dbjr.com.cn/images/logo.gif", "as.gif");
?>
ob_start : 打開(kāi)輸出緩沖
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. (輸出是在內(nèi)部緩沖儲(chǔ)存)
//
readfile : 讀入一個(gè)文件并寫(xiě)入到輸出緩沖
返回從文件中讀入的字節(jié)數(shù)。如果出錯(cuò)返回 FALSE 并且除非是以 @readfile() 形式調(diào)用,否則會(huì)顯示錯(cuò)誤信息。
//
ob_get_contents : Return the contents of the output buffer(返回輸出緩沖的內(nèi)容)
This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn't active. (如果輸出緩沖沒(méi)有活動(dòng)(打開(kāi)),則返回 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.(丟棄并且關(guān)掉) 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. (如果要用緩沖內(nèi)容,則在清理輸出緩沖之前要先調(diào)用 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).
您可能感興趣的文章:
- 一個(gè)PHP的遠(yuǎn)程圖片抓取函數(shù)分享
- 分享PHP源碼批量抓取遠(yuǎn)程網(wǎng)頁(yè)圖片并保存到本地的實(shí)現(xiàn)方法
- 基于php實(shí)現(xiàn)七牛抓取遠(yuǎn)程圖片
- PHP抓取遠(yuǎn)程圖片(含不帶后綴的)教程詳解
- php抓取并保存網(wǎng)站圖片的實(shí)現(xiàn)代碼
- PHP采集類(lèi)Snoopy抓取圖片實(shí)例
- 抓取并下載CSS中所有圖片文件的php代碼
- PHP抓取淘寶商品的用戶(hù)曬單評(píng)論+圖片+搜索商品列表實(shí)例
- PHP通過(guò)CURL實(shí)現(xiàn)定時(shí)任務(wù)的圖片抓取功能示例
- php抓取網(wǎng)站圖片并保存的實(shí)現(xiàn)方法
- PHP封裝的遠(yuǎn)程抓取網(wǎng)站圖片并保存功能類(lèi)
相關(guān)文章
PHP實(shí)現(xiàn)識(shí)別復(fù)雜pdf文檔的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用PHP實(shí)現(xiàn)識(shí)別復(fù)雜pdf文檔的功能,文中的示例代碼講解詳細(xì),感興趣的徐海波可以跟隨小編一起學(xué)習(xí)一下2024-01-01PhpMyAdmin中無(wú)法導(dǎo)入sql文件的解決辦法
PhpMyAdmin中無(wú)法導(dǎo)入sql文件的解決辦法2010-01-01PHP+Swoole實(shí)現(xiàn)web版的shell客戶(hù)端詳解
這篇文章主要為大家詳細(xì)介紹了如何利用PHP+Swoole實(shí)現(xiàn)web版的shell客戶(hù)端,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-07-07PHP生成excel時(shí)單元格內(nèi)換行問(wèn)題的解決方法
今天 客戶(hù)提問(wèn)題說(shuō),導(dǎo)出的excel文件,該換行的單元格沒(méi)有換行。通過(guò)查找資源找到了解決方法。2010-08-08原生JS實(shí)現(xiàn)Ajax通過(guò)POST方式與PHP進(jìn)行交互的方法示例
這篇文章主要介紹了原生JS實(shí)現(xiàn)Ajax通過(guò)POST方式與PHP進(jìn)行交互的方法,涉及ajax使用post方式與后臺(tái)交互及php數(shù)據(jù)接收、處理、查詢(xún)數(shù)據(jù)庫(kù)等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05php性能優(yōu)化之不要在for循環(huán)中操作DB
這篇文章主要為大家介紹了php性能優(yōu)化之不要在for循環(huán)中操作DB示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06