Using GD Library in PHP for remote image download Example _php tutorial

Source: Internet
Author: User

Using GD Library in PHP for remote picture Download instance


This article mainly introduces the use of the GD Library in PHP to achieve remote Picture Download example, this article directly give the implementation code, the need for friends can refer to the following

Because today want to write a remote download pictures of the class, in advance to write a PHP GD library to achieve remote image download function, of course, curl to achieve better, PHP GD library remote image download function mainly using the GD library two function imagecreatefromxxx () Used to generate picture functions and imagexxx functions, XXX represents the extension of different pictures, so you have to find a way to get the extension of the remote image, with the following PHP code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

Header ("content-type:text/html; Charset=utf-8 ");

if (!empty ($_post[' submit ')) {

$url = $_post[' url '];

$pictureName = $_post[' picturename ');

$img = Getpicture ($url, $pictureName);

Echo '

';

}

function Getpicture ($url, $pictureName) {

if ($url = = "") return false;

Get the name extension of a picture

$info = getimagesize ($url);

$mime = $info [' MIME '];

$type = substr (STRRCHR ($mime, '/'), 1);

Different picture types choose different picture generation and save functions

Switch ($type) {

Case ' JPEG ':

$img _create_func = ' imagecreatefromjpeg ';

$img _save_func = ' imagejpeg ';

$new _img_ext = ' jpg ';

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 ';

}

if ($pictureName = = "") {

$pictureName = Time (). ". {$new _img_ext} ";

}else{

$pictureName = $pictureName. ". {$new _img_ext} ";

}

$src _im = $img _create_func ($url); Create a new picture from a URL

$img _save_func ($src _im, $pictureName); Output file to File

return $pictureName;

}

?>

Running results such as: (Picture automatically saved in the current file directory, do not understand can leave a message)

http://www.bkjia.com/PHPjc/1000114.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000114.html techarticle The use of the GD Library in PHP for remote image Download example this article mainly introduces the use of the GD Library in PHP to achieve remote Picture Download example, this article directly give the implementation code, the need for friends can refer to the following ...

  • 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.