PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 19, 2022

[FIXED] How can I draw a dashed line in PHP ImageMagick?

 September 19, 2022     drawing, imagick, php     No comments   

Issue

I'm trying to draw a dashed line using PHP Imagick. This code produces a solid line:

$line = new ImagickDraw();
$line->setStrokeWidth(3);
$line->setStrokeDashArray([10, 10]);
$line->line(0, 0, 100, 100);

setStrokeDashArray() seems to work for outlines on ImagickDraw::rectangle() but not ImagickDraw::line() drawings. Is there any way to draw simple dashed lines?


Solution

To get a nice dashed line without a solid line inside of it, set the fill color to opacity of zero (the actual color choice doesn't matter so long as the opacity value is a 0), and then don't forget to set a stroke color.

A working example (with an added debugging to browser dump):

$line = new ImagickDraw();
$line->setStrokeColor('rgb(0, 0, 0)');
$line->setFillColor('rgba(255, 255, 255, 0)');
$line->setStrokeWidth(3);
$line->setStrokeDashArray([10, 10]);
$line->line(0, 0, 100, 100);

// for debugging, output to browser:
$image = new Imagick();
$image->newImage(200, 200, 'rgb(230, 230, 230)');
$image->setImageFormat("png");
$image->drawImage($line);
header("Content-Type: image/png");
echo $image->getImageBlob();
exit;

Debug output result:

Debug Output Example



Answered By - IncredibleHat
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,214,189

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing