php下载图片到本地

Posted lrxing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php下载图片到本地相关的知识,希望对你有一定的参考价值。

写了一天,就写了这么点代码,凑后用吧。

<?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下载图片到本地的主要内容,如果未能解决你的问题,请参考以下文章

PHP——下载图片到本地代码

php下载图片到本地

微信js-sdk,选择图片,上传,下载到本地,php服务端

php PHP下载远程图片保存到本地

PHP 将某个http地址的远程图片下载到本地的某个目录

php下载远程图片到本地