PHP imagecreatetruecolor Create high definition and transparent Picture Code summary _php Tutorial

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 representing 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 has been loaded this function has been in existence, but when the GD2 is not installed, PHP will send a fatal error and exit. In PHP 4.2.x This behavior instead of an error is issued instead of a warning. Other versions define this function only when the correct GD version is installed.

Create a new GD image stream and output the image
Copy CodeThe code is as follows:
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 (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 as imagecolorallocate () with alpha increases the transparency parameter.


$image
Image resources, through the creation of image functions, such as, a return 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, and 127 is completely transparent.
Take a look at a imagecolorallocatealpha example tutorial
Copy CodeThe 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 Create HD image function
Imagecreatetruecolor () returns an image identifier representing the black image of the specified size.

According to your PHP and GD version of the function definition 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 the case is installed PHP will issue a fatal error and exit.

In PHP version 4.2.x This behavior is different for people who warn, not a mistake. Other versions only define this function

Yes

See Example
Copy CodeThe 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 aspect of cooperation-combining some examples and then dynamically generated text. However, with this setting, I can get

Work to a transparent background also.
Copy CodeThe 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, (+), $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, A, $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 uses imagecreatetruecolor to dynamically generate high-definition image code
Copy CodeThe code is as follows:
Example use Imagecreatetruecolor with us
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--combine good examples and then dynamically generate the text. But, 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, A, $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 addition to, and 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 Tutorial E ($png, N, D, D, $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 and also go through the alpha level set to fully transparent (127) image.

The new bar generated from the above code will have a completely transparent background (a red circle dragged to the image in Photoshop to learn about itself)
The resulting PNG from the code above'll has a red circle on a fully transparent background (drag the image into Photos Hop to see for yourself)

http://www.bkjia.com/PHPjc/321748.html www.bkjia.com true http://www.bkjia.com/PHPjc/321748.html techarticle (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 ...

  • Related Article

    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.