PHP實現下載遠端圖片儲存到本地的方法
阿新 • • 發佈:2018-12-21
PHP實現下載遠端圖片儲存到本地的方法
1.使用 curl
比如我們有下面這兩張圖片:
$images = [
'https://dn-laravist.qbox.me/2015-09-22_00-17-06j.png',
'https://dn-laravist.qbox.me/2015-09-23_00-58-03j.png'
];
第一步,我們可以直接來使用最簡單的程式碼實現:
function download($url, $path = 'images/') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); $filename = pathinfo($url, PATHINFO_BASENAME); $resource = fopen($path . $filename, 'a'); fwrite($resource, $file); fclose($resource); }
那在下載遠端圖片的時候就可以這樣:
foreach ( $images as $url ) {
download($url);
}
2.封裝一個類
縷清思路之後,我們可以將這個基本的功能封裝到一個類中:
class Spider { public function downloadImage($url, $path = 'images/') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); $filename = pathinfo($url, PATHINFO_BASENAME); $resource = fopen($path . $filename, 'a'); fwrite($resource, $file); fclose($resource); } }
在者,我們還可以這樣稍微優化一下:
public function downloadImage($url, $path='images/') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); $this->saveAsImage($url, $file, $path); } private function saveAsImage($url, $file, $path) { $filename = pathinfo($url, PATHINFO_BASENAME); $resource = fopen($path . $filename, 'a'); fwrite($resource, $file); fclose($resource); }
封裝成類之後,我們可以這樣呼叫程式碼來下載圖片:
$spider = new Spider(); foreach ( $images as $url ) { $spider->downloadImage($url); }
轉載地址: PHP實現下載遠端圖片儲存到本地的方法