Home  >  Article  >  Backend Development  >  PHP gets remote pictures to local

PHP gets remote pictures to local

WBOY
WBOYOriginal
2016-07-25 08:42:51817browse
  1. /*
  2. * Get remote image to local
  3. */
  4. function GrabImage($url){
  5. if($url != ""){ //If the image address is empty
  6. $ext = strrchr($url,'.'); //Determine the format of the image
  7. if($ext != '.jpg' && $ext != '.gif' && $ext != '$png'){
  8. return false;exit;
  9. }
  10. $filename_r = time().rand(10,9000).$ext; //Name the picture
  11. $filename = 'getimg/'.$filename_r;
  12. ob_start(); //Open the buffer Area
  13. readfile($url);
  14. $imginfo = ob_get_contents(); //Get the contents of the buffer
  15. ob_end_clean(); //Clear and close the buffer
  16. $fp = fopen($filename,'a');
  17. fwrite($fp,$imginfo);
  18. fclose($fp);
  19. }else{
  20. return false;
  21. }
  22. }
  23. $start_time = microtime(true);
  24. GrabImage("http://img4.shougongke.com /Public/advance/53846840dafb4.jpg");
  25. $end_time = microtime(true);
  26. $time = round($end_time-$start_time,3);
  27. echo 'Total program time'.$time.'seconds';
  28. ?>
Copy code

PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn