Adjust Image Hue Using PHP Imagick
This guide explains how to install the Imagick extension for PHP, create an Imagick object, load an image, adjust its brightness, saturation, and hue using modulateImage, and then save or output the modified image, providing a complete example code snippet.
When developing web applications, you often need to process and adjust images, such as changing their hue. In PHP, the Imagick extension provides powerful image‑manipulation capabilities, including scaling, cropping, rotating, and applying filters.
First, install the Imagick extension:
sudo apt-get install php-imagickAfter installation, you can create an Imagick object and load an image:
$image = new Imagick();
$image->readImage('path/to/image.jpg');Use modulateImage 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 image brightness to 50% of the original:
$image->modulateImage(100, 50, 100);Save the modified image with writeImage or output it directly to the browser.
$image->writeImage('path/to/new_image.jpg');Or send it to the browser:
header('Content-type: image/jpeg');
$image->setImageFormat('jpeg');
echo $image;Complete example:
$image = new Imagick();
$image->readImage('path/to/image.jpg');
$image->modulateImage(100, 50, 100);
$image->writeImage('path/to/new_image.jpg');This code loads an image, adjusts its hue, and saves the result. Using Imagick, you can quickly modify brightness, saturation, and hue with just a few lines of PHP.
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.