2

我对 PHP 函数 imagettftext 有疑问。我有从带有文本信息的数据库生成卡片图像的代码。对于一些卡片,我有问题 - 单词是相互重叠的(就像这里一样)。

我的代码看起来像这样(字体大小根据文本的长度而变化)

            $length = strlen($cardInfo->description);
            if ($length < 15) {
                $divide = 15;
                $fontSize = 16;
                $lineHeight = 25;
                $startPos = 220;
            } else if ($length < 70) {
                $divide = 25;
                $fontSize = 12;
                $lineHeight = 18;
                $startPos = 210;
            } else if ($length < 110) {
                $divide = 28;
                $fontSize = 10;
                $lineHeight = 14;
                $startPos = 210;
            } else {
                $divide = 38;
                $fontSize = 8;
                $lineHeight = 13;
                $startPos = 210;
            }
            $description = wordwrap($cardInfo->description, $divide, ">>>");
            $lines = explode(">>>", $description);
            $count = 0;
            foreach ($lines as $line) {
                $position = $count * $lineHeight;
                $count++;
                imagettftext($image, $fontSize, 0, 28, ($startPos + $position), $black, $font, $line);
            }

数据库中的文本如下所示:

Oblehací stroj

Imunita vůči střelám /Tato jednotka je imunní vůči střeleckým zraněním/

其他问题是换行:here。我不知道为什么“jídlo”这个词在下一行。

感谢您的任何回答!

4

3 回答 3

3

很久以前,我写了一个相当复杂的类来归档一个类似的任务。

我不再有此代码,但步骤相当简单。

第一:不要依赖计算,php依赖稀有字体。

php 的自动换行功能在这里毫无意义,因为您处理的是 php 未知的字符集宽度(例如跟踪)。Wordwrap 假定所有字符都具有相同的字符宽度。

因此,您必须使用 imagettfbbox 构建自己的自动换行功能。然后,您必须确定小写“x”字母和大写“X”字母的大小。这些字母是计算您自己的行高/行距的标准。我还建议您手动分隔单词,因为 PHP 并不总是能正确识别空白宽度。

希望这可以帮助你...

于 2012-07-16T10:55:56.517 回答
1

这对我有用。你需要arial.ttfhttp://dark-project.cz/CardDatabase/cards/lehky_katapult.png

class Test_Canvas {
    protected $res=null;

    public function __construct($width, $height) {
        $this->res = imagecreatetruecolor($width, $height);
        imagealphablending($this->res, true);
        imagesavealpha($this->res, true);
        imagefill($this->res, 0, 0, imagecolorallocatealpha($this->res, 0, 0, 0, 127));
    }

    public function copyTo(Test_Canvas $canvas, $x, $y) {
        imagecopyresampled($canvas->res, $this->res, $x, $y, 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight());
    }

    public function getWidth() {
        return imagesx($this->res);
    }

    public function getHeight() {
        return imagesy($this->res);
    }

    public function saveAsPNG() {
        imagepng($this->res);
    }
}

class Test_Canvas_Image_PNG extends Test_Canvas {
    public function __construct($filename) {
        $res = imagecreatefrompng($filename);
        $w = imagesx($res);
        $h = imagesy($res);
        parent::__construct($w, $h);
        imagecopymerge($this->res, $res, 0, 0, 0, 0, $w, $h, 100);
    }
}

class Test_Canvas_Textarea extends Test_Canvas {
    private $text;
    private $fontsize;
    private $fontfile;

    public function __construct($width, $height, $text, $fontsize, $fontfile) {
        parent::__construct($width, $height);
        $this->text = $text;
        $this->fontsize = $fontsize;
        $this->fontfile = $fontfile;
        $this->removeDuplicateWhitespace();
        $this->formatText();
        $this->applyText();
    }

    private function removeDuplicateWhitespace() {
        $this->text = preg_replace('/[ \t]+/', ' ', $this->text);
    }

    private function formatText() {
        $lines = explode("\n", $this->text);
        $res = array();
        foreach ($lines as $line) {
            $res[] = $this->insertAdditionalLinebreaks($line);
        }
        $this->text = join("\n", $res);
    }

    private function insertAdditionalLinebreaks($line) {
        $words = $this->splitWords($line);
        $res = array();
        $line = "";
        while(count($words)) {
            $word = array_shift($words);
            $testLine = "{$line} {$word}";
            $width = $this->getTextWidth($testLine);
            if($width > $this->getWidth()) {
                $res[] = $line;
                $line = $word;
            } elseif(!count($words)) {
                $res[] = $testLine;
            } else {
                $line = $testLine;
            }
        }
        return join("\n", $res);
    }

    private function getTextWidth($text) {
        $boundaries = imagettfbbox($this->fontsize, 0, $this->fontfile, $text);
        $x1 = min($boundaries[0], $boundaries[6]);
        $x2 = max($boundaries[2], $boundaries[4]);
        return $x2 - $x1;
    }

    private function splitWords($text) {
        return explode(' ', $text);
    }

    private function applyText() {
        $lines = explode("\n", $this->text);
        foreach($lines as $lineNo => $line) {
            imagettftext($this->res, $this->fontsize, 0, 0, ($lineNo + 1) * ($this->fontsize + 5), imagecolorallocate($this->res, 0, 0, 0), $this->fontfile, $line);
        }
    }
}

$rootPath = dirname(__FILE__).'/';
$imageFilename = "{$rootPath}test.png";

$description = "Oblehací stroj\nImunita vuci strelám /Tato jednotka je imunní vuci streleckým zranením/ ";
$description .= $description;
$description .= $description;

header('Content-Type: image/png');

$canvas = new Test_Canvas_Image_PNG($imageFilename);
$text = new Test_Canvas_Textarea(179, 92, $description, 9, 'arial.ttf');
$text->copyTo($canvas, 25, 193);
$canvas->saveAsPNG();
于 2012-08-03T21:56:32.187 回答
0

你不应该对你需要的盒子使用这样的估计。

函数 imagettfbbox() 可以为您提供显示文本所需的框的明确答案。

http://nl3.php.net/manual/en/function.imagettfbbox.php

希望有帮助。

于 2012-07-16T13:07:23.820 回答