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+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
Dec 16 PHP
关于mysql 字段的那个点为是定界符
Jan 15 PHP
Windows Apache2.2.11及Php5.2.9-1的安装与配置方法
Jun 08 PHP
《PHP编程最快明白》第六讲:Mysql数据库操作
Nov 01 PHP
PHP计数器的实现代码
Jun 08 PHP
YII实现分页的方法
Jul 09 PHP
php获取文章上一页与下一页的方法
Dec 01 PHP
Linux下编译redis和phpredis的方法
Apr 07 PHP
PHP控制前台弹出对话框的实现方法
Aug 21 PHP
yii2局部关闭(开启)csrf的验证的实例代码
Jul 10 PHP
Yii框架实现对数据库的CURD操作示例
Sep 03 PHP
php实现根据身份证获取精准年龄
Feb 26 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
基于文本的搜索
2006/10/09 PHP
codeigniter框架The URI you submitted has disallowed characters错误解决方法
2014/05/06 PHP
ThinkPHP的MVC开发机制实例解析
2014/08/23 PHP
PHP给文字内容中的关键字进行套红处理
2016/04/12 PHP
Mootools 1.2教程 事件处理
2009/09/15 Javascript
js constructor的实际作用分析
2011/11/15 Javascript
JavaScript创建一个欢迎cookie弹出窗实现代码
2013/03/15 Javascript
禁止选中文字兼容IE、Chrome、FF等
2013/09/04 Javascript
Jquery的hover方法让鼠标经过li时背景变色
2013/09/06 Javascript
JavaScript学习小结(一)——JavaScript入门基础
2015/09/02 Javascript
JavaScript中实现无缝滚动、分享到侧边栏实例代码
2016/04/06 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
JS数组返回去重后数据的方法解析
2017/01/03 Javascript
微信小程序之picker日期和时间选择器
2017/02/09 Javascript
Vue.js中兄弟组件之间互相传值实例
2017/06/01 Javascript
js数字滑动时钟的简单实现(示例讲解)
2017/08/14 Javascript
javascript实现拖拽碰撞检测
2020/03/12 Javascript
[05:31]DOTA2英雄梦之声_第08期_莉娜
2014/06/23 DOTA
[00:18]天涯墨客三技能展示
2018/08/25 DOTA
python发送邮件示例(支持中文邮件标题)
2014/02/16 Python
使用Python制作获取网站目录的图形化程序
2015/05/04 Python
Python itertools模块详解
2015/05/09 Python
使用Python简单的实现树莓派的WEB控制
2016/02/18 Python
图文详解WinPE下安装Python
2016/05/17 Python
解决python "No module named pip" 的问题
2018/10/13 Python
Python3.6实现带有简单界面的有道翻译小程序
2019/04/16 Python
Python基于Socket实现简单聊天室
2020/02/17 Python
python实现俄罗斯方块游戏(改进版)
2020/03/13 Python
最新Python idle下载、安装与使用教程图文详解
2020/11/28 Python
CSS3属性box-shadow使用详细教程
2012/01/21 HTML / CSS
巴西网上药房:onofre
2016/11/21 全球购物
巴西最大的玩具连锁店:Ri Happy
2020/06/17 全球购物
简易离婚协议书(范本)
2014/10/25 职场文书
文明旅游倡议书
2015/04/28 职场文书
个人道歉信大全
2019/04/11 职场文书
SQLServer常见数学函数梳理总结
2022/08/05 MySQL