Here comes a library that everyone is very familiar with. For image and graphics processing, the GD library is a hurdle that PHPers can't get around. From the very early CMS or Discuz era, all kinds of open source software will clearly point out that the GD library is a must-have extension when they are installed. Of course, in the current version of PHP, I believe you will not ignore this library when compiling. Whether you are adding watermarks to pictures, generating thumbnails or making captchas pictures, you cannot do without the GD library.

Of course, we still learn some commonly used or fun functions in the GD library from the shallower to the deeper.

GD library information in the current environment

First of all, we can check the GD library version and supported image format information in the current PHP environment.

var_dump(gd_info());
// array(13) {
//     ["GD Version"]=>
//     string(26) "bundled (2.1.0 compatible)"
//     ["FreeType Support"]=>
//     bool(true)
//     ["FreeType Linkage"]=>
//     string(13) "with freetype"
//     ["GIF Read Support"]=>
//     bool(true)
//     ["GIF Create Support"]=>
//     bool(true)
//     ["JPEG Support"]=>
//     bool(true)
//     ["PNG Support"]=>
//     bool(true)
//     ["WBMP Support"]=>
//     bool(true)
//     ["XPM Support"]=>
//     bool(false)
//     ["XBM Support"]=>
//     bool(true)
//     ["WebP Support"]=>
//     bool(true)
//     ["BMP Support"]=>
//     bool(true)
//     ["JIS-mapped Japanese Font Support"]=>
//     bool(false)
//   }

The gd_info() function can view the version information of the current GD library. The other fields are the support status of various picture modes. It can be seen that in my system environment, except for the XPM format which is not supported, other various picture formats are available. It is normally supported. The last one is the support of Japanese fonts, which is not available in our current environment.

Get basic picture information

getimagesize(), this function to get the picture information, from the name it is a function to get the picture size but also contains some other information, and this function actually has nothing to do with the GD library, which means that the GD library is not needed In fact, this function can also be used.

var_dump(getimagesize("../img/1.png"));
// array(6) {
//     [0]=>
//     int(150)
//     [1]=>
//     int(150)
//     [2]=>
//     int(3)
//     [3]=>
//     string(24) "width="150" height="150""
//     ["bits"]=>
//     int(8)
//     ["mime"]=>
//     string(9) "image/png"
//   }

The returned result is actually very simple, 0 and 1 are the width and height of the picture respectively, and 2 is the picture type. As mentioned in the previous article, it corresponds to the corresponding picture type in the IMAGETYPE_ constant. 3 is the width and height of the picture in text form, which can be directly used in the img tag. It can be seen that PHP is really a language for the web, and even the function for obtaining the picture size must bring such an attribute back. bits is the number of color bits of the image. mime is the MIME type of the picture.

In addition, the getimagesize() function has a second parameter, which is a reference type parameter, which will return different JPG APP IDs in an associative array. In other words, it also obtains some additional information for JPG-related pictures, which is actually a bit like the information in EXIF learned in the previous article.

var_dump(getimagesize("../img/2.jpg", $info));
// array(7) {
//     [0]=>
//     int(300)
//     [1]=>
//     int(244)
//     [2]=>
//     int(2)
//     [3]=>
//     string(24) "width="300" height="244""
//     ["bits"]=>
//     int(8)
//     ["channels"]=>
//     int(3)
//     ["mime"]=>
//     string(10) "image/jpeg"
//   }

var_dump($info);
// array(1) {
//     ["APP0"]=>
//     string(14) "JFIF��"
//   }

In addition, if it is a JPG picture, we will return an additional channels attribute, which means that if the picture is in RBG format, the return is 3, if it is in CMYK format, the return is 4.

We can also use getimagesize() to obtain information about remote files.

var_dump(getimagesize("https://upload-images.jianshu.io/upload_images/1074666-8df66a94d61cac74.png?imageMogr2/auto-orient/strip|imageView2/2/w/374/format/webp"));
// array(6) {
//     [0]=>
//     int(374)
//     [1]=>
//     int(617)
//     [2]=>
//     int(18)
//     [3]=>
//     string(24) "width="374" height="617""
//     ["bits"]=>
//     int(8)
//     ["mime"]=>
//     string(10) "image/webp"
//   }

In addition to directly manipulating image files, we can also obtain image information directly from strings. However, these strings are generally the binary information of the read image file. If you come directly to a real string like Hello World, it won't be able to parse out any picture content if you kill it.

$data = file_get_contents('../img/1.png');
var_dump(getimagesizefromstring($data));
// array(6) {
//     [0]=>
//     int(150)
//     [1]=>
//     int(150)
//     [2]=>
//     int(3)
//     [3]=>
//     string(24) "width="150" height="150""
//     ["bits"]=>
//     int(8)
//     ["mime"]=>
//     string(9) "image/png"
//   }

Get some information about the picture

We can get the extension of the file by the type of the image. This type constant is also of type IMAGETYPE_.

var_dump(image_type_to_extension(IMAGETYPE_PNG)); // string(4) ".png"
var_dump(image_type_to_extension(IMAGETYPE_JPEG, FALSE)); // string(4) "jpeg"

The image_type_to_extension() function has a second parameter. If it is set to false, it will not be added.

var_dump(image_type_to_mime_type(IMAGETYPE_PNG)); // string(9) "image/png"
var_dump(image_type_to_mime_type(IMAGETYPE_JPEG)); // string(10) "image/jpeg"

Similarly, there is image_type_to_mime_type() function to get the MIME information content of the picture. In addition, there is a function to get the picture types supported by the current system.

var_dump(imagetypes()); // int(111)

Uh, it's weird, why only one 111 was returned? In fact, it is the saved binary image support information. For example, if we want to know whether there is support for PNG images in the current system, we can use it like this:

var_dump(imagetypes() & IMAGETYPE_PNG); // int(3)

And IMAGETYPE_PNG and the result after that is the value of IMAGETYPE_PNG itself, which means that the current system supports this image type operation.

Small example: create a picture of yourself

Finally, let's look at a small example, which is actually using these functions in the GD library to create a simple small picture.

$im = @imagecreate(100, 50) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "Test 测试", $text_color);
imagepng($im, '../img/test.png');
imagedestroy($im);

In fact, the content of each function is well understood, and the function name has been written very clearly. Of course, we will learn it again in detail in a later article. In the final generated image, the Chinese is also garbled, but it can already be used as an image verification code, is it very simple?

Summarize

Today's content is not much, and it is the use of some basic and simple functions. Of course, this is only the first article at the beginning. The GD library is not a simple small extension. It contains a lot of content, and the functions that can be implemented are also very complicated. Don't go away, keep paying attention!

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202012/source/1. Learn the use of GD library in PHP together (1). php

Reference documents:

https://www.php.net/manual/zh/book.image.php

Searchable on their respective media platforms [Hardcore Project Manager]


硬核项目经理
90 声望18 粉丝