Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imagesetthickness() in DrawLineModifier has no effect in certain environments #1298

Closed
ulfie22 opened this issue Feb 8, 2024 · 6 comments
Closed

Comments

@ulfie22
Copy link

ulfie22 commented Feb 8, 2024

Describe the bug
The width parameter for the drawLine method was implemented in v 3.3.3. It worked correctly upon upgrade. For reasons unknown, it has stopped working and all drawn lines are 1 px, no matter what the width parameter is. This is so effing weird!

Code Example

public function drawLine($fromX, $fromY, $toX, $toY, $color, $width): void
    {
        ray($width)->blue();
        $this->image->drawLine(function ($line) use ($fromX, $fromY, $toX, $toY, $color, $width) {
            $line->from($fromX, $fromY);      // starting point of line
            $line->to($toX, $toY);            // ending point
            $line->color($color);             // color of line
            $line->width($width);             // line width in pixels
        });
    }

Expected behavior
In the above example, $width was set to 4 (and verified by ray()) - image produced has a 1 px line

Images
CleanShot 2024-02-07 at 18 33 25@2x

Environment (please complete the following information):

  • PHP Version: 8.3
  • OS: MacOS Sonoma 14.3
  • Intervention Image Version: 3.3.3
  • GD or Imagick: GD
@olivervogel
Copy link
Member

That's really strange. Unfortunately, I can't reproduce it right now.

According to a very old comment in the PHP documentation, the function imagesetthickness() does not work if imageantialias() is activated. I can't say whether this is still relevant (my tests show that it works with both options). But maybe you can comment out the antialias line in the vendor code and see if it has an influence.

@ulfie22
Copy link
Author

ulfie22 commented Feb 8, 2024

@olivervogel Thanks for this response. THAT ACTUALLY FIXES THE PROBLEM! Is there a way for me to override this one function? I tried to just override that one class, but it's buried so deep inside Image Intervention that my override was not being recognized. Thank you for your help!

1 similar comment
@ulfie22
Copy link
Author

ulfie22 commented Feb 8, 2024

@olivervogel Thanks for this response. THAT ACTUALLY FIXES THE PROBLEM! Is there a way for me to override this one function? I tried to just override that one class, but it's buried so deep inside Image Intervention that my override was not being recognized. Thank you for your help!

@olivervogel
Copy link
Member

olivervogel commented Feb 9, 2024

On the one hand, this is good news. On the other hand, it is also bad news, as the current implementation does not seem to work on certain systems. What particularly surprises me is that I can't detect any problems and I'm even using the same setup (MacOS, PHP 8.3.2, GD). And it is not really clear what triggers the behavior and how to handle this.

As a quick solution for the moment, I would suggest to use a custom modifier that replicates the function of the DrawLineModifer without antialiasing.

It could look like this:

use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\ImageInterface;

class DrawPixelLine implements ModifierInterface
{
    public function __construct(
        protected int $fromX,
        protected int $fromY,
        protected int $toX,
        protected int $toY,
        protected mixed $color,
        protected int $width
    ) {
    }

    public function apply(ImageInterface $image): ImageInterface
    {
        $lineColor = $image->driver()->colorProcessor($image->colorspace())->colorToNative(
            $image->driver()->handleInput($this->color)
        );

        foreach ($image as $frame) {
            imagealphablending($frame->native(), true);
            imagesetthickness($frame->native(), $this->width);
            imageline(
                $frame->native(),
                $this->fromX,
                $this->fromY,
                $this->toX,
                $this->toY,
                $lineColor
            );
        }

        return $image;
    }
}

// apply modifier
$image->modify(new DrawPixelLine(10, 20, 130, 200, 'ff00ff', 3));

I'm sorry that I can't offer you a better solution at the moment.

@olivervogel olivervogel changed the title "width" somehow stopped working imagesetthickness() in DrawLineModifier has no effect in certain environments Feb 9, 2024
@olivervogel
Copy link
Member

Here are the specs of my local GD installation for comparison. Maybe there are differences that you can recognize.

Bildschirmfoto 2024-02-09 um 14 56 05

@ulfie22
Copy link
Author

ulfie22 commented Feb 9, 2024

@olivervogel wow - thank you for all the great information. After some further research on my end, I am pretty sure this has something to do with my system, not your code. One of my co-workers pulled my code to his machine (actually a windows box running Sail) and everything worked as expected. I've been trying to get Laravel Herd to work successfully so my guess is something in that process caused this issue. I'm going to say thank you (big time!) and close this - I don't think you need to spend any more time on it unless someone else encounters the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants