PHP 抓取网页图片并且另存为的实现代码


Posted in PHP onMarch 24, 2010

下面是源代码,及其相关解释

<?php 
//URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字 
//默认把图片放在以此脚本相同的目录里 
function GrabImage($url, $filename=""){ 
//$url 为空则返回 false; 
if($url == ""){return false;} 
$ext = strrchr($url, ".");//得到图片的扩展名 
if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){echo "格式不支持!";return false;} 
if($filename == ""){$filename = time()."$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; 
} 
//测试 
GrabImage("https://3water.com/images/logo.gif", "as.gif"); 
?>

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. (输出是在内部缓冲储存)
//
readfile : 读入一个文件并写入到输出缓冲
返回从文件中读入的字节数。如果出错返回 FALSE 并且除非是以 @readfile() 形式调用,否则会显示错误信息。
//

ob_get_contents : Return the contents of the output buffer(返回输出缓冲的内容)
This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn't active. (如果输出缓冲没有活动(打开),则返回 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.(丢弃并且关掉) 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. (如果要用缓冲内容,则在清理输出缓冲之前要先调用 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).

PHP 相关文章推荐
PHP用户指南-cookies部分
Oct 09 PHP
PHP中的日期处理方法集锦
Jan 02 PHP
实用函数2
Nov 08 PHP
php面向对象全攻略 (十五) 多态的应用
Sep 30 PHP
apache和php之间协同工作的配置经验分享
Apr 08 PHP
php防注入,表单提交值转义的实现详解
Jun 10 PHP
解析php中memcache的应用
Jun 18 PHP
zend Framework中的Layout(模块化得布局)详解
Jun 28 PHP
php function用法如何递归及return和echo区别
Mar 07 PHP
PHP魔术方法__GET、__SET使用实例
Nov 25 PHP
对比PHP对MySQL的缓冲查询和无缓冲查询
Jul 01 PHP
PHP实现的简单异常处理类示例
May 04 PHP
Cakephp 执行主要流程
Mar 24 #PHP
php中的观察者模式
Mar 24 #PHP
PHP 获取远程文件内容的函数代码
Mar 24 #PHP
PHP中基本符号及使用方法
Mar 23 #PHP
PHP技术开发技巧分享
Mar 23 #PHP
PHP初学者常见问题集合 修正版(21问答)
Mar 23 #PHP
PHP5 字符串处理函数大全
Mar 23 #PHP
You might like
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
2013/05/15 PHP
CI框架装载器Loader.php源码分析
2014/11/04 PHP
提高网站性能之 如何对待JavaScript
2009/10/31 Javascript
Extjs在exlipse中设置自动提示的方法
2010/04/07 Javascript
style、 currentStyle、 runtimeStyle区别分析
2010/08/01 Javascript
js实现目录定位正文示例
2013/11/14 Javascript
JavaScript的21条基本知识点
2014/03/04 Javascript
深入理解JavaScript系列(26):设计模式之构造函数模式详解
2015/03/03 Javascript
jQuery插件EnPlaceholder实现输入框提示文字
2015/06/05 Javascript
使用JavaScript判断手机浏览器是横屏还是竖屏问题
2016/08/02 Javascript
chrome浏览器如何断点调试异步加载的JS
2016/09/05 Javascript
JS路由跳转的简单实现代码
2017/09/21 Javascript
react 实现页面代码分割、按需加载的方法
2018/04/03 Javascript
Vue iview-admin框架二级菜单改为三级菜单的方法
2018/07/03 Javascript
vue2.0父子组件间传递数据的方法
2018/08/16 Javascript
jquery更改元素属性attr()方法操作示例
2020/05/22 jQuery
以911新闻为例演示Python实现数据可视化的教程
2015/04/23 Python
Python的几个高级语法概念浅析(lambda表达式闭包装饰器)
2016/05/28 Python
Python编程之列表操作实例详解【创建、使用、更新、删除】
2017/07/22 Python
python编程之requests在网络请求中添加cookies参数方法详解
2017/10/25 Python
Python常见MongoDB数据库操作实例总结
2018/07/24 Python
python爬虫之urllib,伪装,超时设置,异常处理的方法
2018/12/19 Python
为何人工智能(AI)首选Python?读完这篇文章你就知道了(推荐)
2019/04/06 Python
linux中如何使用python3获取ip地址
2019/07/15 Python
Python3以GitHub为例来实现模拟登录和爬取的实例讲解
2020/07/30 Python
达拉斯牛仔官方商店:Dallas Cowboys Pro Shop
2018/02/10 全球购物
世界汽车零件:World Car Parts
2019/09/04 全球购物
大学生在校学习的自我评价
2014/02/18 职场文书
社区禁毒工作方案
2014/06/02 职场文书
企业仓管员岗位职责
2014/06/15 职场文书
商务宴请邀请函范文
2015/02/02 职场文书
培根随笔读书笔记
2015/07/01 职场文书
大学生暑假实习总结
2015/07/13 职场文书
高中优秀作文(范文)
2019/08/15 职场文书
Python如何导出导入所有依赖包详解
2021/06/08 Python
Redis 彻底禁用RDB持久化操作
2021/07/09 Redis