How does the imagecreatetruecolor function work? PHP

Asked

Viewed 670 times

0

It came in a part of the course that I am creating a helper for uploading images, and it has an exercise that I need to use imagecreatetruecolor, but I don’t quite understand what the official documentation provides:

Cria uma nova imagem true color

imagecreatetruecolor() retorna um identificador de imagem representando uma imagem preta de tamanho x_size por y_size.

As well as image identifier?

1 answer

3

Briefly we could say that:

The functions below are part of the GD library, which in turn was developed for the processing of images. It is a library open source for dynamic image creation by programmers. The library creates PNG, JPEG and GIF, among other formats is usually used to generate graphics, thumbnails, banners...

According to the official documentation, syntax:

resource imagecreatetruecolor ( int $width , int $height )

imagecreatetruecolor() returns an identifier (Resource) that represents a black image of the specified size. Summarizing:

imagecreatetruecolor ($largura ,$altura )

It takes two parameters, both are integers and returns a resource (Resource) that will be used by other methods to give continuity to the work of building an image.

See an example contained in official documentation:

<?php

header ('Content-Type: image/png');
$im = imagecreatetruecolor(120, 50)
      or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'Issoé um teste', $text_color);
imagepng($im);
imagedestroy($im);

Explaining line by line:

  1. In the first line we set a header (header) for our page, that is, it will be a PNG
  2. We use imagecreatetruecolor() to create "base" image (Resource). Summarizing: we create a new true color image
  3. imagecolorallocate() allocates a color to an image
  4. imagestring() - Draw a string horizontally. This is where we pass the Resource, information of which font will be used, text string and font color
  5. imagepng() - Output a PNG image to browser or file
  6. imagedestroy() - Destroys the image ("wipes the memory")
  • It’s not yet clear to me how it works, what exactly it does and what it’s for. This information above is in the manual, as I said, I did not understand it very well. But, thank you very much, Fábio!

  • I complemented the previous post with an initial paragraph that can take but some questions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.