PHP抓取遠(yuǎn)程圖片并另存為的實(shí)現(xiàn)方法
來(lái)源:易賢網(wǎng) 閱讀:2099 次 日期:2014-08-21 17:14:21
溫馨提示:易賢網(wǎng)小編為您整理了“PHP抓取遠(yuǎn)程圖片并另存為的實(shí)現(xiàn)方法”,方便廣大網(wǎng)友查閱!

下面是源代碼,及其相關(guān)解釋

//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í)間戳另起名

//開始捕捉

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.66xing.com/UploadFile/200609082320515027.bmp", "as.gif");

?>

相關(guān)描述:

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. (輸出是在內(nèi)部緩沖儲(chǔ)存)

//

readfile : 讀入一個(gè)文件并寫入到輸出緩沖

返回從文件中讀入的字節(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)(打開),則返回 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).

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP抓取遠(yuǎn)程圖片并另存為的實(shí)現(xiàn)方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)