For this task you can use GD or imagemagick/Imagick, example drawing with GD:
Drawing a rectangle with GD
Source: http://php.net/manual/en/function.imagerectangle.php
<?php
// cria uma imagem de 200 x 200
$canvas = imagecreatetruecolor(200, 200);
// Aloca cores
$pink = imagecolorallocate($canvas, 255, 105, 180);
$white = imagecolorallocate($canvas, 255, 255, 255);
$green = imagecolorallocate($canvas, 132, 135, 28);
// Desenha o retangulo com estas cores
imagerectangle($canvas, 50, 50, 150, 150, $pink);
imagerectangle($canvas, 45, 60, 120, 100, $white);
imagerectangle($canvas, 100, 120, 75, 160, $green);
header('Content-Type: image/png');
imagepng($canvas);
imagedestroy($canvas);
Wiring a text string with GD
Source: http://php.net/manual/en/function.imagestring.php
<?php
//Cria uma imagem de 100 x30
$im = imagecreate(100, 30);
//Desenha um fundo branco
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Escreve um texto
imagestring($im, 5, 0, 0, 'Ola mundo!', $textcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Write a text with imagemagick
Requires PHP >= 5.1.3 and Imagemagick >= 6.2.4, for installation see: http://php.net/manual/en/imagick.setup.php
<?php
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel('gray');
/* Gera a nova imagem */
$image->newImage(800, 75, $pixel);
/* Cor do texto */
$draw->setFillColor('black');
/* Fonte e tamnanho da fonte */
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 30 );
/* Adiciona o texto */
$image->annotateImage($draw, 10, 45, 0, 'Ola mundo!');
/* Define o tipo da imagem */
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
For more details see the documentation:
Thank you, just one more thing... You can break the line if the text exceeds the image size?
– Silva97
@Superbomber I’m not sure, I don’t know if it’s possible, but I think with http://php.net/manual/en/function.ttimagefbbox.php you can do this.
– Guilherme Nascimento
Not to bother @Guilherme Nascimento ... But the text is appearing with accents all "buggy"... But I’ve already defined Content-Type with utf-8 encoding... You know what it could be?
– Silva97
@Superbomber Save the document with utf-8 without good using Sublimetext or Notepad++, if it doesn’t work open a new question please, I will test your code :)
– Guilherme Nascimento
I was already saving so, and even so happens the mistake... I asked the other question: http://answall.com/questions/73328/php-gerando-imagem-buggyaccents
– Silva97