Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Adjusting Image Hue with PHP Imagick

Adjusting Image Hue with PHP Imagick

This guide explains how to install the PHP Imagick extension, create an Imagick object, load an image, adjust its brightness, saturation, and hue using modulateImage, and then save or output the modified image, providing complete example code for hue adjustment.

When developing web applications, you often need to process and adjust images, a common requirement being hue adjustment. In PHP, the Imagick library can be used to achieve hue adjustment. Imagick is a powerful image processing library that can perform scaling, cropping, rotation, filters, etc.

Before starting, you need to install the Imagick extension. You can install it with the following command: sudo apt-get install php-imagick After installing the extension, you can start using Imagick for hue adjustment.

First, create an Imagick object and load the image to be processed. Use the readImage method as follows:

$image = new Imagick();<br/>$image->readImage('path/to/image.jpg');

Next, use modulateImage to adjust the image hue. This method takes three parameters—brightness, saturation, and hue—each ranging from -100% to +100%, where 0% is the original value, negative reduces, and positive increases.

Below is an example that sets the image brightness to 50% of the original: $image->modulateImage(100, 50, 100); After adjusting hue, you need to save the modified image to a file or output it to the browser. Use writeImage to save: $image->writeImage('path/to/new_image.jpg'); Or use the header function and setImageFormat to output to the browser:

header('Content-type: image/jpeg');<br/>$image->setImageFormat('jpeg');<br/>echo $image;

Full example code:

$image = new Imagick();<br/>$image->readImage('path/to/image.jpg');<br/>$image->modulateImage(100, 50, 100);<br/>$image->writeImage('path/to/new_image.jpg');

The above code loads the image, adjusts hue, and saves the modified image.

Using Imagick to adjust image hue is very simple; just a few lines of code. By adjusting brightness, saturation, and hue, you can achieve different effects to meet various needs. Whether processing images or creating special effects, Imagick is a powerful and flexible tool.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
image processingPHPimagickHue Adjustment

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion