Learn the use of GD Library in PHP (I)

Time:2021-9-16

We have reached a very familiar library. For image and graphics processing, Gd library is a barrier that PHPer can’t get around. From the very early CMS or discuz era, all kinds of open source software will clearly point out that GD library is a necessary extension when they are installed. Of course, in the current PHP version, I believe you will not ignore this library when compiling. Whether you add watermark to the picture, generate thumbnails or make verification code pictures, you can’t do without the shadow of GD library.

Of course, we still learn some common or fun functions in GD library from simple to deep.

GD library information in the current environment

First, we can view 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)
//   }

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

Get basic picture information

Getimagesize(), a function to obtain image information, is a function to obtain image size from the name, but it also contains some other information. In fact, this function has nothing to do with the GD library, that is, it does not need the extension of the GD library. 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 type of picture. As mentioned in the previous article, it corresponds to imagetype_ The corresponding picture type in the 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 born for the web. Even the function to obtain the picture size should bring such an attribute back. Bits is the number of color bits of the picture. Mime is the MIME type of the picture.

In addition, the getimagesize () function also has a second parameter, which is a parameter of reference type. It will return different JPG app identifiers as an associative array. In other words, it also obtains some additional information for JPG related images. In fact, it is 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 image, we will return an additional channels attribute, which means that if the image is in RBG format, it will return 3, and if it is in CMYK format, it will return 4.

We can also use getimagesize () to get information about the remote file.

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 the image file, we can also directly obtain the image information from the string. However, these strings are generally binary information of the read image file. If you directly come to a real string such as hello world, it can’t parse any picture content.

$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 file extension by the type of image. This type constant is also imagetype_ Type.

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

image_ type_ to_ The extension () function has the 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 are images_ type_ to_ mime_ Type() function to get the mime information content of the picture. In addition, there is a function to obtain the types of pictures supported in the current system.

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

Well, it’s strange. Why did you only return one 111? In fact, it is the stored binary image support information. For example, if we want to know whether PNG images are supported in the current system, we can use it this way:

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

Imagetype_ The result of PNG and is imagetype_ The value of PNG itself, which indicates that the current system supports the operation of this picture type.

Small example: create a picture of yourself

Finally, let’s take a look at a small example. In fact, we use 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 easy to understand, and the function name has been written clearly. Of course, we will study it again in detail in later articles. In the final generated picture, Chinese is also garbled, but it can be used as a picture verification code. Is it very simple?

summary

Today’s content is not much, but also 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 has a lot of content and can realize complex functions. Don’t go away and 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 (I). PHP

Reference documents:

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

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532