问题
Something to the tune of:
$gradient->newPseudoImage($width, $height, "gradient:transparent-transparent");
Basically I'm looking to create a wet floor effect for uploaded images. So far, it seems you can only create a gradient into a solid color.
回答1:
It turns out if the image you are cloning does not have an alpha channel set you can't create a gradient from an image to a transparent background (at least, I was unable to find a way) What you can do is test for the alpha channel and set it if need be. Here's the working code if you're interested:
$im = new Imagick('image.jpg');
if (!$im->getImageAlphaChannel()) {
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET);
}
$refl = $im->clone();
$refl->flipImage();
$gradient = new Imagick();
$gradient->newPseudoImage($refl->getImageWidth() + 10, $refl->getImageHeight() + 10, "gradient:transparent-black");
$refl->compositeImage($gradient, imagick::COMPOSITE_DSTOUT, 0, 0);
$canvas = new Imagick();
$width = $im->getImageWidth() + 40;
$height = ($im->getImageHeight() * 2) + 30;
$canvas->newImage($width, $height, 'none');
$canvas->setImageFormat('png');
$canvas->compositeImage($im, imagick::COMPOSITE_SRCOVER, 20, 10);
$canvas->compositeImage($refl, imagick::COMPOSITE_SRCOVER, 20, $im->getImageHeight() + 10);
This will take a run of the mill jpg and convert it into a png with a reflection fading into a transparent background.
$canvas->writeImages("new.png", true);
回答2:
The color code for a transparent gradient is "none".
For example, this program saves a gradient from transparent (top) to red (bottom) in a 100x100 pixel PNG image:
$gradient = new Imagick();
$width = 100;
$height = 100;
$gradient->newPseudoImage($width, $height, 'gradient:none-red');
$gradient->setImageFormat('png');
$gradient->writeImage('test.png');
See ImageMagick Canvas documentation for further details on rendering gradients.
来源:https://stackoverflow.com/questions/4727197/can-you-get-a-transparent-gradient-using-php-imagemagick