imagemagic官方去除图片背景的命令行模式:
convert cyclops.png -bordercolor white -border 1x1 -matte -fill none -fuzz 20% -draw 'matte 0,0 floodfill' -shave 1x1 cyclops_flood_3.png
imagick代码:
<?php
$image = new Imagick('1.png');
$image->borderImage(new ImagickPixel("white"),1,1);
$image->paintfloodfillimage('transparent',2000,NULL,0,0);
$draw = new ImagickDraw();
$draw->color(0,0,imagick::PAINT_FLOODFILL);
$image->drawImage($draw);
$image->shaveImage(1,1);
header("Content-Type: image/{$image->getImageFormat()}");
echo $image->getImageBlob( );
//$image->writeImage('o.png');
$image->clear();
$image->destroy();
?>
源图片:
效果图: