circularize an image with imagick

前端 未结 4 679
庸人自扰
庸人自扰 2020-12-10 00:33

Trying to take a rectangular photo, crop it into a square region, and then mask it into a circular with a transparent background.

//$dims is an array with th         


        
4条回答
  •  再見小時候
    2020-12-10 01:07

    For those with an older version of Imagick (setimagematte does not exist in version lower than 6.2.9), I came up with an easy solution. The thing here is to copy opacity from the mask to the original image.

    Original Image:

    enter image description here

    Mask:

    enter image description here

    Result:

    enter image description here

    The code:

    $base = new Imagick('original.jpg');
    $mask = new Imagick('mask.png');
    
    $base->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
    $base->writeImage('result.png');
    

    You could use an Imagick black circle as mask but I though it wasn't perfect so I used my own.

    Of course you will certainly have to resize / crop your images but that's another story.

    Hope this helps.

    J.

提交回复
热议问题