When we need to collect a webpage content on the network, if the image on the target website does the anti-theft chain, the images we collect directly are not available on our website. Then we use the program to download the image on the target website to our website server, then we can call the picture.
View Demo Demos
This article will use PHP to realize the remote image collection function. Basic Flow:
1. Get the image address of the target website.
2. Read the contents of the picture.
3. Create a path to save the picture and name the picture.
4. Write the contents of the picture.
5, complete.
We implement this process by writing several functions.
The function Make_dir () establishes the directory. Determine if the image file directory to be saved exists, create a directory if it does not exist, and set the directory as writable.
1function Make_dir ($path) {
2 if (!file_exists ($path)) {//does not exist then establishes
3 [email protected] ($path, 0777); Permissions
4 @chmod ($path, 0777);
3;
6 return true;
7}
The function Read_filetext () obtains the picture content. Use fopen to open the picture file, and then fread to read the picture file contents.
01function Read_filetext ($filepath) {
=trim $filepath ($filepath);
[Email protected] ($filepath, "R");
04//Remote
if (Strstr ($filepath, "://")) {
while ([email protected] ($HTMLFP, 500000)) {
$string. = $data;
08}
09}
10//Local
else{
[Email protected] ($HTMLFP, @filesize ($filepath));
13}
@fclose ($HTMLFP);
return $string;
16}
The function Write_filetext () writes the file, fputs the picture content to the file, i.e. saves the picture file.
function Write_filetext ($filepath, $string) {
$string =stripslashes ($string);
[Email protected] ($filepath, "w");
@fputs ($fp, $string);
@fclose ($FP);
}
The function get_filename () Gets the name of the picture, or you can customize the name of the file you want to save.
1function Get_filename ($filepath) {
2 $FR =explode ("/", $filepath);
3 $count =count ($FR)-1;
4 return $FR [$count];
5}
Several functions are then combined, called in the function Save_pic (), and finally returned to the saved picture path.
01function save_pic ($url, $savepath = ") {
02//Processing Address
$url =trim ($url);
$url =str_replace ("", "%20", $url);
05//Read File
$string =read_filetext ($url);
if (empty ($string)) {
Echo ' can't read files '; exit;
09}
10//File name
One $filename = Get_filename ($url);
12//Storage directory
Make_dir ($savepath); Create a storage directory
14//File address
$filepath = $savepath. $filename;
16//write file
Write_filetext ($filepath, $string);
return $filepath;
19}
The final step is to call the Save_pic () function to save the picture, and we use the following code to do the test.
1//Destination Image Address
2$pic = "Http://img0.pconline.com.cn/pconline/1205/06/2776119_end1_thumb.jpg";
3//Saving a directory
4$savepath = "images/";
5echo save_pic ($pic, $savepath);
In practical applications, we may collect the content of a site, such as product information, including the collection of images of the anti-theft chain to the site on the server. At this point we can use the regular matching page content, the page matches the image to find out, and then download to the website server, complete the image collection. The following code is for testing only:
1function get_pic ($cont, $path) {
2 $pattern _src = '/<[img| Img].*?src=[\ ' |\ "] (. *? (?: [\.gif|\.jpg])) [\ ' |\ '].*? [\/]?>/';
3 $num = Preg_match_all ($pattern _src, $cont, $match _src);
4 $pic _arr = $match _src[1]; Get an array of images
5 foreach ($pic _arr as $pic _item) {//loops out the address of each picture
6 Save_pic ($pic _item, $path); Download and save pictures
7 echo "[ok]..!";
8}
9}
Then we analyze the content of the page, the main content to find out, call Get_pic () to achieve the preservation of the picture.
1//We collect Pacific Internet a picture of the content page of the mobile report
2$url = "http://www.onesheng.cn";
3
4$content = file_get_contents ($url);//Get Web content
5$preg = ' #<div class= ' main Clearfix ' > (. *) <div class= "Art_nav_box" > #iUs ';
6preg_match_all ($preg, $content, $arr);
7$cont = $arr [1][0];
8get_pic ($cont, ' img/');
The above code, the author of the pro-test, you can collect pictures, but there are some scenes did not take into account, such as the target site to do more than 302 jumps, the target site to do a variety of anti-collection, left to like to toss the students to try it.
The implementation of PHP language programming to capture remote image resources