Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imagick PHP Extension -- Montage help?

I've been having some trouble generating an image with the Imagick PHP extension. Everything works fine, except my following "montage" has a white background and therefore I cannot overlay it on top of something else. How can I generate a montage with a transparent background?

       $Montage = $Icons->montageImage(new imagickdraw(), "3x2+0+0", "34x34+3+3", imagick::MONTAGEMODE_UNFRAME, "0x0+0+0");
      $Canvas->compositeImage($Montage, $Montage->getImageCompose(), 5, 5);

Thanks!!

like image 986
Nick Avatar asked May 13 '10 21:05

Nick


People also ask

What is imagick PHP extension?

Imagick is a PHP extension to create and modify images using the ImageMagick library. There is also a version of Imagick available for HHVM. Although the two extensions are mostly compatible in their API, and they both call the ImageMagick library, the two extensions are completely separate code-bases.

How do I enable imagick extension in PHP?

To enable Imagick for your website, go to your Site Tools -> Dev -> PHP Manager. Click the PHP Extensions tab and find the entry for the “imagick” extension in the list that appears. Then click the Change value button (pencil icon), select the On radio button for Status and save the changes.

Is imagick and ImageMagick same?

ImageMagick is a PHP utility, a command line tool for image manipulation. For further details, see this. Imagick is an API or a class that performs the function same as ImageMagick. It provides numerous functions for image manipulation in PHP.


1 Answers

I know this is an old question, but I found another way to do this using Imagick's montageImage function. After you create your Imagick object you must declare the background as transparent like this:

$Icons->setBackgroundColor( new ImagickPixel('transparent') );

Once that is set, you can run the object through montageImage which will create a montageImage with a transparent background:

$Montage = $Icons->montageImage(new imagickdraw(), "3x2+0+0", "34x34+3+3", imagick::MONTAGEMODE_UNFRAME, "0x0+0+0");

Then you can add the new montage image to your composite image making sure to use the predefined Imagick composite constant COMPOSITE_ATOP or your desired constant(s) like this:

$Canvas->compositeImage($Montage, imagick::COMPOSITE_ATOP, 5, 5);

Just ran across this question and decided to post another solution in case someone else wants another way to do this without a manual loop.

like image 178
segFault Avatar answered Oct 07 '22 08:10

segFault