php save the contents of the editor of a local remote picture picture

Sometimes our editors copied picture is remote, you need to download to a local server.

 

1, extract a remote image

  / * 
* Function: php perfect Realization of remote image is saved to the local 
* Parameters: file url, save the file directory, save the file name to download using 
* When you save the file name is empty when the original name of the remote file, use 
* / 

    Private the getImage function ($ URL, filename = $ '', $ type = 0) { 
        IF (TRIM ($ URL) == '') { 
            return Array ( 'file_name' => '', 'the save_path' => '', 'error' =>. 1); 
        } 
        $ filePath = 'Upload / Content /' DATE. ( 'Ymd'); 
      // IF (TRIM ($ save_dir) == '') { 
            .. = $ save_dir root_path the DS 'WWW '.. the DS $ filePath; 
        //} 
        IF (TRIM ($ filename) ==' ') {// file name to save 
            $ EXT = The strrchr (URL $,'. '); 
            !. IF (EXT = $' GIF '&& $ ext! ='. jpg '){
                return array('file_name'=>'','save_path'=>'','error'=>3);
            }
            filename = Time $ () $ EXT;. 
        } 
        IF (! 0 == strrpos ($ save_dir, '/')) { 
            $ save_dir = '/';. 
        } 
// create a storage directory 
        if (file_exists ($ save_dir)! ! && mkdir ($ save_dir, 0777, to true)) { 
            return Array ( 'file_name' => '', 'the save_path' => '', 'error' =>. 5); 
        } 
method of obtaining remote file // employed 
        IF ($ type) { 
            $ CH = curl_init (); 
            $ timeout =. 5; 
            curl_setopt ($ CH, CURLOPT_URL to, $ URL); 
            curl_setopt ($ CH, CURLOPT_RETURNTRANSFER,. 1); 
            curl_setopt ($ CH, CURLOPT_CONNECTTIMEOUT, $ timeout); 
            $ img = curl_exec ($ ch) ;
            if($img==false)
            {
                return array('file_name'=>'','save_path'=>'','error'=>6);
            }
            curl_close($ch);
        }else{
            ob_start();
            readfile($url);
            $img=ob_get_contents();
            ob_end_clean();
        }
//$size=strlen($img);
//文件大小
        $fp2=@fopen($save_dir.$filename,'a');
        fwrite($fp2,$img);
        fclose($fp2);
        unset($img,$url);
        return array('file_name'=>$filename,'save_path'=>DS.$filePath,'error'=>0);
    }

  

Alternatively the editor of the content 2, the content acquisition editor, using the above method

 / * Save the remote to the local image * 
    @param $ Content content editor 
    @return $ content had returned content replacement 
    * / 
    protected   function getRempotePicToLocation ($ Content) 
    { 
      preg_match_all ( ' / <IMG [^ "] + = the src" ([^ "] +) \" / ' , $ Content, $ The matches);
       IF ($ The matches) 
      { 
          $ imgList = $ The matches [ . 1 ];
           the foreach ($ imgList AS $ or matches -) 
          { 
             IF (the strpos ($ or matches -, ' HTTP ' )> = 0 ) 
             {    / * file name and extension to get the current file * /
                 preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$matche,$fileInfo);
                 $info=$this->getImage($matche,$fileInfo[1],0);
                 if($info['error']==0)
                 {
                     $content=str_replace($matche,$info['save_path'].DS.$info['file_name'],$content);
                 }
             }
          }

      }
   return $content;
    }
View Code

 

Guess you like

Origin www.cnblogs.com/fogwang/p/12580318.html