php 保存远程图片到本地
来源:互联网 发布:英语动画配音软件 编辑:程序博客网 时间:2024/06/05 06:19
http://justcoding.iteye.com/blog/856425#bc2358473
http://www.521admin.com/bbs/thread-92-1.html
php 保存远程图片到本地
- 博客分类:
- Php / Pear / Mysql / Node.js
PHPEXTUP
显示远程图片:
- <?php
- header('Content-Type:image/jpg');
- echo file_get_contents("http://www.baidu.com/img/baidu_logo.gif");
- ?>
第一种: 精确型
- <?php
- // 变量说明:
- // $url 是远程图片的完整URL地址,不能为空。
- // $filename 是可选变量: 如果为空,本地文件名将基于时间和日期
- // 自动生成.
- function get_photo($url,$filename='',$savefile='test/')
- {
- $imgArr = array('gif','bmp','png','ico','jpg','jepg');
- if(!$url) return false;
- if(!$filename) {
- $ext=strtolower(end(explode('.',$url)));
- if(!in_array($ext,$imgArr)) return false;
- $filename=date("dMYHis").'.'.$ext;
- }
- if(!is_dir($savefile)) mkdir($savefile, 0777);
- if(!is_readable($savefile)) chmod($savefile, 0777);
- $filename = $savefile.$filename;
- 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;
- }
- $img=get_photo("http://www.baidu.com/img/baidu_logo.gif");
- echo $img ? '<pre><img src="'.$img.'"></pre>' : "false";
第二种:从文章中提取图片,并保存至本地
- function getImg($str){
- $str = stripslashes($str);
- $pattern = "/<img[^>]*src\=\"(([^>]*)(jpg|gif|png|bmp|jpeg))\"/i"; //获取所有图片标签的全部信息
- preg_match_all($pattern, $str, $matches);
- return $matches[1]; //$matches[1]中就是所想匹配的结果,结果为数组
- }
- $str = <<<EOT
- Money has been moving into Brazilian stocks over the last couple of days, despite mostly flat trading activity in the U.S. equity markets. During Thursday's session, the iShares MSCI Brazil Index ETF (NYSE: EWZ) has risen 1.12% after outperforming the U.S. indices yesterday as well.
- Must Read
- SonySony Playstation Phone Rumors Heat Up
- A South Korean Army soldier walks up steps of a guard post near the demilitarised zone separating the two Koreas.S. Korea waves olive branch
- <img width="1" height="1" alt="" src="http://img.ibtimes.com/www/site/us/images/1px.gif" sized="yes">
- Two giant resource companies make up a big chunk of the Brazilian Bovespa. They are PetroBras (NYSE: PBR), with a market cap of $163.44 billion, and miner Vale (NYSE: VALE), which has a market cap of $178.95 billion. PBR shares have risen 1.47% thus far today and VALE is trading 0.77% higher at $34.03.
- <img alt="Sony" src="http://img.ibtimes.com/www/thumb/mainpage/13463-12079-sony.jpg" sized="yes">
- This article was originally published on Benzinga, and is republished here with permission.
- EOT;
- foreach( getImg($str) as $url)
- {
- get_photo($url);
- }
php下保存远程图片到本地的办法
- function GrabImage($url,$filename="") {
- if($url=="") return false;
- if($filename=="") {
- $ext=strrchr($url,".");
- if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
- $filename=date("YmdHis").$ext;
- }
- 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;
- }
获取一张图片的代码:
- $img=GrabImage("http://www.baidu.com/img/baidu_logo.gif","logo.gif");
- if($img){
- echo '<img src="'.$img.'">';
- }else{
- echo "false";
- }
这是保存google的logo的例子,获取到的图片保存在同级目录下面。
获取一系列的有规律的图片(例如:以数字1-100命名的100张图片):
- for ($i=1;$i<=100;$i++){
- $img=GrabImage("http://www.yourimagesite.com/images/$i.gif","images/$i.gif");
- }
0 0
- PHP保存远程图片到本地
- php保存远程图片到本地
- php 保存远程图片到本地
- PHP获取远程图片保存到本地
- PHP抓取远程图片到本地保存
- php保存远程图片到本地
- 保存远程图片到本地
- PHP保存远程图片到本地的类
- PHP保存远程图片到本地的办法
- PHP抓取远程图片保存到本地的实现方法
- php下保存远程图片到本地的办法
- php保存文章中远程图片到本地
- php获取远程图片并下载保存到本地
- php获取远程图片并把它保存到本地
- PHP获取远程图片并保存到本地
- php获取远程图片并下载保存到本地
- php完美实现下载远程图片保存到本地
- php获取远程图片并把它保存到本地
- android TextView EditTextView一些技巧使用 (视图代码布局)
- php 操作数组 (合并,拆分,追加,查找,删除等)
- cvc-id.2: There are multiple occurrences of ID value 'copyOfSysDictServiceImp'.
- 引用静态库中的资源文件(Qt下添加*.qrc资源)
- Java。UnSafe类
- php 保存远程图片到本地
- Windows 异常
- iOS开发asi使用(一)ASIHttpRequest创建和执行request
- Freebsd下面安装SNMP
- 如何禁止微软修改浏览器主页为123.msn?
- Mob免费短信验证初探
- CalTech machine learning, video 13 note(validation)
- 关于 for update
- 网址末尾反斜杠的意义