New users are puzzled by the imagecreatetruecolor () and imagecreate () functions of php Image Generation. First, let's take a look at the official explanations of these two functions:
Resource imagecreatetruecolor (int $ x_size, int $ y_size)
Returns an image identifier, representing a black image of x_size and y_size.
Resource imagecreate (int $ x_size, int $ y_size)
Returns an image identifier, representing the size of an image.
The two have some differences when changing the background color.:
Imagecreatetruecolor must be filled with imagefill ()
Imagecreate () needs to use imagecolorAllocate () to add the background color
The php case is as follows::
Copy codeThe Code is as follows: <? Php
$ Img = imagecreatetruecolor (100,100); // create a true color image resource
$ Color = imagecolorAllocate ($ img, 200,200,200); // assign a gray color.
Imagefill ($ img, $ color); // fill in the gray from the upper left corner
Header ('content-type: image/jpeg '); // jpg format
Imagejpeg ($ img); // shows the gray square
?>
Copy codeThe Code is as follows: <? Php
$ Img = imagecreate (100,100 );
Imagecolorallocate ($ img, 200,200,200 );
Header ('content-type: image/jpeg ');
Imagejpeg ($ img );
?>