Hello, I have been developing a script that generates an image from a text which is the following:
<?php
ini_set("default_charset", "UTF-8");
mb_internal_encoding("UTF-8");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$font ='Amperzand.ttf';
$fuente_tamano = 25;
$texto="Es irónico que el amor sea lo más bonito y lo más letal.";
utf8_encode($texto);
$im = imagecreate(400,400);
$fondo=imagecolorallocate ($im, rand(0,255), rand(0,100), rand(0,255));
//Colores
$blanco = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$gris=imagecolorallocate ($im, 160, 160,160);
$negro=imagecolorallocate ($im, 0, 0, 0);
$colorGris = imagecolorallocate($im, 200, 200, 200);
$colorAzul = imagecolorallocate($im, 0, 0, 255);
$lines1 = explode('|', wordwrap(utf8_encode($texto), 28, '|'));
$y=50;
foreach ($lines1 as $line1) {
{
$line1=trim($line1);
//$line1 = str_pad($line1, 30,"0", STR_PAD_BOTH);
$marco= ImageTTFBBox ($fuente_tamano, 0, $font, $line1);
$x = (400 - ($marco[2]-$marco[0])) / 2;
$y += 30;
// El texto negro para que lo usaremos como borde
imagettftext($im, $fuente_tamano, 0, $x + 2 , $y + 1 , $negro, $font, $line1);
imagettftext($im,$fuente_tamano, 0, $x - 2 , $y - 1 , $negro, $font, $line1);
// El texto blanco
imagettftext($im,$fuente_tamano, 0, $x , $y, $blanco, $font, $line1);
}
}
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
How can you see the variable?
$text
contains a phrase with accents that when passed to an image is incomplete and with errors.
I wonder if there is any function that solves this problem and how to implement it in this code.
I've tried utf8_decode();
but I can't solve it
Using the given solution of using utf8_encode();
and leaving the script in this way but the problem still continues, I hope someone can tell me where it is wrong.
you can try adding this to your PHP file:
Similarly, you must correct the following line:
A:
as you had mentioned use utf8_decode
Cheers
I hope you can use this function using
mb_convert_encoding()
,preg_replace()
and regular expressions (regex). (Maybe there is a better regex but I don't know much so I hope it works for you)don't use
utf8_decode();
usesutf8_encode();
since you are "decoding" it to utf8 and what you need to do is "encode" it.