Thursday, November 13, 2025
HomeLanguagesPHP | Imagick sparseColorImage() Function

PHP | Imagick sparseColorImage() Function

The Imagick::sparseColorImage() function is an inbuilt function in PHP which is used to interpolate the colors across the whole image.

Syntax:

bool Imagick::sparseColorImage( int $SPARSE_METHOD, array $arguments, int $channel )

Parameters: This function accepts three parameters as mentioned above and described below:

  • $SPARSE_METHOD: It specifies an integer corresponding to one of SPARSECOLORMETHOD constants. List of SPARSECOLORMETHOD constants are given below:
    • imagick::SPARSECOLORMETHOD_UNDEFINED (0)
    • imagick::SPARSECOLORMETHOD_BARYCENTRIC (1)
    • imagick::SPARSECOLORMETHOD_BILINEAR (7)
    • imagick::SPARSECOLORMETHOD_POLYNOMIAL (8)
    • imagick::SPARSECOLORMETHOD_SPEPARDS (16)
    • imagick::SPARSECOLORMETHOD_VORONOI (18)
  • $arguments: It specifies the coordinates.
  • $channel (Optional): It specifies any channel constant that is valid for your channel mode. To apply more than one channel, combine channel constants using bit-wise operators. Its default value is Imagick::CHANNEL_DEFAULT.

Return Value: This function returns TRUE on success.

Below programs illustrate the Imagick::sparseColorImage() function in PHP:

Program 1:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$array = array(0, 0, 1, 0, 0, 1, 900, 0, 0, 1,
               0, 1, 0, 1, 100, 1, 0, 70, 400,
               90, 0, 0, 1, 1);
  
// Apply the sparseColorImage() function
$imagick->sparseColorImage(imagick::SPARSECOLORMETHOD_BILINEAR, $array);
  
// Show the output
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Program 2:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$array = array(0, 0, 1, 0, 0, 1, 78, 0, 0, 1,
               0, 1, 0, 1, 10, 1, 0, 20, 400,
               90, 0, 0, 1, 1);
  
// Apply the sparseColorImage() function
$imagick->sparseColorImage(imagick::SPARSECOLORMETHOD_BARYCENTRIC, $array);
  
// Show the output
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Reference: https://www.php.net/manual/en/imagick.sparsecolorimage.php

RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6836 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS