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 相关文章推荐
一个更简单的无限级分类菜单代码
Jan 16 PHP
人尽可用的Windows技巧小贴士之下篇
Mar 22 PHP
zend framework文件上传功能实例代码
Dec 25 PHP
让CodeIgniter数据库缓存自动过期的处理的方法
Jun 12 PHP
php中magic_quotes_gpc对unserialize的影响分析
Dec 16 PHP
Web程序工作原理详解
Dec 25 PHP
php随机生成数字字母组合的方法
Mar 18 PHP
经典PHP加密解密函数Authcode()修复版代码
Apr 05 PHP
PHP实现简单实用的验证码类
Jul 29 PHP
使用PHP+AJAX让WordPress动态加载文章的教程
Dec 11 PHP
Yii2数据库操作常用方法小结
May 04 PHP
PHP进阶学习之依赖注入与Ioc容器详解
Jun 19 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
法兰绒滤网冲泡
2021/03/03 冲泡冲煮
作为程序员必知的16个最佳PHP库
2015/12/09 PHP
Yii2验证器(Validator)用法分析
2016/07/23 PHP
Mac系统下安装PHP Xdebug
2018/03/30 PHP
jquer之ajaxQueue简单实现代码
2011/09/15 Javascript
javascript不可用的问题探究
2013/10/01 Javascript
js导出table数据到excel即导出为EXCEL文档的方法
2013/10/10 Javascript
jQuery控制TR显示隐藏的三种常用方法
2014/08/21 Javascript
JS实现在状态栏显示打字效果完整实例
2015/11/02 Javascript
AngularJS 单元测试(一)详解
2016/09/21 Javascript
jQuery Tree Multiselect使用详解
2017/05/02 jQuery
JS跳转手机站url的若干注意事项
2017/10/18 Javascript
如何使用less实现随机下雪动画详解
2019/01/02 Javascript
后台使用freeMarker和前端使用vue的方法及遇到的问题
2019/06/13 Javascript
JavaScript获取某一天所在的星期
2019/09/05 Javascript
vue 导航锚点_点击平滑滚动,导航栏对应变化详解
2020/08/10 Javascript
vue 插槽简介及使用示例
2020/11/19 Vue.js
django自定义Field实现一个字段存储以逗号分隔的字符串
2014/04/27 Python
python以环状形式组合排列图片并输出的方法
2015/03/17 Python
python3.4.3下逐行读入txt文本并去重的方法
2018/04/29 Python
python实现超市扫码仪计费
2018/05/30 Python
用python实现k近邻算法的示例代码
2018/09/06 Python
Python常用模块os.path之文件及路径操作方法
2019/12/03 Python
使用Python内置模块与函数进行不同进制的数的转换
2020/04/26 Python
python GUI模拟实现计算器
2020/06/22 Python
新加坡第一大健康与美容零售商:屈臣氏新加坡(Watsons Singapore)
2020/12/11 全球购物
德国二手设计师时装和复古时装跳蚤市场:Mädchenflohmarkt
2020/11/09 全球购物
优秀员工自荐书
2013/12/19 职场文书
小学教师管理制度
2014/01/18 职场文书
《影子》教学反思
2014/02/21 职场文书
党员干部承诺书范文
2014/03/25 职场文书
服务承诺书怎么写
2014/05/24 职场文书
整改通知书格式
2015/04/22 职场文书
导游词之江苏同里古镇
2019/11/18 职场文书
读《工匠精神》有感:热爱工作,精益求精
2019/12/28 职场文书
剧场版《转生恶役只好拔除破灭旗标》公开最新视觉图 2023年上映
2022/04/02 日漫