1

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 ?
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
Alok Banjare
  • 170
  • 1
  • 11
  • Are you looking for [imagegrabscreen()](http://php.net/manual/en/function.imagegrabscreen.php) ? – Raptor Jul 24 '15 at 06:23
  • @Raptor Thanks for the quick reply, imagegrabscreen() grabs the screen shot of the whole screen, I am looking to convert the Rich textarea's text into an image. – Alok Banjare Jul 24 '15 at 06:26

3 Answers3

1

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");

enter image description here


Keywords: ImageMagick, IMagick, PHP, HTML, markup, RTF, Rich Text Format, formatted text

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

Maybe imagick extension link is also an option? What do u mean by rich text? Do u mean strings in php?

cedricliang
  • 352
  • 1
  • 7
  • Rich Text means the text may have html element like if I put Some Text Text should occur in Orange color, But what is happening now span tags are coming as it is on image. – Alok Banjare Jul 24 '15 at 06:56
0

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"); ?>'/>