10

Is it possible to take this image:

image1

And apply this mask:

mask

And turn it into this:

image2

Using either GD or Imagick? I know it's possible to mask an image using shapes but I'm not sure how to go on about doing it with a pre-created alphatransparent image. :s

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jedi
  • 121
  • 1
  • 1
  • 4
  • I just did some searching and apparently I might be able to use the Wideimage (http://wideimage.sourceforge.net/) library to do this. But is there a simpler way than using a full blown library for just this functionality? – Jedi Nov 16 '09 at 11:10

3 Answers3

16

Using Imagick and ImageMagick version > 6 (I don't know if it will work on older versions):

// Set image path
$path = '/path/to/your/images/';

// Create new objects from png's
$dude = new Imagick($path . 'dude.png');
$mask = new Imagick($path . 'dudemask.png');

// IMPORTANT! Must activate the opacity channel
// See: http://www.php.net/manual/en/function.imagick-setimagematte.php
$dude->setImageMatte(1); 

// Create composite of two images using DSTIN
// See: http://www.imagemagick.org/Usage/compose/#dstin
$dude->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0);

// Write image to a file.
$dude->writeImage($path . 'newimage.png');

// And/or output image directly to browser
header("Content-Type: image/png");
echo $dude;
jspash
  • 191
  • 1
  • 6
1

I think you are looking for imagealphablending. I use it for watermarks, and I believe it will do the effect you are looking for.

Cryophallion
  • 704
  • 5
  • 10
  • 1
    No I don't believe it does. I'm not merging images, I'm using one image to mask an existing image. The result image is alphatransparent. – Jedi Nov 17 '09 at 12:51
  • 3
    Reverse your mask. Make the center trans, and the edges fade to white. Then crop or scale your image to the mask size. This assumes a white background of course. If white isn't workable, are you doing black and white? If so, take an odd color (bright pink or chroma green, both of which may work if you have color as well). Make the true color image and the mask that color, then set that palette color transparent. So, yes, I think it will work, just need to try it from all angles. – Cryophallion Nov 18 '09 at 02:03
0

Great work with (ImageMagick) NOT GD .. I see the tags of this question is GD!!

Here is a GD version at this link: PHP GD Use one image to mask another image, including transparency

Community
  • 1
  • 1
Saud Alfadhli
  • 846
  • 1
  • 8
  • 11