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 相关文章推荐
用Zend Encode编写开发PHP程序
Oct 09 PHP
PHP中通过HTTP_USER_AGENT判断是否为手机移动终端的函数代码
Feb 14 PHP
IIS6.0 开启Gzip方法及PHP Gzip函数分享
Jun 08 PHP
php数字每三位加逗号的功能函数
Oct 22 PHP
PHP保存session到memcache服务器的方法
Jan 19 PHP
浅谈mysql_query()函数的返回值问题
Sep 05 PHP
php curl 模拟登录并获取数据实例详解
Dec 22 PHP
PHP实现webshell扫描文件木马的方法
Jul 31 PHP
PDO::inTransaction讲解
Jan 28 PHP
PHP中__set()实例用法和基础讲解
Jul 23 PHP
PHP pthreads v3在centos7平台下的安装与配置操作方法
Feb 21 PHP
PHP常量DIRECTORY_SEPARATOR原理及用法解析
Nov 10 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
《魔兽争霸3:重制版》更新 多项视觉效果调整
2020/05/04 魔兽争霸
Bo-Blog专用的给Windows服务器的IIS Rewrite程序
2007/08/26 PHP
PHP curl批处理及多请求并发实现方法分析
2018/08/15 PHP
jQuery 研究心得 取得属性的值
2007/11/30 Javascript
jquery实现树形二级菜单实例代码
2013/11/20 Javascript
jQuery模拟原生态App上拉刷新下拉加载更多页面及原理
2015/08/10 Javascript
微信小程序上滑加载下拉刷新(onscrollLower)分批加载数据(二)
2017/05/11 Javascript
node基于puppeteer模拟登录抓取页面的实现
2018/05/09 Javascript
react.js组件实现拖拽复制和可排序的示例代码
2018/08/20 Javascript
JavaScript学习笔记之DOM基础操作实例小结
2019/01/09 Javascript
uni-app之APP和小程序微信授权方法
2019/05/09 Javascript
vue-mugen-scroll组件实现pc端滚动刷新
2019/08/16 Javascript
微信小程序webSocket的使用方法
2020/02/20 Javascript
Python实现将n个点均匀地分布在球面上的方法
2015/03/12 Python
Python实现的视频播放器功能完整示例
2018/02/01 Python
Python使用pip安装pySerial串口通讯模块
2018/04/20 Python
Python3 获取一大段文本之间两个关键字之间的内容方法
2018/10/11 Python
Flask之请求钩子的实现
2018/12/23 Python
使用python实现希尔、计数、基数基础排序的代码
2019/12/25 Python
使用Python制作新型冠状病毒实时疫情图
2020/01/28 Python
python中如何进行连乘计算
2020/05/28 Python
Python 可视化神器Plotly详解
2020/12/26 Python
css背景图片的背景裁切、背景透明度、背景变换等效果运用
2012/12/24 HTML / CSS
Bose法国官网:购买耳机、扬声器、家庭影院、专业音响
2017/12/21 全球购物
新西兰最大的天然保健及护肤品网站:HealthPost(直邮中国)
2021/02/13 全球购物
Java和Javasciprt的区别
2012/09/02 面试题
四年的大学生生活自我评价
2013/12/09 职场文书
家长通知书家长评语
2014/04/17 职场文书
党校学习个人总结
2015/02/15 职场文书
2015年公司后勤管理工作总结
2015/05/13 职场文书
2016小学教师读书心得体会
2016/01/13 职场文书
高二英语教学反思
2016/03/03 职场文书
vue引入Excel表格插件的方法
2021/04/28 Vue.js
Python的三个重要函数详解
2022/01/18 Python
分享几种python 变量合并方法
2022/03/20 Python
关于mysql中string和number的转换问题
2022/06/14 MySQL