I am trying to convert rich text into an image but some how its showing html tags as it is in the image, I am using PHP image functions to convert the text into an image.
- Anyone has an idea how we can convert RICH TEXT into an image using PHP ?
I am trying to convert rich text into an image but some how its showing html tags as it is in the image, I am using PHP image functions to convert the text into an image.
You can configure PHP and ImageMagick to use Pango which is a type of rich text. I am no expert in Pango, and a lot more is certainly possible - see Pango Gallery for examples.
$img = new Imagick();
$img->setBackgroundColor(new ImagickPixel('yellow'));
$img->setPointSize(72);
$HTML = "<span fgcolor='red' bgcolor='#0000ff'>red</span>\n";
$HTML.= "<b><u>Bold and underlined</u></b>\n";
$HTML.= "<i><s>Italic and struckthrough</s></i>";
$img->newPseudoImage(800,400,"pango:$HTML");
$img->writeImage("result.png");
Keywords: ImageMagick, IMagick, PHP, HTML, markup, RTF, Rich Text Format, formatted text
Maybe imagick extension link is also an option? What do u mean by rich text? Do u mean strings in php?
This function converts text into image, can set the font and color of the text, save the random name and transparent background image in the directory and return the address.
Need to have the font in the root and create the cache directory.
function converteTextoImagem($texto,$cor_letra = '000000'){
if($texto){
$qtd_letras = strlen($texto);
$tamanho = $qtd_letras*6.8;
$im = imagecreate($tamanho, 14);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$r_l =substr($cor_letra, 0, 2);
$g_l =substr($cor_letra, 2, 2);
$b_l =substr($cor_letra, 4, 2);
$r_l = "0x".$r_l;
$g_l = "0x".$g_l;
$b_l = "0x".$b_l;
$cor_letra = imagecolorallocate($im,$r_l,$g_l,$b_l);
$font_size = 11; //Tamanho da font
$anglo = 0; //Anglo do texto em graus
$dest_x = 0; //Posição do texto na horizontal
$dest_y = 11; //Posição do texto na vertical
$font = './Helvetica LT 47 Light Condensed.ttf'; //Font usada para escrever o texto
imagettftext($im, $font_size, $anglo, $dest_x, $dest_y, $cor_letra, $font, $texto); //Escreve o texto na imagem
// envia a imagem
$nome = rand(10000000,99999999);
imagepng($im, 'cache/img/'.$nome.'.jpg'); //Se voce quiser salva-la em um novo arquivo
//header ("Content-type: image/jpeg");
//imageJpeg($im); //Se voce quiser imprimi la na tela
//imagedestroy($im); //Deleta a imagem temporaria
return 'cache/img/'.$nome.'.png';
}
}
<image src='<?php echo converteTextoImagem("text"); ?>'/>