简体   繁体   中英

Imagettftext - incorrect line spacing

I have an problem with PHP function imagettftext. I have code for generating card images from database with text informations. And with some cards I have problem - words are written over each other ( like here ).

My code looks like this (font size changes depending on the length of the text)

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

and the text in the database looks like this:

Oblehací stroj

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

Other problem is with the line wrapping: here . I don't know why the word "jídlo" is on the next line.

Thank you for any answers!

Long time ago, I have written a quite complex class to archive a similar task.

I don't have this code anymore, but the steps are fairly simple.

First: Don't rely on the calculations, php does on rare fonts.

Php's wordwrap-function is senseless here, because you deal with a charset-width (eg tracking) unknown to php. Wordwrap assumes, that all characters have the same char-width.

So, you have to build your own wordwrap-function using imagettfbbox. Then, you'll have to determine the size of the lowercase "x"-letter and the uppercase "X"-letter. These letters are the norm to calculate your own line-height/line-spacing. I also recommend you to manually separate words, since PHP does not always recognize the white-space-width correctly.

Hope this could help you...

This works for me. You need arial.ttf and http://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();

You shouldn't use such estimates for the box you'll need.

The function imagettfbbox() can give you a definite answer to the box you'll need to display the text.

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

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM