PHP as "draw" an image

Asked

Viewed 1,651 times

0

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

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

1 answer

3


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?

  • 1

    @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.

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

  • @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 :)

  • 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

Browser other questions tagged

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