PHP抓取远程图片并另存为的实现方法_PHP教程

php中文网
发布: 2016-07-13 10:22:25
原创
1268人浏览过

php抓取远程图片并另存为的实现方法

   下面是源代码,及其相关解释

  

  //URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字

  //默认把图片放在以此脚本相同的目录里

  function GrabImage($url, $filename=""){

  //$url 为空则返回 false;

立即学习PHP免费学习笔记(深入)”;

  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);

存了个图
存了个图

视频图片解析/字幕/剪辑,视频高清保存/图片源图提取

存了个图 17
查看详情 存了个图

  return $filename;

  }

  //测试

  GrabImage("http://www.66xing.com/UploadFile/200609082320515027.bmp", "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).

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/848320.htmlTechArticlePHP抓取远程图片并另存为的实现方法 下面是源代码,及其相关解释 //URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字 /...
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号