【问题标题】:Output an Image for Each Word Rather than the Whole String in PHP Using Pango and Cairo在 PHP 中使用 Pango 和 Cairo 为每个单词而不是整个字符串输出一个图像
【发布时间】:2011-10-20 06:15:51
【问题描述】:

我正在研究一种解决方案,可以自动为包含复杂脚本(UTF-8 中的高棉语)的大型文档的每个单词制作图像。我发现 Pango 和 Cairo 可以正确渲染高棉语。我不是一个程序员,所以我从 Pango 和 Cairo 的 PHP 版本开始。但我不确定如何分解字符串并自动为每个单词生成图像。单词之间没有“真正的”空格,只有 Unicode 字符 U+0200B(零宽度空格)。

有人愿意帮助我吗?

这是我当前使用的输出整个字符串的代码:

<?php
header("Content-Type: image/png");
/* Make a 300x300px image surface */
$s = new CairoImageSurface(CairoFormat::ARGB32, 300, 300);
$c = new CairoContext($s);

/* Set the background to white */
$c->setSourceRGB(1, 1, 1);
$c->paint();

/* Let's draw using black 'ink' */
$c->setSourceRGB(0, 0, 0);

/* Make a Pango layout, set the font, then set the layout size */
$l = new PangoLayout($c);
$desc = new PangoFontDescription("KhmerOS Regular 28");
$l->setFontDescription($desc);
$l->setWidth(250 * PANGO_SCALE);

/* Here is the text */
$l->setMarkup("កាល​ដើម​ដំបូង​ឡើយ ព្រះ​បាន​បង្កើត​ផ្ទៃ​មេឃ និង​ផែនដី។");

/* Draw the layout on the surface */
$l->showLayout($c);

/* Output the PNG to the browser */
$s->writeToPng("php://output");
?>

【问题讨论】:

    标签: php cairo pango


    【解决方案1】:

    我用 foreach 弄明白了:

    <?php
    //header("Content-Type: image/png");
    $str = "កាល​ដើម​ដំបូង​ឡើយ ព្រះ​បាន​បង្កើត​ផ្ទៃ​មេឃ និង​ផែនដី។";
    //$words = explode('​', $str);
    $words = preg_split('/ |​/', $str);
    
    $i=1;
    foreach($words as $word) {
    /* Make a 300x300px image surface */
    $s = new CairoImageSurface(CairoFormat::ARGB32, 300, 300);
    $c = new CairoContext($s);
    
    /* Set the background to white */
    $c->setSourceRGB(1, 1, 1);
    $c->paint();
    
    /* Let's draw using black 'ink' */
    $c->setSourceRGB(0, 0, 0);
    
    /* Make a Pango layout, set the font, then set the layout size */
    $l = new PangoLayout($c);
    $desc = new PangoFontDescription("KhmerOS Regular 28");
    $l->setFontDescription($desc);
    $l->setWidth(250 * PANGO_SCALE);
    
    
    /* Here, we use Pango markup to make part of the text bold */
    
    
    
    $i++;
    
    $l->setMarkup($word);
    
    /* Draw the layout on the surface */
    $l->showLayout($c);
    
    /* Output the PNG to the browser */
    //$s->writeToPng("php://output");
    $s->writeToPng(dirname(__FILE__) . '/test'.$i.'.png');
    echo $img = "<img src=\"test".$i.".png\">";
    echo $i;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 2017-04-09
      • 2014-01-05
      • 1970-01-01
      • 2019-08-09
      相关资源
      最近更新 更多