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中的登陆login
Jan 18 PHP
PHP $_SERVER详解
Jan 16 PHP
PHP 手机归属地查询 api
Feb 08 PHP
IP攻击升级,程序改进以对付新的攻击
Nov 23 PHP
PHP array_multisort()函数的使用札记
Jul 03 PHP
解析phpstorm + xdebug 远程断点调试
Jun 20 PHP
PHP合并discuz用户脚本的方法
Aug 04 PHP
详解PHP的Yii框架中自带的前端资源包的使用
Mar 31 PHP
java模拟PHP的pack和unpack类
Apr 13 PHP
PHP基本语法实例总结
Sep 09 PHP
php实现水印文字和缩略图的方法示例
Dec 29 PHP
php实现有序数组旋转后寻找最小值方法
Sep 27 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
利用 window_onload 实现select默认选择
2006/10/09 PHP
MySQL数据源表结构图示
2008/06/05 PHP
php之readdir函数用法实例
2014/11/13 PHP
php从文件夹随机读取文件的方法
2015/06/01 PHP
php简单随机字符串生成方法示例
2017/04/19 PHP
购物车实现的几种方式优缺点对比
2018/05/02 PHP
PHP htmlspecialchars()函数用法与实例讲解
2019/03/08 PHP
利用js实现遮罩以及弹出可移动登录窗口
2013/07/08 Javascript
jQuery操作select下拉框的text值和value值的方法
2014/05/31 Javascript
js实现拖拽效果
2015/02/12 Javascript
yui3的AOP(面向切面编程)和OOP(面向对象编程)
2015/05/01 Javascript
js实现文本上下来回滚动
2017/02/03 Javascript
JS中正则表达式要注意lastIndex属性
2017/08/08 Javascript
sublime text配置node.js调试(图文教程)
2017/11/23 Javascript
JavaScript基于对象方法实现数组去重及排序操作示例
2018/07/10 Javascript
JS实现的简单tab切换功能完整示例
2019/06/20 Javascript
NodeJs 模仿SIP话机注册的方法
2019/06/21 NodeJs
JS开发 富文本编辑器TinyMCE详解
2019/07/19 Javascript
AngularJs的$http发送POST请求,php无法接收Post的数据问题及解决方案
2020/08/13 Javascript
JavaScript实现切换多张图片
2021/01/27 Javascript
深入理解Python变量与常量
2016/06/02 Python
python出现&quot;IndentationError: unexpected indent&quot;错误解决办法
2017/10/15 Python
python实现textrank关键词提取
2018/06/22 Python
浅谈django三种缓存模式的使用及注意点
2018/09/30 Python
在 Python 中接管键盘中断信号的实现方法
2020/02/04 Python
在python3中实现查找数组中最接近与某值的元素操作
2020/02/29 Python
Python爬虫爬取杭州24时温度并展示操作示例
2020/03/27 Python
解决reload(sys)后print失效的问题
2020/04/25 Python
PyCharm中如何直接使用Anaconda已安装的库
2020/05/28 Python
HTML table 表格边框的实现思路
2019/10/12 HTML / CSS
中学生评语大全
2014/04/18 职场文书
团日活动总结书格式
2014/05/08 职场文书
世界读书日的活动方案
2014/08/20 职场文书
群众路线教育党员自我剖析材料
2014/10/06 职场文书
财务部岗位职责范本
2015/04/14 职场文书
Win11开始菜单添加休眠选项
2022/04/19 数码科技