Page 1 of 1

Imagick: fuzzy edges on transparent PNG?

Posted: Wed Nov 10, 2010 7:32 am
by Fejstan
I've made a script that creates thumbnails on-the-fly from uploaded images. The thumbs must have a reflection-effect so I'm using Imagick to create a PNG for this. The problem is that I want the reflection to be blurry because it just looks better. Now, I use gaussianBlurImage()-function to do this but since the new image is created on a transparent image the edges does NOT become blurry (fuzzy). I want the edges to be fuzzy as well! If I use a colored background it works, but NOT on a transparent one.

I've tried several different approaches to achieve this (manipulate the alpha-channel etc) but nothing works... Now, is there ANYONE out there who knows how to do this???

This is the code:

Code: Select all

// Create image from uploaded and resize it
$uploaded = new Imagick($_FILES["file"]["tmp_name"]);
$uploaded->scaleImage(260, 208, false);

// Create reflection
$reflection = $uploaded->clone();
$reflection->flipImage();
$reflection->setImageOpacity(0.5);

// Create new image
$image = new Imagick();
$image->newImage(300, 254, new ImagickPixel("none"));
$image->setImageFormat('png');

// Add together
$image->compositeImage($reflection, Imagick::COMPOSITE_DEFAULT, 20, 208);
$image->gaussianBlurImage(3,3);
$image->compositeImage($uploaded, Imagick::COMPOSITE_DEFAULT, 20, 0);

// Save new image
$data = $image->getImageBlob();
file_put_contents ($real_dir.$_FILES["file"]["name"], $data); 

Re: Imagick: fuzzy edges on transparent PNG?

Posted: Wed Nov 10, 2010 10:59 am
by requinix
Wouldn't it be easier to blur the reflection before merging it with the original?

Re: Imagick: fuzzy edges on transparent PNG?

Posted: Wed Nov 10, 2010 11:30 am
by Fejstan
That was my first approach but it didn't work either. But that's obvious, if I want the edge to be fuzzy there must be pixles outside of the edge of the reflection so that the edge can "smudge" outside of itself... So that's why I first add the reflection to a bigger image area and then apply blur.

Re: Imagick: fuzzy edges on transparent PNG?

Posted: Wed Nov 10, 2010 11:45 am
by requinix
Try adding a call to setImageVirtualPixelMethod():

Code: Select all

$image->compositeImage($reflection, Imagick::COMPOSITE_DEFAULT, 20, 208);
$image->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_BACKGROUND);
$image->gaussianBlurImage(3,3);
$image->compositeImage($uploaded, Imagick::COMPOSITE_DEFAULT, 20, 0);

Re: Imagick: fuzzy edges on transparent PNG?

Posted: Fri Nov 12, 2010 6:56 am
by Fejstan
Nope, didn't work... but thanx anyway for the tip

Re: Imagick: fuzzy edges on transparent PNG?

Posted: Fri Nov 12, 2010 12:37 pm
by requinix
Oh, it's those edges that are the problem.

I'm not sure. ImageMagick can't blur into transparency (either it doesn't work or you get black) so I think you'll need some series of actions to... well, I'm not sure how you can do it. Someone with Photoshop et al. experience would probably know - if you can convert that into Imagick functions then you should be set.

Re: Imagick: fuzzy edges on transparent PNG?

Posted: Mon Nov 22, 2010 10:19 am
by Fejstan
Ye, well I've tried everything now, so I guess the only way is to use alpha-channel and layers and stuff and do it in several steps.

Thanx anyway for the help.