imagick icon indicating copy to clipboard operation
imagick copied to clipboard

"non-conforming drawing primitive definition `stroke-dasharray'" when setting back to null

Open neekfenwick opened this issue 2 years ago • 2 comments

Platform: Fedora 36 ImageMagick: ImageMagick-6.9.12.66-1.fc36.remi.x86_64 imagick: php74-php-pecl-imagick-im6-3.7.0-1.fc36.remi.x86_64

I'm finding the documented way of resetting the strokeDashArray behaviour throws an exception, though the docs are rather weird.

According to the docs at https://www.php.net/manual/en/imagickdraw.setstrokedasharray.php we should "To remove an existing dash array, pass a zero number_elements argument and null dash_array."

However, the signature of the function is: public ImagickDraw::setStrokeDashArray(array $dashArray): bool so there is no number_elements argument, I presume this is documentation taken from the C API.

There is a Comment on the PHP documentation:

To remove the dash and switch back to solid line, provide an array with a null value in it for $dashArray.

->setStrokeDashArray( [null] );

However this causes an exception. Consider the following code, which sets strokeDashArray, draws a rectangle, tries to reset strokeDashArray and draw another rectangle:

<?php
$fillColour = new \ImagickPixel('white');
$strokeColour = new \ImagickPixel('black');

$imagick = new \Imagick();
$imagick->newImage(500, 500, $fillColour);

$draw = new \ImagickDraw();
$draw->setStrokeDashArray([5]);
$draw->setStrokeColor($strokeColour);

$draw->rectangle(10, 10, 100, 100);

/* Throws exception: Uncaught ImagickException: non-conforming drawing primitive definition `stroke-dasharray' */
$draw->setStrokeDashArray([null]);

$draw->rectangle(30, 30, 300, 300);

$imagick->setImageFormat("png");
$imagick->drawImage($draw);

Trying instead to pass an empty array i.e. setStrokeDashArray([]) results in: ImagickDrawException: Cannot read stroke dash array parameter

Omitting the parameter entirely, or providing null, does not throw an Exception, but causes PHP to issue Warnings because of the function signature being misused.

How are we supposed to reset the strokeDashArray?

I don't know ImageMagick well enough to translate this into a convert command line experiment, to prove that this problem actually exists in the underlying ImageMagick package, and not in this PHP binding, maybe someone can help out if this should be bounced up to that package's github?

neekfenwick avatar Nov 02 '22 07:11 neekfenwick

Well, a bug that is actually my fault and something I can do something about. Which makes a nice change.

It currently isn't possible. I have a fix in c555f5e13158c6b0d56a2cf547695a8f315fafc7 which I will need to do a release for.

Danack avatar Nov 02 '22 12:11 Danack

Well, a bug that is actually my fault and something I can do something about. Which makes a nice change.

Happy to help :P Thank you @Danack !

neekfenwick avatar Nov 03 '22 03:11 neekfenwick