Adjusting Image Hue with PHP Imagick
This guide explains how to install the PHP Imagick extension and use it 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, it is common to need image processing such as hue adjustment, and the PHP Imagick library offers powerful capabilities for scaling, cropping, rotating, filtering, and more.
First, install the Imagick extension:
sudo apt-get install php-imagickAfter installation, create an Imagick object and load the target image:
$image = new Imagick();
$image->readImage('path/to/image.jpg');Use modulateImage to change brightness, saturation, and hue. The method accepts three parameters (brightness, saturation, hue) with values ranging from -100% to +100%, where 0% leaves the attribute unchanged.
$image->modulateImage(100, 50, 100);Save the modified image to a file:
$image->writeImage('path/to/new_image.jpg');Or output the image directly to the browser:
header('Content-type: image/jpeg');
$image->setImageFormat('jpeg');
echo $image;Complete 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');This script loads an image, adjusts its hue (and optionally brightness and saturation), and saves the result. Using Imagick for hue adjustment is straightforward and requires only a few lines of code, making it a flexible tool for various image‑processing needs.
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.