imagick icon indicating copy to clipboard operation
imagick copied to clipboard

Bug(?) non-conforming drawing primitive definition `text' @ error/draw.c/RenderMVGContent/4469

Open 4d4ch4u32 opened this issue 2 years ago • 7 comments

PHP version:

PHP 7.4.20 (cli) (built: Jun 28 2021 22:14:08) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans

imagick version:

/var/www/html # php -i | grep imagick
/usr/local/etc/php/conf.d/docker-php-ext-imagick.ini,
imagick
imagick module => enabled
imagick module version => 3.5.1
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
imagick.allow_zero_dimension_images => 0 => 0
imagick.locale_fix => 0 => 0
imagick.progress_monitor => 0 => 0
imagick.set_single_thread => 1 => 1
imagick.shutdown_sleep_count => 10 => 10
imagick.skip_version_check => 0 => 0

Relevant php code:

        $img = new \Imagick();
        $img->setResolution(300, 380);
        $img->readImage($img2Fake);
        $img->setFormat('png24');

        $bg = new \Imagick();

        $bg->newImage(300, 380, new \ImagickPixel('#ffffff'));

        $draw = new \ImagickDraw();

        $draw->setFillColor(new \ImagickPixel("#ede717"));
        $draw->setStrokeColor(new \ImagickPixel('#ede717'));

        $bgx2 = 63.5 * (float)$avgRating;
        $draw->rectangle(24, 0, $bgx2, 380);

        $bg->drawImage($draw);
        $bg->compositeImage($img, \Imagick::COMPOSITE_OVER, 0, 0);
        $bg->setImageFormat('jpg');

        $strDraw = new \ImagickDraw();
        $strDraw->setFillColor(new \ImagickPixel('#555'));
        $strDraw->setTextAlignment(\Imagick::ALIGN_CENTER);
        $strDraw->setFontSize('32');
        $strDraw->setFontWeight(900);
        $strDraw->annotation(150, 135, $ratingText);
        $strDraw->setFontSize('20');
        $strDraw->setFontWeight(200);
        $strDraw->annotation(150, 168, $avgRating . ' / 5,0');
        $strDraw->setFontSize('18');
        $strDraw->setFontWeight(200);
        $strDraw->annotation(150, 194, number_format($count, 0, ',', '.') . ' Bewertungen');
        $bg->drawImage($strDraw);

Exception:

ImagickException: non-conforming drawing primitive definition `text' @ error/draw.c/RenderMVGContent/4469
#6 /var/www/html/src/pp/WidgetBundle/Lib/WidgetRenderer.php(95): Imagick::drawimage
#5 /var/www/html/src/pp/WidgetBundle/Lib/WidgetRenderer.php(95): pp\WidgetBundle\Lib\WidgetRenderer::renderWidget
#4 /var/www/html/src/pp/WidgetBundle/Controller/DefaultController.php(30): pp\WidgetBundle\Controller\DefaultController::renderAction
#3 /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(151): Symfony\Component\HttpKernel\HttpKernel::handleRaw
#2 /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel::handle
#1 /var/www/html/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(200): Symfony\Component\HttpKernel\Kernel::handle
#0 /app.php(162): null

Relevant Dockerfile part:

FROM registry.var-lab.com/php/php:7.4.20-fpm-alpine

...

RUN set -ex \
    && apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS imagemagick-dev libtool \
    && export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" \
    && pecl install imagick-3.5.1 \
    && docker-php-ext-enable imagick \
    && apk add --no-cache --virtual .imagick-runtime-deps imagemagick \
    && apk del .phpize-deps

It has worked without using docker, but with the same PHP version "directy" installed on an Ubuntu 16.x machine.

4d4ch4u32 avatar Mar 28 '22 11:03 4d4ch4u32

Hi, can you try:

i. List the fonts returned by Imagick::queryFonts() to see if there any fonts configured.

ii. Explicitly set a font, either one listed from above, or a TTF file you acquire from somewhere.

This error message (which is not great) seems to happen when a default font isn't available.

Danack avatar Mar 28 '22 12:03 Danack

Thanks for your reply @Danack. Can you say which one the default font is?

4d4ch4u32 avatar Mar 28 '22 12:03 4d4ch4u32

Imagick::queryFonts() outputs an empty array.

It works now, after adding the following line to the Dockerfile:

RUN apk add --no-cache msttcorefonts-installer && update-ms-fonts && fc-cache -f

4d4ch4u32 avatar Mar 28 '22 12:03 4d4ch4u32

I think either Arial or Helvetica.

I'm beginning to think though that relying on system fonts is a 'not great' way of managing fonts, and explicitly packaging the fonts you want to use might be better.

Danack avatar Mar 28 '22 12:03 Danack

At least a more descriptive error message would be very helpful!

4d4ch4u32 avatar Mar 28 '22 12:03 4d4ch4u32

Going to change this back to open so people see it, and remind myself to do something about it.

Danack avatar Apr 02 '22 15:04 Danack

Explicitly set a font, either one listed from above, or a TTF file you acquire from somewhere. How do I do this?

I have a TTF font and don't want to install the entire msttcorefonts-installer

Using Wand, Python and Docker.

hagen00 avatar Jun 08 '22 15:06 hagen00