PHP how to "draw" an image

0

I would like to know the following, how can I "draw" an image with PHP?
I have seen on some websites, PHP pages that make an image on time. What I want is something simple, just a background with border and a text. If it is not possible, only a simple text already makes me happy.

I already know that I need to set Content-Type para image/png . I just need the functions to "draw" the image.

    
asked by anonymous 07.07.2015 / 23:10

1 answer

3

For this task you can use GD or imagemagick / imagick, drawing example with GD:

Drawing a rectangle with GD

Source: link

<?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);

Transforming a string into text with GD

Source: link

<?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: link

<?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:

07.07.2015 / 23:26