SoFunction
Updated on 2025-03-03

Implementation method of sharing PHP source code to batch capture remote web page pictures and save them to the local area

As a imitation site worker, when a website has copyright or even encrypts it, WEBZIP will also turn off. How to deduct pictures and background pictures on the web page? Sometimes, you may think of using Firefox. This browser seems to be a powerful bug. The article has copyright. If you block the right click, Firefox will not be affected at all.

But as a developer who loves php, he likes to do it himself more. So, I wrote the following source code, PHP remotely crawling picture applet. You can read the css file and grab the background image in the css code. The following code is also written for crawling the pictures in the css.

<?php
 header("Content-Type: text/html; charset=utf-8");
    error_reporting(E_ERROR|E_WARNING);
 //Global configuration $fromFile = "";  //The file to be crawled $savePath = "ttttttttt";  //Save path $fromDomain = "/"; //The domain name to be crawled //Read the css style and separate out all image urls $str = file_get_contents($fromFile);
 $strArr = explode("url(",$str); 
 $i = 0;
 foreach($strArr as $val){
 $val1 = explode(")",$val);
 if(strpos($val1[0],'jpg')||strpos($val1[0],'png')||strpos($val1[0],'gif'))
 $imgUrl[$i++] = $val1[0];
 }
    //PS: You can use regular ones above, but I think this is also good //Start crawling foreach($imgUrl as $url){
 if($url=="") continue;
 $filename = $savePath.$url;
 $url = $fromDomain.$url;
 getImage($url,$filename);
 }
 function getImage($url,$filename){
 ob_start();
 $context = stream_context_create(
       array (
     'http' => array (
      'follow_location' => false // don't follow redirects
      )
     )
 );
 //Please make sure that the fopen wrappers in it is activated readfile( $url,false,$context);
 $img = ob_get_contents();
        ob_end_clean();
 $fp2 = @fopen($filename,"a");
 fwrite($fp2,$img);
 fclose($fp2);
 echo $filename." ok √<br/>";
 }
?>

Then if there is no accident, you will find that the folder you specified is full of pictures, haha...

PS: php get remote pictures and download them to save them locally

Share a function code that uses php to obtain remote pictures and download remote pictures to local:

/*
 *Function: PHP perfectly realizes downloading remote pictures and saving them to local
 * Parameters: file url, save file directory, save file name, download method used
 *When the save file name is empty, use the original name of the remote file
 */ 
function getImage($url,$save_dir='',$filename='',$type=0){ 
  if(trim($url)==''){ 
    return array('file_name'=>'','save_path'=>'','error'=>1); 
  } 
  if(trim($save_dir)==''){ 
    $save_dir='./'; 
  } 
  if(trim($filename)==''){//Save the file name    $ext=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 save directory  if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){ 
    return array('file_name'=>'','save_path'=>'','error'=>5); 
  } 
  //The method used to obtain remote files  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); 
  //File size  $fp2=@fopen($save_dir.$filename,'a'); 
  fwrite($fp2,$img); 
  fclose($fp2); 
  unset($img,$url); 
  return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0); 
} 

The above content is the implementation method of PHP source code to batch capture remote web page pictures and save them locally. I hope you like it.