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
Nov 25 PHP
PHP 常见郁闷问题答解
Nov 25 PHP
上传多个文件的PHP脚本
Nov 26 PHP
php中的strpos使用示例
Feb 27 PHP
php快递单号查询接口使用示例
May 05 PHP
PHP永久登录、记住我功能实现方法和安全做法
Apr 27 PHP
PHP session会话操作技巧小结
Sep 27 PHP
PHP中功能强大却很少使用的函数实例小结
Nov 10 PHP
PHP输出多个元素的排列或组合的方法
Mar 14 PHP
thinkphp框架page类与bootstrap分页(美化)
Jun 25 PHP
PHP 文件锁与进程锁的使用示例
Aug 07 PHP
PHP连接及操作PostgreSQL数据库的方法详解
Jan 30 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
smarty 缓存控制前的页面静态化原理
2013/03/15 PHP
cakephp打印sql语句的方法
2015/02/13 PHP
PHP的Socket网络编程入门指引
2015/08/11 PHP
Javascript - HTML的request类
2007/01/09 Javascript
为JavaScript类型增加方法的实现代码(增加功能)
2011/12/29 Javascript
jquery选择器排除某个DOM元素的方法(实例演示)
2014/04/25 Javascript
JavaScript获取图片的原始尺寸以宽度为例
2014/05/04 Javascript
jQuery实现的兼容性浮动层示例
2016/08/02 Javascript
jQuery中slidedown与slideup方法用法示例
2016/09/16 Javascript
关于在vue-cli中使用微信自动登录和分享的实例
2017/06/22 Javascript
vue2.0之多页面的开发的示例
2018/01/30 Javascript
vue项目中将element-ui table表格写成组件的实现代码
2019/06/12 Javascript
Layui实现主窗口和Iframe层参数传递
2019/11/14 Javascript
Element Tooltip 文字提示的使用示例
2020/07/26 Javascript
vue watch监控对象的简单方法示例
2021/01/07 Vue.js
Python enumerate遍历数组示例应用
2008/09/06 Python
跟老齐学Python之模块的加载
2014/10/24 Python
在Python中操作字典之update()方法的使用
2015/05/22 Python
Python中is与==判断的区别
2017/03/28 Python
Java分治归并排序算法实例详解
2017/12/12 Python
python监控键盘输入实例代码
2018/02/09 Python
对python3 urllib包与http包的使用详解
2018/05/10 Python
基于Python开发chrome插件的方法分析
2018/07/07 Python
Python实现读取txt文件中的数据并绘制出图形操作示例
2019/02/26 Python
Python2.7版os.path.isdir中文路径返回false的解决方法
2019/06/21 Python
俄罗斯首家面向中国消费者的一站式购物网站:Wruru
2020/05/08 全球购物
英语专业毕业生自荐信
2013/10/28 职场文书
大学生的四年学习自我评价
2013/12/13 职场文书
夫妻忠诚协议书范本
2014/11/17 职场文书
新郎答谢词
2015/01/04 职场文书
幼儿园大班个人总结
2015/02/28 职场文书
企业法律事务工作总结
2015/08/11 职场文书
中学总务处工作总结
2015/08/12 职场文书
2019银行员工个人工作自我鉴定
2019/06/27 职场文书
浅谈MySQL表空间回收的正确姿势
2021/10/05 MySQL
配置Kubernetes外网访问集群
2022/03/31 Servers