Home >Backend Development >PHP Problem >Specific steps to create images in php
php specific steps to create an image: 1. Set the header and tell the browser the MIME type to be generated; 2. Create a canvas; 3. Perform color management; 4. Fill color; 5. Draw graphics and Text; 6. Output image; 7. Destroy image.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
Specific steps for creating images with PHP
There are three types in total:
<?php header("content-type: image/png");
resource imagecreatetruecolor (int $width, int $height) Create a new true color image
Returns an image identifier, representing a black image with size width and height.
Return value: Return the image resource after success, return FALSE after failure.
$width = 200; $height = 100; $img = imagecreatetruecolor($width,$height);
**int imagecolorallocate (resource $image, int $red, int $green, int $blue)**Assign color to an image
red , green and blue are the red, green and blue components of the desired color respectively. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF
$color = imagecolorallocate($img,0xcc,0xcc,0xcc); // 灰色
bool imagefill ( resource $image , int $x , int $ y , int $color )
The image coordinates x and y (the upper left corner of the image is 0, 0) are filled with color (that is, all points with the same color as x and y and adjacent points will be be filled).
imagefill($img,0,0,$color);
for ($i= 0; $i < 100; $i++) { $color = imagecolorallocate($img,rand(0,25), rand(0,255), rand(0,255)); $x = rand(0,$width); $y = rand(0,$height); imagesetpixel($img, $x, $y, $color); }
for ($i= 0; $i < 10; $i++) { $color = imagecolorallocate($img,rand(0,25), rand(0,255), rand(0,255)); $x = rand(0,$width); $y = rand(0,$height); $x1 = rand(0,$width); $y1 = rand(0,$height); imageline($img, $x, $y, $x1, $y1,$color); }
##Draw text
$color = imagecolorallocate($img,154, 75, 65)); $font = "simsunb.ttf"; $str = "hello , how are you?"; imagettftext($img, 30, 45, $x, $y, $color, $font, $str);
bool imagepng ( resource $image [, string $filename ] )
Output the GD image stream (image) in PNG format to the standard output (usually the browser), or if using filename If a filename is given, output it to that file.imagepng($img);
Release the memory associated with the image 推荐学习:《PHP视频教程》imagedestroy($img);
The above is the detailed content of Specific steps to create images in php. For more information, please follow other related articles on the PHP Chinese website!