2017-10-05 85 views
1

我想用透明度將圖像合成到另一個圖像。因此我使用Imagick和PHP。Imagick在使用setImageOpacity和composeImage時丟失了透明度

這就是我得到: imagick result

這就是我想要的: expected result

沒有人有答案,爲什麼$ imageTransparent的透明度得到使用setImageOpacity後丟失()?

這裏是我的代碼:

$image = new \Imagick(); 
$imageTransparent = new \Imagick(); 

$background = new \ImagickPixel('rgba(0,0,255,1)'); 
$image->newImage(600, 400, $background); 

$background = new \ImagickPixel('rgba(255,255,255,0)'); 
$imageTransparent->newImage(600, 400, $background); 

$draw = new \ImagickDraw(); 
$draw->setFillColor('#ff0000'); 
$draw->polygon(array(
    array('x' => 50, 'y' => 50), 
    array('x' => 400, 'y' => 50), 
    array('x' => 50, 'y' => 300), 
)); 

$imageTransparent->drawImage($draw); 
$imageTransparent->setImageOpacity(0.5); 

$image->compositeImage($imageTransparent, \Imagick::COMPOSITE_DEFAULT, 0, 0); 

編輯:我不想設置在DrawObject透明度! $ imageTransparency可以是任何東西。例如一個背景透明的PNG。

我要到解散:http://www.imagemagick.org/Usage/compose/#dissolve

回答

0

我不知道該Imagick很好,但嘗試使用透明圖像上的不透明的藍色背景,然後畫上一個半透明的紅色三角形。 (您將整個輸出圖像設置爲半透明,並在較小的圖像上使用透明白色背景)。注意:我已刪除的行$imageTransparent->setImageOpacity(0.5);

$image = new \Imagick(); 
$imageTransparent = new \Imagick(); 

$background = new \ImagickPixel('rgba(0,0,255,1)'); 
$image->newImage(600, 400, $background); 

$background = new \ImagickPixel('rgba(0,0,255,1)'); 
$imageTransparent->newImage(600, 400, $background); 

$draw = new \ImagickDraw(); 
$draw->setFillColor('rgba(255,0,0,0.5)'); 
$draw->polygon(array(
    array('x' => 50, 'y' => 50), 
    array('x' => 400, 'y' => 50), 
    array('x' => 50, 'y' => 300), 
)); 

$imageTransparent->drawImage($draw); 

$image->compositeImage($imageTransparent, \Imagick::COMPOSITE_DEFAULT, 0, 0); 

以上應該是下面的ImageMagick命令行的等效:

convert \ 
\(-size 600x400 xc:blue \) \ 
\(-size 600x400 xc:blue -fill "rgba(255,0,0,0.5)" \ 
    -draw "polygon 50,50 400,50 50,300" \) \ 
-composite test.png 

將會產生以下結果:

enter image description here

+0

謝謝,但那不是我想要的:$ draw-> setFillColor('rgba(255,0,0,0.5)'); 可以說$ imageTransparent是從PNG加載的圖像;) – luggi

1

在Imagemagick命令行如果你想減少50%的圖像的透明度,你會選擇alpha通道和d乘以0.5,然後再打開所有通道。

這是一個透明的Imagemagick標誌(白色背景製成透明)。

enter image description here

convert \ 
\(-size 600x400 xc:blue \) \ 
\(logot.png -channel a -evaluate multiply 0.5 +channel \) \ 
-composite test2.png 

enter image description here

相比於不改變α通道和剛剛堆肥爲:

convert \ 
\(-size 600x400 xc:blue \) \ 
\(logot.png \) \ 
-composite test1.png 

enter image description here

對不起,我不知道的等效命令在Im agick。但請參閱Imagick :: evaluateImage