How to Adjust Image Hue in PHP with Imagick – A Step‑by‑Step Guide
This tutorial shows how to install the Imagick extension, load an image in PHP, modify its brightness, saturation, and hue using the modulateImage method, and then either save the result to a file or stream it directly to the browser.
When building web applications, you often need to modify image tones; the PHP Imagick library provides a powerful way to do this.
Installation : First install the Imagick extension on your server: sudo apt-get install php-imagick Loading an image : Create an Imagick instance and read the target file:
$image = new Imagick();
$image->readImage('path/to/image.jpg');Adjusting hue, saturation, and brightness : Use modulateImage with three parameters (brightness, saturation, hue). Values range from -100% to +100%, where 0% leaves the attribute unchanged. For example, to reduce saturation to 50% while keeping brightness and hue unchanged: $image->modulateImage(100, 50, 100); Saving the modified image : Write the result back to disk: $image->writeImage('path/to/new_image.jpg'); Streaming to the browser : Alternatively, output the image directly:
header('Content-type: image/jpeg');
$image->setImageFormat('jpeg');
echo $image;Complete example combining all steps:
$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, reduces its saturation by half, and saves the adjusted file. Imagick makes tone adjustments straightforward with just a few lines of PHP.
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.
