Home  >  Article  >  Backend Development  >  php imagecreatetruecolor code summary for creating high-definition and transparent images_PHP tutorial

php imagecreatetruecolor code summary for creating high-definition and transparent images_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:27931browse

(PHP 4 >= 4.0.6, PHP 5)
imagecreatetruecolor — Create a new true color image

Description
resource imagecreatetruecolor ( int $x_size , int $y_size )
imagecreatetruecolor() Returns an image identifier representing a black image of size x_size and y_size.

Whether this function is defined depends on the version of PHP and GD. From PHP 4.0.6 to 4.1.x, this function always exists as long as the GD module is loaded. However, if it is called when GD2 is not installed, PHP will issue a fatal error and exit. In PHP 4.2.x this behavior was changed to issue a warning instead of an error. Other versions only define this function when the correct GD version is installed.

Create a new GD image stream and output the image

Copy the code The code is as follows:

< ?php
header("Content-type: image/png");
$im = @imagecreatetruecolor(50, 100)
or die("Cannot Initialize new GD image stream"); ​​
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

Note: This function requires GD 2.0.1 or higher (2.0.28 and higher is recommended).

php imagecolorallocatealpha creates a transparent image instance
imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha)
imagecolorallocatealpha() Behaves the same as imagecolorallocate() with alpha increasing the transparency parameter.


$image
Image resource, created through an image function, such as one returned by imagecreatetruecolor().

$red
The value of the red component.

$green
Value of green ingredients.

$blue
The value of the blue component.

$alpha
A value between 0 and 127. 0 means completely opaque, while 127 means completely transparent.
Let’s take a look at the imagecolorallocatealpha example tutorial
Copy the code The code is as follows:

$size = 300;
$image=imagecreatetruecolor($size, $size);

// something to get a white background with black border
$back = imagecolorallocate($image, 255, 255, 255);
$border = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back);
imagerectangle($image, 0, 0, $size - 1, $size - 1, $border);

$yellow_x = 100;
$yellow_y = 75;
$red_x = 120;
$red_y = 165;
$blue_x = 187;
$blue_y = 125;
$radius = 150;

// allocate colors with alpha values ​​
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
$red = imagecolorallocatealpha($image, 255, 0, 0, 75);
$blue = imagecolorallocatealpha($image, 0, 0, 255, 75);

// drawing 3 overlapped circle
imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow);
imagefilledellipse($image, $ red_x, $red_y, $radius, $radius, $red);
imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue);

// don't forget to output a correct header!
header('Content-type: image/png');

// and finally, output the result
imagepng($image);
imagedestroy ($image);
?>


php imagecreatetruecolor creates a high-definition image function
imagecreatetruecolor() returns an image identifier representing the specified size Black image.

Depending on whether the function is defined or not in your PHP and GD versions. For PHP 4.0.6 through 4.1.x this function is always present

and if the GD module is loaded but it requires GD2 to be installed PHP will issue a fatal error and exit.

With PHP version 4.2.x this behavior is a warning rather than a bug. Other versions only define this function

,

Look at the example
Copy the code The code is as follows:

header ('Content-type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>


I propose this collaboration - combining some examples and then dynamically generated text. But, with this setup, I was able to get

to transparent backgrounds working too.
Copy code The code is as follows:

// Set the content-type

header('Content-type: image/png');

// Create the image
$im = imagecreatetruecolor(175, 15);
imagesavealpha($im, true);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 25, $black);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);

// The text to draw
$text = $_GET['text'];
// Replace path by your own font path
$font = 'catriel regular.ttf';

// Add some shadow to the text
imagettftext($im, 9, 0, 13, 16, $black, $font, $text);

// Add the text
imagettftext($im, 9, 0, 12, 15, $white, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

ph利用imagecreatetruecolor动态生成高清图片代码
复制代码 代码如下:

//实例用我们用imagecreatetruecolor
header ('Content-type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);

//我把这个一起 - 结合较好的例子,然后动态生成的文本。但是,与此成立,我能得到透明背景以及工作。
//实例二imagecreatetruecolor
header('Content-type: image/png');

// Create the image
$im = imagecreatetruecolor(175, 15);
imagesavealpha($im, true);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 25, $black);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);

// The text to draw
$text = $_GET['text'];
// Replace path by your own font path
$font = 'catriel regular.ttf';

// Add some shadow to the text
imagettftext($im, 9, 0, 13, 16, $black, $font, $text);

// Add the text
imagettftext($im, 9, 0, 12, 15, $white, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

/*
实例三创建透明图片

如果你想创建一个PNG图像*透明*,其中的背景是完全透明的,所有行动发生在借鉴,除此之外,然后执行下列操作:
*/
$png = imagecreatetruecolor(800, 600);
imagesavealpha($png, true);

$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);

$red = imagecolorallocate($png, 255, 0, 0);
imagefilledellips教程e($png, 400, 300, 400, 300, $red);

header("Content-type: image/png");
imagepng($png);

你要做的就是创建一个真正的彩色图像,确保阿尔法保存状态是,然后填写一个颜色,也经历了阿尔法级别设置为完全透明(127)的图像。

从上面的代码产生的巴新将有一个完全透明的背景(一红色圆圈拖到Photoshop中的图像,以了解自己)
The resulting PNG from the code above will have a red circle on a fully transparent background (drag the image into Photoshop to see for yourself)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/321748.htmlTechArticle(PHP 4 = 4.0.6, PHP 5) imagecreatetruecolor — 新建一个真彩色图像 说明 resource imagecreatetruecolor ( int $x_size , int $y_size ) imagecreatetruecolor() 返回一个图像...
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