zoukankan      html  css  js  c++  java
  • php保存远程图片

    php获取远程图片并把它保存到本地

    来源:   时间:2013-09-05 19:26:57   阅读数:45006

    分享到:1

    [导读] 在php中我们经常使用写一些简单的采集功能,这样可以自动把远程服务器的图片或资源直接采集保存到本地服务器中,下面我来给大家详细介绍远程图片并把它保存到本地几种方法。例1 代码如下复制代码 **

    在php中我们经常使用写一些简单的采集功能,这样可以自动把远程服务器的图片或资源直接采集保存到本地服务器中,下面我来给大家详细介绍远程图片并把它保存到本地几种方法。

    例1

     代码如下 复制代码

    /*
    *功能:php多种方式完美实现下载远程图片保存到本地
    *参数:文件url,保存文件名称,使用的下载方式
    *当保存文件名称为空时则使用远程文件原来的名称
    */
    function getImage($url,$filename='',$type=0){
        if($url==''){return false;}
        if($filename==''){
            $ext=strrchr($url,'.');
            if($ext!='.gif' && $ext!='.jpg'){return false;}
            $filename=time().$ext;
        }
        //文件保存路径 
        if($type){
      $ch=curl_init();
      $timeout=5;
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
      $img=curl_exec($ch);
      curl_close($ch);
        }else{
         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;
    }

    例2

     代码如下 复制代码

    <?php

    //
    // Function: 获取远程图片并把它保存到本地
    //
    //
    //   确定您有把文件写入本地服务器的权限
    //  
    //
    // 变量说明:
    // $url 是远程图片的完整URL地址,不能为空。
    // $filename 是可选变量: 如果为空,本地文件名将基于时间和日期
    // 自动生成.

    function GrabImage($url,$filename="") {
       if($url==""):return false;endif;

       if($filename=="") {
         $ext=strrchr($url,".");
         if($ext!=".gif" && $ext!=".jpg"):return false;endif;
         $filename=date("dMYHis").$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;
    }


    $img=GrabImage("http://www.php100.com","");
    if($img):echo '<pre><img src="'.$img.'"></pre>';else:echo "false";endif;  

    ?>


    dedecms中的:

     代码如下 复制代码
    if(!empty($saveremoteimg))
             {
                     $body = stripslashes($body);
                     $img_array = array();
                     preg_match_all("/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array);
                     $img_array = array_unique($img_array[2]);
                     set_time_limit(0);
                     $imgUrl = $img_dir."/".strftime("%Y%m%d",time());
                     $imgPath = $base_dir.$imgUrl;
                     $milliSecond = strftime("%H%M%S",time());
                     if(!is_dir($imgPath)) @mkdir($imgPath,0777);
                     foreach($img_array as $key =>$value)
                     {
                             $value = trim($value);
                             $get_file = @file_get_contents($value);
                             $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
                             $fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3);
                             if($get_file)
                             {
                                     $fp = @fopen($rndFileName,"w");
                                     @fwrite($fp,$get_file);
                                     @fclose($fp);
                             }
                             $body = ereg_replace($value,$fileurl,$body);
                     }
                     $body = addslashes($body);
             }
    ?>

    例4

     代码如下 复制代码

    <?php
    //
    // Function: 获取远程图片并把它保存到本地
    //
    //
    // 确定您有把文件写入本地服务器的权限
    //
    //
    // 变量说明:
    // $url 是远程图片的完整URL地址,不能为空。
    // $filename 是可选变量: 如果为空,本地文件名将基于时间和日期// 自动生成.
    function GrabImage($url,$filename='') {
    if($url==''):return false;endif;
    if($filename=='') {
    $ext=strrchr($url,'.');
    if($ext!='.gif' && $ext!='.jpg'):return false;endif;$filename=date('dMYHis').$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;
    }
    $img=GrabImage('http://www.ccc.cc/static/image/common/logo.png','');
    if($img){echo '<pre><img src='.$img.'></pre>';}else{echo 'false';}
    ?>


     

    除非特别声明,PHP100新闻均为原创或投稿报道,转载请注明作者及原文链接
    原文地址: http://www.php100.com/html/php/lei/2013/0905/5367.html

  • 相关阅读:
    hibernate 单元測试框架
    freemarker基本数据类型
    Effective C++:条款26:尽可能延后变量定义式的出现时间
    Hive Python Streaming的原理及写法
    算法(第四版)学习笔记之java实现希尔排序
    Passing address of non-local object to __autoreleasing parameter for write-back
    VB 中窗口发现冲突名称,将使用名称...怎么解决?
    struts2基础梳理(二)
    EM算法求高斯混合模型參数预计——Python实现
    Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layou
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4528540.html
Copyright © 2011-2022 走看看