Adjust Image Hue in PHP with Imagick: A Step‑by‑Step Guide
This tutorial explains how to install the Imagick extension, create an Imagick object in PHP, use the modulateImage method to adjust brightness, saturation, and hue, and then save or output the modified image, providing complete example code for each step.
When developing web applications, adjusting an image's hue is a common requirement; the PHP Imagick extension offers powerful image‑processing capabilities such as scaling, cropping, rotating, and filtering.
Installation sudo apt-get install php-imagick After installing the extension, you can start using Imagick to modify image tones.
Loading an image
$image = new Imagick();
$image->readImage('path/to/image.jpg');Adjusting hue, saturation, and brightness
The modulateImage method accepts three parameters: brightness , saturation , and hue . Each value ranges from –100% to +100%, where 0% leaves the original unchanged.
Example: set brightness to 50% of the original. $image->modulateImage(100, 50, 100); Saving or outputting the result
To write the modified image to a file: $image->writeImage('path/to/new_image.jpg'); Or send it directly 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 script loads an image, reduces its saturation to 50%, and saves the adjusted image. Using Imagick for hue adjustments requires only a few lines of code, making it a flexible tool for various image‑processing needs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
