PHP Imagick不使用自定义字体>转换呢

时间:2017-09-22 04:06:24

标签: php nginx fonts imagemagick imagick

它让我疯了..只是搜索了几个小时才找到解决方案。但没有真正找到。希望有人能帮助我。

我正在尝试动态创建自定义图像。我用它来创建图像:https://github.com/Treinetic/ImageArtist

除了字体外,一切正常。这是该课程的片段:

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

$font-getPath();
看起来正确并且回显
/var/www/vhosts/example.com/dev-test/ImageArtist/src/lib/AssetManager/../../resources/Fonts/Gotham-Light.ttf

这是完整的代码

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

但是Imagick没有使用指定的字体。如果我尝试通过

从den命令行创建图像
convert -font /var/www/vhosts/example.com/dev-test/ImageArtist/src/lib/AssetManager/../../resources/Fonts/Gotham-Light.ttf -pointsize 72 label:Test test.gif

并且......字体用于命令行创建的图像:/字体文件可以通过php访问和读取,但不能通过Imagick使用....

有没有人知道为什么php imagick不能使用我的字体但转换-font可以?

该脚本在专用根服务器上的PHP-FPM 7.0.23上运行,其中nginx为RPS。

希望你能帮帮我..这真让我疯狂:)。

由于 Stanlay

解决

发现

$im->newPseudoImage($width, $height, "pango:" . $text );
导致问题。切换到
$im->newPseudoImage($width, $height, "caption:" . $text );
,一切正常。

0 个答案:

没有答案