PHP fetch a remote image to be saved locally

In recent work, the need to develop a user story features , which need to be online all kinds of picture material localized storage. So the Internet to find some relevant information, and according to their own development needs, compiled a main logic code.

/ * * 

* PHP will grab images on web pages to local storage 

* @param $ imgUrl picture url address 

* @param string $ saveDir local storage path stored by default in the current directory 

* @param null $ fileName pictures stored in the local file name 

* Mix @return 

* / 

function crabImage ( $ for imgUrl , $ savedir =, '/.' $ fileName = null ) { 

      IF ( empty ( $ for imgUrl )) { 

            return  to false ; 

      } 



      // get image information size 

      $ ImgSize = getimagesize ( $ for imgUrl ); 

      IF (! the in_array ( $ ImgSize [ 'MIME'],Array ( 'Image / JPG', 'Image / GIF', 'Image / PNG', 'Image / JPEG'), to true )) { 

            return  to false ; 

      } 

      // Get suffix 

      $ _mime = the explode ( '/', $ ImgSize [ 'MIME' ]); 

      $ _ext = '.'. End ( $ _mime ); 



      IF ( empty ( $ fileName )) {   // generate unique file name 

            $ fileName = of uniqid ( Time (), to true ). $ _ext ; 

      } 

      // start grab 

      ob_start ();

      ReadFile ( $ for imgUrl ); 

      $ imgInfo = ob_get_contents (); 

      ob_end_clean (); 

      IF (! the file_exists ( $ savedir {)) 

            mkdir ( $ savedir , 0777, to true ); 

      } 

      $ FP = the fopen ( $ savedir . $ fileName , ' A ' ); 

      $ imgLen = strlen ( $ imgInfo );     // calculate the size of the source image 

      $ _inx = 1024;    // every write 1K 

      $ _time, = ceil($imgLen/$_inx);

      for($i=0; $i<$_time; $i++){

            fwrite($fp,substr($imgInfo, $i*$_inx, $_inx));

      }

      fclose($fp);

      //返回结果

      return substr($saveDir.$fileName,1);

}

 -------------------------------------------- The information is stored in a txt in------------------------------------------------- ------------------

fputs(fopen('phone'.date('Ymdhis').'.txt','a+'),$v['phone'].PHP_EOL);

 

 ------------------------------------ ----------- connect to other databases -------------------------------------------------- --------------------------------

            Db $ = Db :: Connect ([
                 // database type 
                'of the type' => 'MySQL', // server address 
                'hostname' => 'xx.xx.xx.213', // database name 
                'database' => 'xiaocxxu_xxt', // database username 
                'username' => 'xiaxxxxnxx_txxt', // database password 
                'password' => 'JRLDxxxxxxdbFc', 
                'HostPort' => '', // connected DSN 
                'DSN' => '' ,// database connection parameters 
                [], 'the params' => @ database defaults encoding utf8
                
                
                
                
                
                
                
                'charset' => 'UTF8', // database table prefix 
                'prefix' => 'xcf_', // database debug mode 
            ]); $ START_TIME = Time (); $ RES = $ Db -> Table ( 'xcf_users' ) -> select ();
                
                

            
      
            

 

Guess you like

Origin www.cnblogs.com/yehuisir/p/11614251.html