Friday, October 10, 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
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS