Imagick: fuzzy edges on transparent PNG?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Fejstan
Forum Newbie
Posts: 4
Joined: Wed Nov 10, 2010 7:19 am

Imagick: fuzzy edges on transparent PNG?

Post 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); 
Last edited by Weirdan on Wed Nov 10, 2010 7:45 am, edited 1 time in total.
Reason: added syntax highlighting
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Imagick: fuzzy edges on transparent PNG?

Post by requinix »

Wouldn't it be easier to blur the reflection before merging it with the original?
Fejstan
Forum Newbie
Posts: 4
Joined: Wed Nov 10, 2010 7:19 am

Re: Imagick: fuzzy edges on transparent PNG?

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Imagick: fuzzy edges on transparent PNG?

Post 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);
Fejstan
Forum Newbie
Posts: 4
Joined: Wed Nov 10, 2010 7:19 am

Re: Imagick: fuzzy edges on transparent PNG?

Post by Fejstan »

Nope, didn't work... but thanx anyway for the tip
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Imagick: fuzzy edges on transparent PNG?

Post 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.
Fejstan
Forum Newbie
Posts: 4
Joined: Wed Nov 10, 2010 7:19 am

Re: Imagick: fuzzy edges on transparent PNG?

Post 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.
Post Reply