how to add opacity to image?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
hotzenplotz

how to add opacity to image?

Post by hotzenplotz »

hello,
how can I add opacity to an image? It's important to preserve the transparency in PNG's and GIF's.x
The following seems to do nothing:

$image=new Imagick($file);
$image->setImageType(Imagick::IMGTYPE_TRUECOLORMATTE);
$image->evaluateImage(Imagick::EVALUATE_SUBTRACT, $opacity,
Imagick::CHANNEL_OPACITY);
$image->writeImage($file_new);

and this seems to add opacity, but when opening the picture, I can see only a black square

$image=new Imagick($file);
$image->setImageOpacity(round($opacity/100,1));
$image->writeImage($file_new);

what can I do?

nico
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: how to add opacity to image?

Post by mkoppanen »

Hi,

can i see the image youre processing?
Mikko Koppanen
My blog: http://valokuva.org
hotzenplotz

Re: how to add opacity to image?

Post by hotzenplotz »

Hi,

there was a little error in my description. Right is, that the first statement does nothing and the second adds opacity to complete image, so I can see the partially transparent square on a colored background. For testing I'm using the "Silk Icons" from http://www.famfamfam.com/lab/icons/silk/ (ie female.png).

Nico
hotzenplotz

SOLVED Re: how to add opacity to image?

Post by hotzenplotz »

hi,

$image->evaluateImage(Imagick::EVALUATE_MULTIPLY, 0.3,
Imagick::CHANNEL_OPACITY);

works as expected. now I'm still looking for a solution to display animated gif through PHP API in browser...

thanks
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: how to add opacity to image?

Post by mkoppanen »

you need to send correct headers and use the getImagesBlob method.
Mikko Koppanen
My blog: http://valokuva.org
hotzenplotz

Re: how to add opacity to image?

Post by hotzenplotz »

mkoppanen wrote:you need to send correct headers and use the getImagesBlob method.
header('Content-type: image/gif'); should be right, but the real problem seems to be the gif handling by imagick, see http://imagemagick.org/discourse-server ... =1&t=11579
whatever I do to load the gif file in imagick, only the first frame is rendered...

$image=file_get_contents("image.gif");
header(...);
echo $image;

works, but simple script

$image=file_get_contents("image.gif");
header(...);
$im=new Imagick();
$im->readImageBlob($image);
echo $im->getImageBlob();

shows first frame... :(
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: how to add opacity to image?

Post by mkoppanen »

Can you re-read what I posted? This time with care.
Mikko Koppanen
My blog: http://valokuva.org
hotzenplotz

Re: how to add opacity to image?

Post by hotzenplotz »

I've re-read it for times and noticed, that you wrote getImagesBlob, but when looking into php documentation of imagick, this function call is not documented. alls works perfectly. great thanks.
Post Reply