zoukankan      html  css  js  c++  java
  • PHP 抓取网页图片并且另存为

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

    1 <?php
    2
    3  //URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字
    4 //默认把图片放在以此脚本相同的目录里
    5  function GrabImage($url, $filename=""){
    6
    7 //$url 为空则返回 false;
    8   if($url == ""){return false;}
    9 $ext = strrchr($url, ".");//得到图片的扩展名
    10 if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){echo "格式不支持!";return false;}
    11 if($filename == ""){$filename = time()."$ext";}//以时间戳另起名
    12 //开始捕捉
    13 ob_start();
    14 readfile($url);
    15 $img = ob_get_contents();
    16 ob_end_clean();
    17 $size = strlen($img);
    18 $fp2 = fopen($filename , "a");
    19 fwrite($fp2, $img);
    20 fclose($fp2);
    21 return $filename;
    22
    23 }
    24 //测试
    25 GrabImage("http://www.66xing.com/UploadFile/200609082320515027.bmp", "as.gif");
    26
    27 ?>

    相关描述:

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


  • 相关阅读:
    模仿微信右上角菜单栏
    改变checkbox的默认样式
    webpack中的静态资源处理
    如何解决Unsupported Architecture. Your executable contains unsupported architecture '[x86_64, i386]
    页面生命周期
    关于在使用Exchange2003系统时无法向sina,yahoo,hotmail等邮箱发送邮件问题的解决方法
    重置TCP/IP协议堆栈的经历
    网通电信双线路上网,网通的走网通线路,电信的走电信线路,内网通过NAT上网,双线路故障自动切换
    在OUTLOOK或OWA中查看邮件的SCL级别(转)
    常用的RBL服务器列表及介绍
  • 原文地址:https://www.cnblogs.com/catprayer/p/1692926.html
Copyright © 2011-2022 走看看