image icon indicating copy to clipboard operation
image copied to clipboard

Is there an option or way to use anti-aliasing when creating circles/masks?

Open samwalshnz opened this issue 12 years ago • 8 comments

I'm trying to crop a square image to be a circle, but if I create a circle image with background #fff and use it to mask an existing image, the circle's edges are very jagged.

<?php


namespace App\Image\Effects;


use Intervention\Image\Image;

class CropCircle implements Effect{

    /**
     * @param Image $image
     * @param array $options
     * @return mixed
     */
    public function apply(Image $image, array $options = [ ])
    {
        $mask = $this->makeCircleMaskImage($image->width(), $image->height());

        $image = $image->mask($mask);

        return $image;
    }

    /**
     * @param int $width
     * @param int $height
     * @return Image
     */
    private function makeCircleMaskImage($width, $height)
    {
        $circle = \Image::canvas($width, $height, '#000000');

        $circle = $circle->circle($width - 1, $width / 2, $height / 2, function ($draw)
        {
            $draw->background('#ffffff');
        });

        return $circle;
    }
}

missing

samwalshnz avatar Sep 04 '14 23:09 samwalshnz

Here's a horrible hack I've just done in the mean time. But the more image manipulations I do, the slower it will get. In this one, I create a circle twice the size, then resize it to the normal width/height which will obviously using scaling algorithms to recalculate the pixels, thus effectively kinda anti-aliasing the circle.


<?php


namespace App\Image\Effects;


use Intervention\Image\Image;

class CropCircle implements Effect{

    /**
     * @param Image $image
     * @param array $options
     * @return mixed
     */
    public function apply(Image $image, array $options = [ ])
    {
        $mask = $this->makeCircleMaskImage($image->width(), $image->height());

        $image = $image->mask($mask);

        return $image;
    }

    /**
     * @param int $width
     * @param int $height
     * @return Image
     */
    private function makeCircleMaskImage($width, $height)
    {
        $bigWidth = $width * 2;
        $bigHeight = $height * 2;

        $circle = \Image::canvas($bigWidth, $bigHeight, '#000000');

        $circle = $circle->circle($bigWidth - 1, $bigWidth / 2, $bigHeight / 2, function ($draw)
        {
            $draw->background('#ffffff');
        });

        $circle = $circle->resize($width, $height);

        return $circle;
    }
}

missing-1

samwalshnz avatar Sep 04 '14 23:09 samwalshnz

As far as I know, GD does not support AA. I recommend using Imagick.

But your hack looks interesting. I will take a look at this.

olivervogel avatar Sep 05 '14 14:09 olivervogel

The resizing-hack is interesting but it will mess up possible border-sizes, when sizing down.

I recommend using Imagick for proper AA.

olivervogel avatar Oct 15 '14 14:10 olivervogel

Awesome. K thanks for the tip

samwalshnz avatar Oct 16 '14 00:10 samwalshnz

Thank you @samwalshnz <3

intransitorio avatar Jun 16 '16 16:06 intransitorio

@olivervogel how to enable AA, when using Imagick?

badalsurana avatar Jan 30 '18 17:01 badalsurana

Thank you! @samwalshnz and all the friends!

WillingAC avatar Jul 26 '18 03:07 WillingAC

Thanks, @samwalshnz

mitulvc avatar Sep 15 '20 13:09 mitulvc