PHP Imagecreatetruecolor Create high-definition and transparent Picture Code summary _php Foundation

Source: Internet
Author: User
Tags image identifier imagejpeg
(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 that represents a black image with a size of 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 as long as the GD module is loaded this function always exists, but when the GD2 is not installed, PHP will send a fatal error and exit. This row in the PHP 4.2.x is a warning instead of an error instead. The other version defines this function only when the correct version of GD is installed.

Create a new GD image stream and output the image
Copy Code code 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 the GD 2.0.1 or later version (recommended 2.0.28 and later).

PHP Imagecolorallocatealpha Create a transparent picture instance
Imagecolorallocatealpha (Resource $image, int $red, int $green, int $blue, int $alpha)
Imagecolorallocatealpha () behaves the same imagecolorallocate () with alpha to increase the transparency parameters.


$image
Image resources, through the creation of image features, such as a return to Imagecreatetruecolor ().

$red
The value of the red component.

$green
The green ingredient of value.

$blue
The value of the blue ingredient.

$alpha
A value between 0 and 127. 0 means completely opaque, while 127 means full transparency.
Take a look at a imagecolorallocatealpha example tutorial
Copy Code code as follows:

<?php
$size = 300;
$image =imagecreatetruecolor ($size, $size);

Something to get a white background and 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 Create HD image function
Imagecreatetruecolor () returns an image identifier representing a black image of the specified size.

According to your PHP and GD version of the function defined or not. For PHP 4.0.6 through 4.1.x This function always exists

If the Guangdong module is loaded, but it requires GD2 to be installed in case PHP will issue a fatal error and exit.

The PHP 4.2.x version of this behavior is different for people to issue a warning, not a mistake. Other versions define this work only

Yes

Take a look at the example
Copy Code code as follows:

<?php
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 cooperation-combine some examples and then dynamically generate the text. However, with this setting, I can get

To the transparent background of the work also.
Copy Code code as follows:

<?php
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, $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, $black, $font, $text);

Add the text
Imagettftext ($im, 9, 0, $white, $font, $text);

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

ph Use imagecreatetruecolor dynamic generation of High-definition image code
Copy Code code as follows:

Example use us to use 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);

I put this together--combining good examples and then dynamically generating text. However, with this set up, I can get a transparent background as well as work.
Example Two 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, $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, $black, $font, $text);

Add the text
Imagettftext ($im, 9, 0, $white, $font, $text);

Using imagepng () results in clearer text compared with imagejpeg ()
Imagepng ($im);
Imagedestroy ($im);

/*
Instance three create a transparent picture

If you want to create a PNG image * transparent *, where the background is completely transparent, all actions take place in reference, in addition, then do the following:
*/
$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 Tutorials E ($png, $red);

Header ("Content-type:image/png");
Imagepng ($png);

All you have to do is create a real color image, make sure the Alpha save state is, and then fill in a color that also goes through the alpha level set to fully transparent (127) of the image.

From the above code generated by the new Baba will have a completely transparent background (a red circle dragged into Photoshop image to understand yourself)
The resulting PNG from the code above would have a red circle on a fully transparent (background the image into drag Hop to = for yourself)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.