Adjusting Image Hue with PHP Imagick
This guide explains how to install the PHP Imagick extension and use its Imagick class to load an image, adjust its brightness, saturation, and hue with modulateImage, and then save or output the modified image, providing complete example code.
When developing web applications, adjusting an image's hue is a common requirement; the PHP Imagick library provides powerful image manipulation capabilities, including scaling, cropping, rotating, and applying filters.
First, install the Imagick extension:
sudo apt-get install php-imagickAfter the extension is installed, you can start using Imagick to modify image tones.
Create an Imagick object and load the target image:
$image = new Imagick();
$image->readImage('path/to/image.jpg');Use the modulateImage method to adjust brightness, saturation, and hue. The method accepts three parameters (brightness, saturation, hue) ranging from -100% to +100%, where 0% leaves the value unchanged.
For example, to set the brightness to 50% of the original:
$image->modulateImage(100, 50, 100);After adjusting the tone, save the modified image to a file:
$image->writeImage('path/to/new_image.jpg');Alternatively, output the image directly to the browser:
header('Content-type: image/jpeg');
$image->setImageFormat('jpeg');
echo $image;Full example code combining the steps above:
$image = new Imagick();
$image->readImage('path/to/image.jpg');
$image->modulateImage(100, 50, 100);
$image->writeImage('path/to/new_image.jpg');The code loads an image, adjusts its hue (brightness, saturation, hue), and saves the result.
Using Imagick to adjust image tones is straightforward and requires only a few lines of code; by tweaking brightness, saturation, and hue you can achieve a wide range of visual effects, making Imagick a flexible tool for image processing tasks.
Java learning material download
C language learning material download
Frontend learning material download
C++ learning material download
PHP learning material download
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.