如何使用PHP将富文本转换为图像?

时间:2015-07-24 06:20:07

标签: php image rich-text-editor

我正在尝试将富文本转换为图片,但有些人如何在图片中显示html标签,我使用 PHP 图片功能将文本转换为图片。

  • 任何人都知道如何使用 PHP RICH TEXT 转换为图片

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

也许imagick extension link也是一种选择?丰富的文字是什么意思?你的意思是php中的字符串吗?

答案 2 :(得分:0)

此功能将文本转换为图像,可以设置文本的字体和颜色,将随机名称和透明背景图像保存在目录中并返回地址。

需要在根目录中创建字体并创建缓存目录。

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"); ?>'/>
相关问题