Home  >  Article  >  Backend Development  >  PHP grabs the images in the content and downloads the saved code

PHP grabs the images in the content and downloads the saved code

WBOY
WBOYOriginal
2016-07-25 09:00:451039browse
PHP grabs the images in the web page content and downloads and saves the implementation code to the specified directory. Friends in need can refer to it.

The complete code is as follows.

<?php
 //$url 远程完整图片地址,不能为空
 //程序员之家 http://bbs.it-home.org
 //$filename 另存为的图片名称,保存在与脚本相同的目录中
 function getImage($url, $filename=""){
  if($url == ""){return false;}
  $ext = strrchr($url, ".");//获取图片的扩展名
  if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){
   echo "格式不支持!";return false;
  }
  if($filename == ""){
   $filename = time()."$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;
 }
 //测试
 getImage("http://bbs.it-home.org/images/123.jpg", "zq123.jpg");
?>

For articles about information in php crawled content, you can also refer to: php file_get_contents code to capture page information php file_get_contents function code to capture page information php code for grabbing email addresses from web pages



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