circularize an image with imagick

前端 未结 4 678
庸人自扰
庸人自扰 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 00:52

    There's also another workaround that I suggest here :

    // create an imagick object of your image
    $image = new \Imagick('/absolute/path/to/your/image');
    // crop square your image from its center (100px witdh/height in my example)
    $image->cropThumbnailImage(100, 100);
    // then round the corners (0.5x the width and height)
    $image->roundCorners(50, 50);
    // force the png format for transparency
    $image->setImageFormat("png");
    // write the new image
    $image->writeImage('/absolute/path/to/your/new/image');
    // done!
    

    Many thanks to all previous answers and contributors that lead me to this code!

    Feel free to test/comment my solution!

提交回复
热议问题