Use the gd Library in php to download all images on the webpage _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags imagejpeg
Use the gd Library in php to download all images on the webpage. Using the gd Library in php to download all images on the webpage this article mainly introduces how to use the gd Library in php to download all images on the webpage. This article provides the implementation code directly, you can use the gd Library in php to download all images on the webpage.

This article mainly introduces how to use the gd Library in php to download all the images on the webpage. This article provides the implementation code directly. For more information, see

In the previous php Tutorial, the php gd library can be used to download remote images. However, it only downloads an image in the same principle, to download all images on a Web page, you only need to use regular expressions to judge and find out all the image URLs to download them cyclically. I specially wrote the gd Library image download class by referring to network resources!

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

43

44

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

84

85

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

Header ("Content-type: text/html; charset = utf-8 ");

If (! Empty ($ _ POST ['submit ']) {

$ Url = $ _ POST ['URL'];

// Operations to obtain images with relative paths

$ Url_fields = parse_url ($ url );

$ Main_url = $ url_fields ['host'];

$ Base_url = substr ($ url, 0, strrpos ($ url, '/') + 1 );

// Obtain the webpage content

// Sets the 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 and save all matching strings to the array $ matches

$ Reg = "// I ";

Preg_match_all ($ reg, $ content, $ matches );

$ Count = count ($ matches [0]);

For ($ I = 0; $ I <$ count; $ I ++ ){

/* Convert the URLs of all images to lowercase letters

* $ Matches [1] [$ I] = strtolower ($ matches [1] [$ I]);

*/

// Convert the image to the full path if it is a relative path

If (! Strpos ('A'. $ matches [1] [$ I], 'http ')){

// Because '/' is 0th locations

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 images

$ Img_arr = array_unique ($ matches [1]);

// Instantiate the image 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 = $ getImg-> download ();

}

Echo "download complete! Haha, that's easy! ";

}

Class DownImage {

Public $ source; // remote image URL

Public $ save_address; // save the local address

Public $ set_extension; // sets the image extension.

Public $ quality; // image quality (0 ~ 100,100 is the best. the default value is about 75)

// Download method (using GD Library image download)

Public function download (){

// Obtain remote image information

$ Info = @ getimagesize ($ this-> source );

// Obtain the image extension.

$ Mime = $ info ['Mime '];

$ Type = substr (strrchr ($ mime, '/'), 1 );

// Select different image generation and storage functions for different Image types

Switch ($ type ){

Case 'jpeg ':

$ Img_create_func = 'imagecreatefromjpeg ';

$ Img_save_func = 'imagejpeg ';

$ New_img_ext = 'jpg ';

$ Image_quality = isset ($ this-> quality )? $ This-& gt; 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 ';

}

// Merge local file names based on whether to set the extension.

If (isset ($ this-> set_extension )){

$ Ext = strrchr ($ this-> source ,".");

$ Strlen = strlen ($ ext );

$ Newname = basename (substr ($ this-> source, 0,-$ strlen). '.'. $ new_img_ext;

} Else {

$ Newname = basename ($ this-> source );

}

// Generate the 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;

}

}

?>

Running result

This article describes how to use the gd Library in php to download all images on the webpage. This article provides the implementation code directly...

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.