Applying opacity when flattening to JPEG?

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
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Applying opacity when flattening to JPEG?

Post by pwnedd »

Suppose you have an image with an alpha channel and the opacity is set to 50%. Does anyone know how you can "flatten" the image to JPEG so that the image appears to be 50% transparent on a black background?

Example:

Code: Select all

    $im = new Imagick("50percent_opacity.png");
    $im->setImageFormat("JPEG");
    header("Content-type: image/jpg");
    echo $im;
The result then is a JPEG image with a black background and a 100% opaque copy of the image on top.

What I want instead is a JPEG image with a black background and a half-transparent version of the original image on top, similar to how GIMP/Photoshop would flatten the image with a black background.

Any suggestions?

Thanks,
Keith
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Applying opacity when flattening to JPEG?

Post by fmw42 »

see flattenImage and setBackground at http://us2.php.net/imagick
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Applying opacity when flattening to JPEG?

Post by pwnedd »

Hi Fred,

Thanks for the suggestion. I've tried using both of those methods, however, with no luck., e.g.:

Generate a 50% opaque image:

Code: Select all

convert logo: -type PaletteMatte -channel Alpha -evaluate Divide 2 input.png
Or, using IMagick:

Code: Select all

$logo = new Imagick("logo:");
$logo->setImageType(imagick::IMGTYPE_PALETTEMATTE);
$logo->setImageOpacity(0.5);
Example input (50% opaque image)
Example input (50% opaque image)
input.png (102.26 KiB) Viewed 16695 times
Next, attempt to apply black background and flatten:

Code: Select all

$logo = new Imagick("input.png");
$logo->setBackgroundColor("black");
$logo->flattenImages();
$logo->setImageFormat("jpg");
$logo->writeImage("actual_result.jpg");
Result (Actual):
Actual result
Actual result
actual_output.jpg (42.67 KiB) Viewed 16695 times
Whereas the expected/desired result should look like:
Desired result
Desired result
desired_output.jpg (23.86 KiB) Viewed 16695 times
Know what I'm missing here? How can I can from that input, to the desired output?

Thanks,
Keith
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Applying opacity when flattening to JPEG?

Post by fmw42 »

Code: Select all

$logo = new Imagick("input.png");
$logo->setBackgroundColor("black");
$logo->flattenImages();
$logo->setImageFormat("jpg");
$logo->writeImage("actual_result.jpg");
I have only dabbled with iMagick and so am not an expert. But your code appears reasonable to me. You might try setting the imageFormat prior to flattenImages, but it may not make a difference.

Are you using the latest iMagick and the latest IM? Imagick is at 3.x and Imagemagick is at 6.6.5-10
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Applying opacity when flattening to JPEG?

Post by pwnedd »

Switching the order doesn't help either :\

I'm using pretty recent versions of both (ImageMagick 6.5.7-8 and IMagick 3.0.0).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Applying opacity when flattening to JPEG?

Post by fmw42 »

pwnedd wrote:Switching the order doesn't help either :\

I'm using pretty recent versions of both (ImageMagick 6.5.7-8 and IMagick 3.0.0).
Imagick is at 3.1x and ImageMagick is at 6.6.5-10. But I don''t know if that will help. Perhaps some PHP expert can help further. You might contact Bonzo, if he does not respond to this topic.
Post Reply