PHP ImageMagick - Sharpening & Blurring



Sharpening Imaging

To give a sharper appearance to images, Imagemagick provides an inbuilt function ‘adaptiveSharpenImage()’ which sharpens the images adaptively. It takes an image as input and produces the sharpened image as output.

This method uses algorithms to detect areas in an image where sharpening should be applied, and adjusts the amount of sharpening accordingly. This allows for more natural-looking results than uniform sharpening techniques without sacrificing quality or introducing artifacts into the image.

Syntax

public Imagick::adaptiveSharpenImage(float $radius, float $sigma, int $channel = Imagick::CHANNEL_DEFAULT): bool

This function consists of three parameters which are radius, sigma, and channel.

  • Radius is a float value that specifies the radius of the Gaussian, in pixels, not counting the center pixel.

  • Sigma is a float value that specifies the standard deviation of the Gaussian, in pixels.

  • Channel provides any channel constant that is valid for your channel mode.

Example

To have a clear understanding on how to implement this function, look at the below example. This code creates an imagick object and inputs the image. Then, ‘adaptiveSharpenImage()’ function is applied with the required parameters (radius=19, sigma=15). The output image is obtained in the form ‘adaptiveSharpenImage.png’.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.png");
   $image->adaptiveSharpenImage(19,15);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/adaptiveSharpenImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Sharpening Imaging

Output

Sharpening Imaging

Blurring Images

Blurring an image makes the color transition smooth. By blurring, the rapid changes in the intensity of pixels are averaged. In this chapter, you will be learning about different ways of blurring images with the help of the inbuilt functions provided by Imagemagick.

PHP Image magick library provides a range of powerful functions using which we can perform a variety of operations including blurring, resizing, cropping, and more. With PHP Imagemagick you can easily blur your images with just a few lines of code.

You can also adjust the intensity of the blur as well as several other settings to achieve the desired effect. Whether you're looking for subtle or dramatic changes in your photos, PHP Imagemagick has everything you need to create stunning results!

Adding Blur Filter

To add a blur filter on an image, Imagemagick provided a method named ‘blurImage()’. It takes an image as input and generates/returns the blurred image.

Syntax

public Imagick::blurImage(float $radius, float $sigma, int $channel = ?): bool

This function has 3 parameters: radius, sigma, and channel.

  • Radius is a float value that specifies the radius that needs to be blurred.

  • Sigma is a float value that specifies the standard deviation.

  • channel specifies the channel-type constant. When channel is not specified, all channels are blurred.

Example

In the below example, a new imagick object is created and an image is taken as input. Then, ‘blurImage()’ function is applied to blur the image. The radius and sigma are the parameters specified inside the function (radius=25, sigma=5). The blurred image is obtained as output in the form ‘blurImage.png’.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.png");
   $image->blurImage(25, 5);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/blurImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Adding Blur Filter

Output

Adding Blur Filter

Applying blur by a certain angle

The 'rotationalImageBlur()' function of Imagemagick can be used for image manipulation, allowing users to apply blurring effects on an image at any angle. It accepts the input image and produces a blurred version of the same with the desired degree of blurriness.

Syntax

public Imagick::rotationalBlurImage(float $angle, int $channel =Imagick::CHANNEL_DEFAULT):bool

This function contains two parameters which are angle and channel.

  • Angle is a float value that is used to store the angle.

  • channel is a constant that is valid for your channel mode.

Example

In the below PHP code example, a new imagick object is created and image is taken as input. Then, ‘rotationalBlurImage()’ function is applied to blur the image on a certain angle (15). The blurred image is obtained as output in the form ‘rotationalBlurImage.png’.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.png");
   $image->rotationalBlurImage (15);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/rotationalBlurImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Applying Blur Certain Angle

Output

Applying Blur Certain Angle

Simulating motion blur

The blur that is seen in moving objects in an image is called ‘motion blur’. To simulate motion blur, Imagemagick provided an inbuilt function ‘motionBlurImage()’. It takes an image as input and produces the motion-blurred image as output.

Syntax

public Imagick::motionBlurImage(float $radius, float $sigma, float $angle, int $channel =Imagick::CHANNEL_DEFAULT): bool

This function contains 4 parameters which are radius, sigma, angle, and channel.

  • Radius is a float value that specifies the radius of the Gaussian (in pixels) excluding the center pixel.

  • sigma is a float value that specifies the standard deviation of the Gaussian (in pixels).

  • angle is also a float value which specifies the angle of the blurring motion.

  • Channel is a constant that is valid for your channel mode.

Example

In the example below, a new Imagick object is created and an image is taken as input. The 'motionBlurImage()' function is then applied to blur the image with radius (20), sigma (20) and angle (45) as parameters. As a result, a blurred version of the original image can be obtained in the form of 'motionBlurImage.png'.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.png");
   $image->motionBlurImage(20, 20, 45);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/motionBlurImage.png");
?>

Assume that the following is the input image (image.png) in the program −

Simulating Motion Blur

Output

Simulating Motion Blur
Advertisements