PHP Imagick don't use custom font > convert do

2019-08-24 18:18发布

问题:

It's driving me insane.. Just searched for hours to find a solution. But nothing really found. So hopefully anyone can help me.

I'm trying to create an customized image on the fly. I use this to create the images: https://github.com/Treinetic/ImageArtist

Everything works fine except the font. This is the snippet from the class:

    $im = new \Imagick();
    $background = new \ImagickPixel('none');

    $im->setBackgroundColor($background);


    $im->setFont($font->getPath());
    $im->setPointSize($writer->getSize() * (0.75));
    $im->setGravity(\Imagick::GRAVITY_EAST); //later we will have to change this

    $width = $writer->getWidth();
    $height = $writer->getHeight();
    $text = $writer->getText();
    $margin = $writer->getMargin();

    $im->newPseudoImage($width, $height, "pango:" . $text );
    $clut = new \Imagick();
    $clut->newImage(2, 2, new \ImagickPixel($color->toString()));
    $im->clutImage($clut);
    $clut->destroy();

    $im->setImageFormat("png");
    $image = imagecreatefromstring($im->getImageBlob());
    $template = $this->imageHelper->createTransparentTemplate($width+ (2*$margin),$height+ (2 *$margin));

    $img = new Image($template);
    $text = new Image($image);

    imagedestroy($image);
    imagedestroy($template);

    return $img->merge($text,$margin,$margin);

The

$font-getPath();
looks correct and echo
/var/www/vhosts/example.com/dev-test/ImageArtist/src/lib/AssetManager/../../resources/Fonts/Gotham-Light.ttf

This is the full code

use Treinetic\ImageArtist\lib\PolygonShape;
use Treinetic\ImageArtist\lib\Text\TextBox;
use Treinetic\ImageArtist\lib\Text\Color;
use Treinetic\ImageArtist\lib\Text\Font;
use Treinetic\ImageArtist\lib\Overlays\Overlay;
use Treinetic\ImageArtist\lib\Image;

require('../vendor/autoload.php');

$main_image = new Image("ranks/main-image.png");

$textBox = new TextBox(720,40);
$textBox->setColor(new Color(0,0,0, 125));
$textBox->setFont(Font::getFont('./Gotham-Light.ttf'));
$textBox->setSize(42);
$textBox->setMargin(0);
$textBox->setText($_GET["name"]);

$main_image->setTextBox($textBox, 40, 620);
$main_image->save("./newImage.png",IMAGETYPE_PNG);

But the font specified is not used by Imagick. If i try to create an image from den command line via

convert -font /var/www/vhosts/example.com/dev-test/ImageArtist/src/lib/AssetManager/../../resources/Fonts/Gotham-Light.ttf -pointsize 72 label:Test test.gif

And... The Font is used on command line created image :/ The font file is reachable and readable via php but not used via Imagick ....

Does anyone has an idea why php imagick can't use my font but convert -font can?

The script is running on PHP-FPM 7.0.23 on an dedicated root server with nginx as an RPS.

Hope you can help me out.. It really drives me crazy :)

Thanks Stanlay

SOLVED

Found out that

$im->newPseudoImage($width, $height, "pango:" . $text );
cause the issue. Switching to
$im->newPseudoImage($width, $height, "caption:" . $text );
and everything works fine.