【问题标题】:How to convert rich text into an image using PHP?如何使用 PHP 将富文本转换为图像?
【发布时间】:2015-07-24 06:20:07
【问题描述】:

我正在尝试将富文本转换为图像,但它如何显示图像中的 html 标签,我正在使用 PHP 图像函数将文本转换为图像。

  • 有人知道我们如何使用 PHPRICH TEXT 转换为 图像 吗?

【问题讨论】:

  • 您在寻找imagegrabscreen() 吗?
  • @Raptor 感谢您的快速回复,imagegrabscreen() 抓取整个屏幕的屏幕截图,我希望将富文本区域的文本转换为图像。

标签: php image rich-text-editor


【解决方案1】:

您可以配置 PHP 和 ImageMagick 以使用富文本类型的 Pango。我不是 Pango 方面的专家,当然还有更多可能 - 请参阅 Pango Gallery 了解示例。

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


关键字:ImageMagick、IMagick、PHP、HTML、标记、RTF、富文本格式、格式化文本

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 富文本意味着文本可能有 html 元素,就像我把 Some Text 文本应该以橙色出现,但发生了什么现在跨度标签正在出现,就像它在图像上一样。
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 2015-09-25
      • 2018-08-15
      相关资源
      最近更新 更多