PHP using the GD library implementation download all the pictures in the Web page

Source: Internet
Author: User
Tags imagejpeg php code php tutorial relative strlen

This article mainly introduces the use of the GD library in PHP to download all the pictures in the Web page, this article directly gives the implementation code, the need for friends can refer to the

In the previous PHP tutorial on the PHP GD Library can achieve remote picture download, but that just downloaded a picture, the principle is the same, to download a Web page of all the pictures as long as the use of regular expressions to judge, find all the picture URL can be recycled to download, I specially refer to the network resources to write the GD library picture download Class!

The PHP code is as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 <?php header ("content-type:text/html; Charset=utf-8 "); if (!empty ($_post[' submit ')) {$url = $_post[' url ']//action to get a picture of the relative path $url _fields = Parse_url ($url); $main _url = $url _f ields[' host ']; $base _url = substr ($url, 0,strrpos ($url, '/') +1); Get Web content/Set proxy server $opts = Array (' http ' =>array (' Request_fulluri ' =>true)); $context = Stream_context_create ($opts); $content = File_get_contents ($url, False, $context); Match the IMG tag, save all matching strings to the array $matches $reg = "/<img.*?src=" (. *?). *?>/i "; Preg_match_all ($reg, $content, $matches); $count = count ($matches [0]); For ($i =0 $i < $count; $i + +) {/* Converts the URL of all pictures to lowercase * $matches [1][$i] = Strtolower ($matches [1][$i]);///If the picture is a relative path to the full path if (!strpos (' a '. $matches [1][$i], ' http ') {//Because '/' is No. 0 position if (strpos (' a '. $matches [1][$i], '/')] {$matches [1][$i] = ' http ://'. $main _url. $matches [1][$i]; }else{$matches [1][$i] = $base _url. $matches [1][$i];} //Filter Duplicate pictures $img _arr = Array_unique ($matches [1]); Materialized picture Download class $getImg = new Downimage (); $url _count = Count ($img _ARR); For ($i =0 $i < $url _count $i + +) {$getImg->source = $img _arr[$i]; $getImg->save_address = './pic/'; $file = $getIm G->download (); echo "Download complete! Haha, simple! "; } Class downimage{Public $source//remote picture URL public $save _address;//save local address public $set _extension;//Set Picture extension public $quality ; Picture quality (0~100,100 best, default 75 or so)//download method (choose GD Library picture download) public function download () {//Get remote picture information $info = @getimagesize ($this-> SOURCE); Gets the picture extension $mime = $info [' MIME ']; $type = substr (STRRCHR ($mime, '/'), 1); Different picture types choose different pictures to generate and save function switch ($type) {case ' jpeg ': $img _create_func = ' imagecreatefromjpeg '; $img _save_func = ' Imagejpeg '; $new _img_ext = ' jpg '; $image _quality = isset ($this->quality)? $this->quality:100; Break Case ' png ': $img _create_func = ' imagecreatefrompng '; $img _save_func = ' imagepng '; $new _img_ext = ' png '; Break Case ' BMP ': $img _create_func = ' imagecreatefrombmp '; $img _save_func = ' imagebmp '; $new _img_ext = ' bmp '; Break Case ' gif ': $img _create_func = ' imagecreatefromgif '; $img _save_fUNC = ' imagegif '; $new _img_ext = ' gif '; Break Case ' vnd.wap.wbmp ': $img _create_func = ' imagecreatefromwbmp '; $img _save_func = ' imagewbmp '; $new _img_ext = ' bmp '; Break Case ' XBM ': $img _create_func = ' IMAGECREATEFROMXBM '; $img _save_func = ' IMAGEXBM '; $new _img_ext = ' XBM '; Break Default: $img _create_func = ' imagecreatefromjpeg '; $img _save_func = ' imagejpeg '; $new _img_ext = ' jpg '; The local filename if (isset ($this->set_extension)) {$ext = STRRCHR ($this->source, ".") is synthesized based on whether or not to set the extension; $strlen = strlen ($ext); $newname = basename (substr ($this->source,0,-$strlen)). $new _img_ext; }else{$newname = basename ($this->source);}  //Generate local file path $save _address = $this->save_address. $newname; $img = @ $img _create_func ($this->source); if (Isset ($image _quality)) {$save _img = @ $img _save_func ($img, $save _address, $image _quality); else{$save _img = @ $img _save_func ($img, $save _address);} return $save _img; }?> <form method= "POST" action= "" > Remote URL Address: <input type= "text" name= "url" size=30/> <input type= "Submit" name= "submit" value= "Download all pictures of this page"/> </form>

Run the results as shown in figure:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.