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树的代码,可以嵌套任意层
Oct 09 PHP
php标签云的实现代码
Oct 10 PHP
深入解析fsockopen与pfsockopen的区别
Jul 05 PHP
php+mysqli批量查询多张表数据的方法
Jan 29 PHP
Windows下编译PHP5.4和xdebug全记录
Apr 03 PHP
关于PHP开发的9条建议
Jul 27 PHP
php轻量级的性能分析工具xhprof的安装使用
Aug 12 PHP
PHP弹出对话框技巧详细解读
Sep 26 PHP
php上传图片生成缩略图(GD库)
Jan 06 PHP
Laravel路由设定和子路由设定实例分析
Mar 30 PHP
PHP框架Laravel中使用UUID实现数据分表操作示例
May 30 PHP
yii2中关于加密解密的那些事儿
Jun 12 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
js下函数般调用正则的方法附代码
2008/06/22 PHP
使用PHP导出Word文档的原理和实例
2013/10/21 PHP
php数组键值用法实例分析
2015/02/27 PHP
教大家制作简单的php日历
2015/11/17 PHP
php+MySQL实现登录时验证登录名和密码是否正确
2016/05/10 PHP
在PHP 7下安装Swoole与Yar,Yaf的方法教程
2017/06/02 PHP
javascript 打开页面window.location和window.open的区别
2010/03/17 Javascript
jquery+ajax每秒向后台发送请求数据然后返回页面的代码
2011/01/17 Javascript
jquery获取选中的文本和值的方法
2014/07/08 Javascript
js交换排序 冒泡排序算法(Javascript版)
2014/10/04 Javascript
jQuery插件EnPlaceholder实现输入框提示文字
2015/06/05 Javascript
javascript验证香港身份证的格式或真实性
2017/02/07 Javascript
深入理解Angularjs中$http.post与$.post
2017/05/19 Javascript
解决vue.js在编写过程中出现空格不规范报错的问题
2017/09/20 Javascript
vue中倒计时组件的实例代码
2018/07/06 Javascript
JS实现判断图片是否加载完成的方法分析
2018/07/31 Javascript
JavaScript 中 JSON.parse 函数 和 JSON.stringify 函数
2018/12/05 Javascript
python分析apache访问日志脚本分享
2015/02/26 Python
常用python编程模板汇总
2016/02/12 Python
老生常谈进程线程协程那些事儿
2017/07/24 Python
PyTorch线性回归和逻辑回归实战示例
2018/05/22 Python
Python 获取主机ip与hostname的方法
2018/12/17 Python
使用Python3内置文档高效学习以及官方中文文档
2019/05/19 Python
ubuntu 16.04下python版本切换的方法
2019/06/14 Python
简单了解python代码优化小技巧
2019/07/08 Python
python3的数据类型及数据类型转换实例详解
2019/08/20 Python
Python matplotlib画图时图例说明(legend)放到图像外侧详解
2020/05/16 Python
python Cartopy的基础使用详解
2020/11/01 Python
党的群众路线教育实践活动心得体会
2014/03/03 职场文书
白莲教口号
2014/06/18 职场文书
出国签证在职证明
2014/09/20 职场文书
幼儿教师辞职信
2015/02/27 职场文书
元宵节晚会主持词
2015/07/01 职场文书
学生早退检讨书(范文)
2019/08/19 职场文书
golang gopm get -g -v 无法获取第三方库的解决方案
2021/05/05 Golang
div与span之间的区别与使用介绍
2021/12/06 HTML / CSS