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如何透过ODBC来存取数据库
Oct 09 PHP
聊天室php&amp;mysql(五)
Oct 09 PHP
PHP 面向对象实现代码
Nov 11 PHP
php 无法加载mcrypt.dll的解决办法
Apr 03 PHP
php通过ajax实现双击table修改内容
Apr 28 PHP
PHP 如何获取二维数组中某个key的集合
Jun 03 PHP
php实现的Cookies操作类实例
Sep 24 PHP
使用php的HTTP请求的库Requests实现美女图片墙
Feb 22 PHP
php校验表单检测字段是否为空的方法
Mar 20 PHP
PHP中include和require的区别实例分析
May 07 PHP
PHP判断json格式是否正确的实现代码
Sep 20 PHP
php获取目录下所有文件及目录(多种方法)(推荐)
May 14 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
php 上一篇,下一篇文章实现代码与原理说明
2010/05/09 PHP
php设计模式 Composite (组合模式)
2011/06/26 PHP
解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
2013/06/24 PHP
Yii中CGridView关联表搜索排序方法实例详解
2014/12/03 PHP
Laravel5.4框架使用socialite实现github登录的方法
2019/03/20 PHP
20款效果非常棒的 jQuery 插件小结分享
2011/11/18 Javascript
js实现图片旋转的三种方法
2014/04/10 Javascript
JS实现表格数据各种搜索功能的方法
2015/03/03 Javascript
jQuery模拟黑客帝国矩阵效果实例
2015/06/28 Javascript
兼容各大浏览器的JavaScript阻止事件冒泡代码
2015/07/09 Javascript
JavaScript制作淘宝星级评分效果的思路
2020/06/23 Javascript
js+html5实现canvas绘制网页时钟的方法
2016/05/21 Javascript
JavaScript中setTimeout的那些事儿
2016/11/14 Javascript
Vue.js使用v-show和v-if的注意事项
2016/12/13 Javascript
利用Javascript裁剪图片并存储的简单实现
2017/03/13 Javascript
AngularJS之ionic 框架下实现 Localstorage本地存储
2017/04/22 Javascript
vue 里面使用axios 和封装的示例代码
2017/09/01 Javascript
jQuery除指定区域外点击任何地方隐藏DIV功能
2017/11/13 jQuery
JavaScript事件对象event用法分析
2018/07/27 Javascript
微信小程序实现简单评论功能
2018/11/28 Javascript
20个必会的JavaScript面试题(小结)
2019/07/02 Javascript
vue中如何添加百度统计代码
2020/12/19 Vue.js
Python中的多行注释文档编写风格汇总
2016/06/16 Python
Windows下Anaconda的安装和简单使用方法
2018/01/04 Python
Python抽象和自定义类定义与用法示例
2018/08/23 Python
详解10个可以快速用Python进行数据分析的小技巧
2019/06/24 Python
Python操作Mongodb数据库的方法小结
2019/09/10 Python
python统计指定目录内文件的代码行数
2019/09/19 Python
Python+OpenCV实现旋转文本校正方式
2020/01/09 Python
关于多元线性回归分析——Python&amp;SPSS
2020/02/24 Python
如何基于python把文字图片写入word文档
2020/07/31 Python
服装行业创业计划书范文
2014/02/05 职场文书
《月亮湾》教学反思
2014/04/14 职场文书
2014年新生军训方案
2014/05/01 职场文书
助学贷款贫困证明
2014/09/23 职场文书
《悲惨世界》:比天空更广阔的是人的心灵
2020/01/16 职场文书