How to add unsharp mask to this code?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
Amin Sabet
Posts: 4
Joined: 2016-11-11T04:56:18-07:00
Authentication code: 1151

How to add unsharp mask to this code?

Post by Amin Sabet »

I am using a forum software called Xenforo, which resizes images using Imagick.

If the code says:

Code: Select all

foreach ($this->_image AS $frame)
			{
				$frame->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9, true);  // keep EXIF
				$frame->setImagePage($frame->getImageWidth(), $frame->getImageHeight(), 0, 0);
			}
and later in the code this:

Code: Select all

if ($scaleUp)
   {
       $frame->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9, true);
   }
   else if ($oldImagick)
   {
       $frame->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9, true);  // keep EXIF
   }
   else
   {
       $frame->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9, true);   // higher-quality scaling + EXIF retained
   }
   $frame->setImagePage($width, $height, 0, 0);
}
How can I add a little unsharp mask to those resize operations?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to add unsharp mask to this code?

Post by Bonzo »

It is interesting that all four resize options do exactly the same thing.

I do not use Imagick but would try adding this line below the $frame->resizeImage( option you are using:

$frame->unsharpMaskImage(0 , 0.5 , 1 , 0.05);
Amin Sabet
Posts: 4
Joined: 2016-11-11T04:56:18-07:00
Authentication code: 1151

Re: How to add unsharp mask to this code?

Post by Amin Sabet »

In the default Xenforo code, the resize options does different things:

Code: Select all

foreach ($this->_image AS $frame)
{
   if ($scaleUp)
   {
      $frame->resizeImage($width, $height, Imagick::FILTER_QUADRATIC, .5, true);
   }
   else if ($oldImagick)
   {
      $frame->thumbnailImage($width, $height, true);
   }
   else
   {
      $frame->thumbnailImage($width, $height, true, true);
   }
   $frame->setImagePage($width, $height, 0, 0);
}
I changed them to do the same thing because I didn't want them to strip out EXIF. I suppose I could have taken out the if / else stuff and just written it once :).

Thanks for your help, I will try what you wrote.
Amin Sabet
Posts: 4
Joined: 2016-11-11T04:56:18-07:00
Authentication code: 1151

Re: How to add unsharp mask to this code?

Post by Amin Sabet »

So this is what I put:

Code: Select all

foreach ($this->_image AS $frame)
			{
				$frame->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9, true);
				$frame->unsharpMaskImage(0 , 0.5 , 1 , 0.05);
				$frame->setImagePage($frame->getImageWidth(), $frame->getImageHeight(), 0, 0);
But as far as I can tell there is no difference in sharpening being applied.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to add unsharp mask to this code?

Post by snibgo »

0.5 is a small amount of unsharpening. A larger number like 5 is more obvious.
snibgo's IM pages: im.snibgo.com
Post Reply