-
- function make_dir($path){
- if(!file_exists($path)){//Create if it does not exist
- $mk=@mkdir($path,0777); //Permissions
- @chmod($path,0777);
- }
- return true;
- }
Copy the code The function read_filetext() gets the image content.
Use fopen to open the image file, and then fread to read the image file content.
- function read_filetext($filepath){
- $filepath=trim($filepath);
- $htmlfp=@fopen($filepath,"r");
- //remote
- if( strstr($filepath,"://")){
- while($data=@fread($htmlfp,500000)){
- $string.=$data;
- }
- }
- //local
- else{
- $ string=@fread($htmlfp,@filesize($filepath));
- }
- @fclose($htmlfp);
- return $string;
- }
Copy codeFunction write_filetext() writes the file and writes the image The content fputs is written into the file, that is, the image file is saved.
- function write_filetext($filepath,$string){
- //$string=stripSlashes($string);
- $fp=@fopen($filepath,"w");
- @ fputs($fp,$string);
- @fclose($fp);
- }
-
Copy codeThe function get_filename() gets the image name, and you can also customize the file name to be saved.
- function get_filename($filepath){
- $fr=explode("/",$filepath);
- $count=count($fr)-1;
- return $fr[$ count];
- }
Copy the codeThen, combine several functions, call them in the function save_pic(), and finally return the saved picture path.
- function save_pic($url,$savepath=''){
- //Processing address
- $url=trim($url);
- $url=str_replace(" ","% 20",$url);
- //Read file
- $string=read_filetext($url);
- if(empty($string)){
- echo 'Cannot read file';exit;
- }
- //File name
- $filename = get_filename($url);
- //Storage directory
- make_dir($savepath); //Create a storage directory
- //File address
- $filepath = $savepath.$filename;
- //Write a file
- write_filetext( $filepath,$string);
- return $filepath;
- }
Copy the code The last step is to call the save_pic() function to save the image, and use the following code for testing.
- //Target image address
- $pic = "http://img0.pconline.com.cn/pconline/1205/06/2776119_end1_thumb.jpg";
- //Save directory
- $savepath = "images/";
- echo save_pic($pic,$savepath);
-
Copy codeIn actual application, the content of a certain site may be collected, such as product information, including collection of anti-hotlinking information The image is saved to the server on the website.
At this time, you can use regular matching to match the page content, find all the matching pictures in the page, and then download them to the website server respectively to complete the collection of pictures.
Test example:
- function get_pic($cont,$path){
- $pattern_src = '/<[img|IMG].*?src=['|"](.*?(? :[.gif|.jpg]))['|"].*?[/]?>/';
- $num = preg_match_all($pattern_src, $cont, $match_src);
- $pic_arr = $match_src[ 1]; //Get the picture array
- foreach ($pic_arr as $pic_item) { //Loop to get the address of each picture
- save_pic($pic_item,$path); //Download and save the picture
- echo "[OK]. .!";
- }
- }
-
Copy the code
Then, by analyzing the page content, find out the main content, and call get_pic() to save the picture.
-
- //Collect pictures of the content page of a report about mobile phones on Pacific Computer Network
- $url = "http://gz.pconline.com.cn/321/3215791.html";
-
- $content = file_get_contents($url);//Get web content
- $preg = '#
(.*) div>#iUs';
- preg_match_all($preg, $content, $arr);
- $cont = $arr[1][0];
- get_pic($cont,'img/');
-
Copy code
The above code was personally tested by the author, and it can collect images, but there are still some scenarios that have not been taken into account. For example, the target website has made more than 302 jumps, and the target website has made various anti-collection methods. Please study it yourself.
|