Thursday, October 2, 2025
HomeLanguagesPHP | Imagick averageImages() Function

PHP | Imagick averageImages() Function

The Imagick::averageImages() function is an inbuilt function in PHP which is used to create an average of two or more images after image processing. It is well defined in PECL imagick 2.0.0 version. This function has been depreciated in further versions of Imagick hence it is replaced by Imagick::mergeImageLayers() in PECL imagick 2.1.0 version. Syntax:

Imagick Imagick::averageImages( void )

Return Value: This function returns a new imagick object on success. Errors/Exceptions: It throws ImagickException while error occurred. Below program illustrates the Imagick::averageImages() function in PHP: Program: 

php




<?php
 
// Store the path of an image into variable
$imagePath =
"https://media.geeksforgeeks.org/wp-content/uploads/20190912230402/hexgon_compositeImage.png";
 
// Create a new Imagick object
$imagick = new \Imagick($imagePath);
 
// Create a new ImagickDraw object
$draw = new \ImagickDraw();
 
// Draw an image and set its font size
$draw->setFontSize(35);
 
// Set the color of text
$fillcolor = new \ImagickPixel("red");
 
// Set the fill color
$draw->setFillColor($fillcolor);
 
// Set the gravity
$draw->setGravity(Imagick::GRAVITY_CENTER);
 
// Annotate the text "neveropen" to the image
$imagick->annotateImage( $draw, 0, 0, 0, "neveropen");
 
// Assign thresholdcolor to the image
$thresholdColor = "rgb(255, 0, 0)";
 
$imagick->blackthresholdimage($thresholdColor);
 
// Flop the image
$imagick->flopImage();
 
// Turn image to oil paint image
$imagick->oilPaintImage(1);
$imagick->resetIterator();
 
// Merging all image layers and flattened
$imagick->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
 
// It works in PECL imagick 2.0.0 version,
// comment it when higher version.
$imagick->averageImages();
 
header("Content-Type: image/png");
header("Cache-Control: no-store, no-cache"); 
 
// Downloading output image of
// Imagick::averageImage() function
header('Content-Disposition: attachment; filename="average.png"');
 
echo $imagick->getImageBlob();
 
?>


Output: Reference: https://www.php.net/manual/en/imagick.averageimages.php

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7079 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS