PHP generating image with accents "bugados"

1

I'm generating an image, which is basically a simple text. Only the text gets the accents "bugados", all incorrect, even I defining the coding for utf-8. Here is the code:

<?php
   header('Content-Type: image/png; charset=utf-8');
   draw("Olá, como vai você?");

   function draw($text){
      $imagem = imagecreate(700, 30);
      $fundo  = imagecolorallocate($imagem, 241, 243, 240);
      $color  = imagecolorallocate($imagem, 200, 30, 30);
      imagestring($imagem, 5, 0, 0, $text, $color);
      imagepng($imagem);
      imagedestroy($imagem);
   }
?>

This is just a "short version" of the code I want to do. Because it involves more than a single sentence ... The code is just for example of my problem.
The draw() it is executed only once. What varies is just the text ...

    
asked by anonymous 08.07.2015 / 00:16

1 answer

3

I actually tested another function, in this case use imagettftext :

array imagettftext ( resource $imagem , float $tamanho, float $angulo, int $posicaoX, int $posicaoY, int $cor , string $fonte , string $texto)

First copy a font into the same script folder, for example arial.ttf :

<?php
define('FULL_PATH', rtrim(strtr(dirname(__FILE__), '\', '/'), '/') . '/');

header('Content-Type: image/png');

//Cria 400x30
$im = imagecreatetruecolor(400, 30);

$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

$texto = 'áéíóú';
$fonte = FULL_PATH . 'arial.ttf';

imagettftext($im, 20, 0, 11, 21, $black, $fonte, $texto);

imagepng($im);
imagedestroy($im);
    
08.07.2015 / 00:33