2012-09-15 79 views

回答

2

我希望這個代碼將有所幫助。

$mask = new Imagick(); 
$mask->newimage(500, 500, new ImagickPixel('transparent')); 
$mask->setimageformat('png'); 
$polygon = new ImagickDraw(); 
$polygon->setFillColor(new ImagickPixel('black')); 
/* 
    You didn't specified from where to start the crop and where to end it, 
    so just assumed those values 
*/ 
$polygon->polygon(array(
    array('x' => 200, 'y' => 0), 
    array('x' => 400, 'y' => 0), 
    array('x' => 450, 'y' => 500), 
    array('x' => 250, 'y' => 500), 
    array('x' => 200, 'y' => 0), 
)); 
$mask->drawimage($polygon); 
$image = new Imagick(); 
$image->readimage("Your source image path :) "); 
$image->setImageFormat('png'); 
$image->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT); 
$image->setImageMatte(true); 
$image->compositeimage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA); 
$image->trimimage(0); 
header('Content-Type: image/png'); 
echo $image; 
die(); 
+0

任何想法如何保持圖像的透明度? –