Home  >  Article  >  Backend Development  >  Example of remote image download using gd library in PHP_PHP tutorial

Example of remote image download using gd library in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:53:41814browse

An example of remote image download using gd library in php

This article mainly introduces an example of remote image download using gd library in php. This article directly gives the implementation code, which is required Friends can refer to it

Because today I want to write a class for remote image downloading, I warmed up in advance and wrote a php gd library to implement the remote image download function. Of course, curl implementation is better. The php gd library implements the remote image download function mainly using two functions of the gd library. ImageCreateFromXXX() is used to generate image functions and ImageXXX functions.

 ?

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

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

if (!empty($_POST['submit'])){

$url = $_POST['url'];

$pictureName = $_POST['pictureName'];

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

echo '
<img src="'.$img.'">
';

}

function getPicture($url,$pictureName){

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

//Get the extension of the image

$info = getimagesize($url);

$mime = $info['mime'];

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

//Select different image generation and saving functions for different image types

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 image from url

$img_save_func($src_im, $pictureName); //Output file to file

return $pictureName;

}

?>

Remote url address:

File name:

The running result is as follows: (The picture is automatically saved in the current file directory, if you don’t understand, you can leave a message)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000114.htmlTechArticleAn example of using the gd library to implement remote image downloading in php. This article mainly introduces the use of the gd library to implement remote images in php. Download the example. This article directly gives the implementation code. Friends who need it can refer to it...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn