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
第十四节--命名空间
Nov 16 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(八)
Jun 23 PHP
PHP获取客户端真实IP地址的5种情况分析和实现代码
Jul 08 PHP
十个PHP高级应用技巧果断收藏
Sep 25 PHP
php过滤所有的空白字符(空格、全角空格、换行等)
Oct 27 PHP
Codeigniter中集成smarty和adodb的方法
Mar 04 PHP
PHP实现的登录,注册及密码修改功能分析
Nov 25 PHP
php mysql操作mysql_connect连接数据库实例详解
Dec 26 PHP
PHP连接MYSQL数据库的3种常用方法
Feb 27 PHP
PDO::beginTransaction讲解
Jan 27 PHP
PHP 8新特性简介
Aug 18 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
解决dede生成静态页和动态页转换的一些问题,及火车采集入库生成动态的办法
2007/03/29 PHP
PHP 面向对象 final类与final方法
2010/05/05 PHP
PHP连接操作access数据库实例
2015/03/30 PHP
php防止CC攻击代码 php防止网页频繁刷新
2015/12/21 PHP
laravel如何开启跨域功能示例详解
2017/08/31 PHP
两种方法实现文本框输入内容提示消失
2013/03/17 Javascript
js 图片随机不定向浮动的实现代码
2013/07/02 Javascript
jQuery中delegate和on的用法与区别详细解析
2014/01/26 Javascript
jquery实现显示已选用户
2014/07/21 Javascript
值得分享的轻量级Bootstrap Table表格插件
2016/05/30 Javascript
Vue侧滑菜单组件——DrawerLayout
2017/12/18 Javascript
ajaxfileupload.js实现上传文件功能
2019/04/19 Javascript
js实现随机8位验证码
2020/07/24 Javascript
vue tab切换,解决echartst图表宽度只有100px的问题
2020/07/19 Javascript
解决echarts vue数据更新,视图不更新问题(echarts嵌在vue弹框中)
2020/07/20 Javascript
vue中封装axios并实现api接口的统一管理
2020/12/25 Vue.js
[01:30]DOTA2上海特锦赛现场采访 Loda倾情献唱
2016/03/25 DOTA
[01:30:15]DOTA2-DPC中国联赛 正赛 Ehome vs Aster BO3 第二场 2月2日
2021/03/11 DOTA
Python跳出循环语句continue与break的区别
2014/08/25 Python
Python实现的批量下载RFC文档
2015/03/10 Python
python 出现SyntaxError: non-keyword arg after keyword arg错误解决办法
2017/02/14 Python
Python微信公众号开发平台
2018/01/25 Python
Python with语句上下文管理器两种实现方法分析
2018/02/09 Python
Python简单获取二维数组行列数的方法示例
2018/12/21 Python
python3.x提取中文的正则表达式示例代码
2019/07/23 Python
幼儿园安全责任书
2014/04/14 职场文书
论文指导教师评语
2014/04/28 职场文书
家长学校培训材料
2014/08/20 职场文书
初中国旗下的演讲稿
2014/08/28 职场文书
企业授权委托书范本
2014/09/22 职场文书
学校机关党总支领导班子整改工作方案
2014/10/26 职场文书
佛光寺导游词
2015/02/10 职场文书
2015年学习部工作总结范文
2015/03/31 职场文书
2016优秀毕业生个人事迹材料
2016/02/29 职场文书
Python3 使用pip安装git并获取Yahoo金融数据的操作
2021/04/08 Python
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python