php下載圖片到本地
阿新 • • 發佈:2017-09-12
commons 測試的 lod aws == ani posit anim cat
寫了一天,就寫了這麽點代碼,湊後用吧。
<?php
/**
* @filename saveImage.php
*/ function getImgName($url) { if (!preg_match(‘/\/([^\/]+\.[a-z]{3,4})(\?.*?)?$/i‘,$url, $matches)) { return null; } $image_name = strtolower($matches[1]); return $image_name; } function saveImage($url, $path) {global $debuger; $handle = curl_init ($url); /* 顯示響應頭信息 */ curl_setopt($handle, CURLOPT_HEADER, true); curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); $img = curl_exec ($handle); $file_size = curl_getinfo($handle, CURLINFO_SIZE_DOWNLOAD); $http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE); curl_close ($handle); list ($header, $body) = explode("\r\n\r\n", $img, 2); wlog ("http code: $http_code"); if ($http_code == 301 || $http_code == 302) { wlog ("[$url]重定向..."); $matches = array(); if (!preg_match(‘/(Location:|URI:)(.*?)\n/‘, $header, $matches)) { wlog (‘解析頭信息失敗,結束。‘); return false; } $redirect_url = trim(array_pop($matches)); $url_parsed = parse_url($redirect_url); if (isset($url_parsed)) { wlog ("已獲取重定向地址[$redirect_url],\n正在跳轉..."); return saveImage($redirect_url, $path); } else { wlog (‘獲取重定向地址失敗,結束。‘); return false; } } elseif ($http_code == 200) { wlog (‘請求成功...‘); } else { wlog (‘無效的請求,結束。‘); return false; } $image_name = getImgName($url); if ($image_name == null) { if (preg_match (‘/Content-Disposition:.*?filename="([^"]+)".*?\n/‘, $header, $matches) && !empty($matches[1])) { $image_name = $matches[1]; } else { wlog (‘無效的圖片地址!‘); return false; } } if (!file_exists ($path)) { wlog ("目錄$path不存在,正在創建..."); if (mkdir ($path)) { wlog (‘目錄創建成功...‘); } else { wlog (‘目錄創建失敗,結束。‘); return false; } } $file_path = rtrim ($path, ‘/‘) . ‘/‘ . $image_name; $fp = fopen ($file_path, ‘w‘); $length = fwrite ($fp, $body); fclose ($fp); if ($length) { wlog ("文件保存成功!\n大小: $length\n位置: $file_path"); } else { wlog (‘文件保存失敗。‘); return false; } return true; } function wlog ($msg, $file_path = ‘‘) { if (empty ($file_path)) { $file_path = ‘log/save_img.log‘; } if (!file_exists (dirname ($file_path))) { if (!mkdir (dirname ($file_path))) { die(‘can not create directory‘ . dirname ($file_path)); } } $fp = @fopen ($file_path, ‘a‘); flock ($fp, LOCK_EX); fwrite ($fp, $msg . "\n"); flock ($fp, LOCK_UN); fclose ($fp); }
調用的時候直接用saveImage($url, $path)就可以了。
寫了個測試的腳本,把常用的圖片格式都試了一遍,都可以通過
<?php
/**
* @filename test.php
*/ define(‘PATH‘, ‘download/‘); require(‘class_inc/downloadImg.php‘); $img_list = [‘png‘ => ‘http://www.freepngimg.com/download/facebook/1-2-facebook-download-png.png‘, ‘gif‘ => ‘https://kanimg.9ku.com/Article/20170725/1500953725469381.gif‘, ‘jpg & redirect‘ => ‘http://pic2116.ytqmx.com:82/2017/0725/37/4.jpg‘, ‘jpeg‘ => ‘https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Soviet_BMP-1_IFV.JPEG/300px-Soviet_BMP-1_IFV.JPEG‘, ‘gif‘ => ‘http://n.sinaimg.cn/sports/transform/20170906/W34l-fykpyua5747968.gif‘, ‘jpg with params‘ => ‘http://img.freepik.com/free-icon/bmp-image-file-type-outlined-interface-symbol_318-72075.jpg?size=338&ext=jpg‘, ‘webp‘ => ‘http://www.gstatic.com/webp/gallery/1.webp‘, ‘bmp‘ => ‘http://samples.fileformat.info/format/bmp/sample/4cb74cda027a43f3b278c05c3770950f/MARBLES.BMP?AWSAccessKeyId=0V91BEFA7GM093MEVMG2&Signature=t18N8JiZUw0QOd%2FZcyK8oZAwUWk%3D&Expires=1505213123‘, ‘bmp1‘ => ‘http://cfile8.uf.tistory.com/image/167475304C879B427393BA‘ ]; foreach ($img_list as $type => $url) { echo "download image $type:"; if (test($url, PATH)) { echo "done\n"; } else { echo "failed\n"; } } function test($url, $path) { saveImage($url, $path); $file_name = getImgName($url); $file_path = rtrim($path) . ‘/‘ . $file_name; if (file_exists($file_path)) { return true; } return false; }
php下載圖片到本地