PHP generating image with accents "buggers"

Asked

Viewed 439 times

1

I’m generating an image, which is basically a simple text.
Only the text gets the "buggy" accents, all incorrect, even if I set the encoding to utf-8.
Here’s 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 "abridged version" of the code I want to do. It involves more than a single sentence... The code is just for example of my problem.
The draw() it runs only once. What varies is only the text...

1 answer

3


I really will need another function, in this case use the imagettftext:

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

First copy a font in 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);
  • I tried this way but it appears "The image contains errors and cannot be displayed." :/

  • Copied the source arial.ttf in the script folder? @Superbomber

  • Yes I copied, I took the source of Windows and server is Linux... Could be this?

  • @Superbomber removes the header('Content-Type: image/png'); see the error that occurs and let me know.

  • Our I hadn’t thought of it...rsrs here: Warning: imagettftext() [Function.imagettftext]: Could not find/open font in /home/a4071498/public_html/index.php on line 39 ... But I raised the source, see: http://prntscr.com/7q1z34

  • @Superbomber then it was my mistake, put the following absolute path. I will edit the script in the reply and you copy. Only 1 min

  • @Superbomber so it was my mistake, I’ve already edited the answer, now it points the whole way.

  • Another error is now occurring: Warning: imagettftext() [Function.imagettftext]: Could not read font in /home/a4071498/public_html/index.php on line 40 ... Apparently he couldn’t read the source, maybe because it’s for Windows, I don’t know if it makes a difference... But if you do, you know where to download a font for Linux?

  • But the function imagettftext is to load ttf fonts anyway, this is weird. I’ll try it on my Debian and let you know

  • @Superbomber Managed to find the problem?

  • Unfortunately not... But don’t worry, I decided to use phrases without accents. Thank you very much. :)

  • 1

    Hope not give up rs, I will test here, one minute @Superbomber

  • Let’s go continue this discussion in chat. @Superbomber

  • Okay, I’m already logged in to the chat.

  • @Superbomber out of chat? See the message?

  • @Superbomber maybe this script using putenv help you http://answall.com/a/87967/3635

Show 11 more comments

Browser other questions tagged

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