Imagick / imagick

🌈 The Imagick PHP extension 🌈
http://pecl.php.net/imagick
Other
548 stars 138 forks source link

Add setImageMask #120

Open isage opened 9 years ago

isage commented 9 years ago

Add setImageMask to allow masked compose operations. (convert input.img overlay.img mask.img -compose ...) Sadly, there's no wand api for it, but it can be easily emulated with:

SetImageMask(GetImageFromMagickWand(img_wand), GetImageFromMagickWand(mask_wand)
Danack commented 9 years ago

I'll think about it - it's something that would be useful, as the current way of compositing stuff is a 'bit' annoying to use, as it's just not powerful enough.

Can you propose the set of method calls and their parameters that would be needed?

isage commented 9 years ago

As an example, something like:

$base = new Imagick('base.png');
$mask = new Imagick('mask.png');
$over = new Imagick('overlay.png');

$base->setMask($mask);
$base->compositeImage($over, Imagick::COMPOSITE_DEFAULT, 0, 0);
$base->setMask(NULL);
$base->writeImage('output.png');

It's a modified http://stackoverflow.com/a/8725843 without all that alpha magick.

Or, tinting only dark parts of image (see colortone here: http://code.tutsplus.com/tutorials/create-instagram-filters-with-php--net-24504)

$base = new Imagick('base.png');

$mask = clone $base;
$mask->setImageColorspace (imagick::COLORSPACE_GRAY);
$mask->negateImage(false);

$overlay = new Imagick();
list($w,$h) = $base->getSize();
$overlay->newPseudoImage($w, $h, "xc:rgb(50,0,0)");

$base->setImageArtifact("compose:args", "100,0");
$overlay->setImageArtifact("compose:args", "100,0");

$base->setImageMask($mask);
$base->compositeImage($over, Imagick::COMPOSITE_BLEND, 0, 0);
$base->setImageMask(NULL);
$base->writeImage('output.png');

So, basically, just setImageMask method, which should accept Imagick object or NULL (to reset mask).

PetersOtto commented 7 months ago

Hello @isage!

I spent the whole day yesterday, trying to recreate the Instagram Nashville filter. Unfortunately, it didn't really work. Then today I found this old post. Year happiness 😀 Unfortunately I get an error message: Uncaught Error: Call to undefined method Imagick::setImageMask() ...

Can you perhaps help me here? I only found the error $base->compositeImage($over, Imagick::COMPOSITE_BLEND, 0, 0); I think it must be $overlay instead of $over. However, the error message still remains.

EDIT: Or will that only work with png and not with jpg?

isage commented 7 months ago

Of course it throws error, because it wasn't implemented

PetersOtto commented 7 months ago

Oh how embarrassing, I didn't read that correctly...

PetersOtto commented 7 months ago

@isage, have you found an alternative way to implement function colortone from the tutorial?