Open In App

PHP | Imagick floodFillPaintImage() Function

The Imagick floodFillPaintImage() Function is an in-built function in PHP which is used to change the color value of any pixel that matches the target, along with its immediate neighbor pixels of the same color.

Note: This method is a replacement for deprecated Imagick::paintFloodFillImage() Function and is available if Imagick has been compiled against ImageMagick version 6.3.8 or above.



Syntax:

bool Imagick::floodFillPaintImage 
( mixed $fill, float $fuzz, mixed $target, int $x, int $y, bool $invert 
[, int $channel = Imagick::CHANNEL_DEFAULT ] )  

Parameters: The function has seven parameters as we can see from the syntax above. They are:



Note: In context to ‘Mixed type’ here, please note that the ‘Mixed’ keyword in PHP is not a primitive type and it cannot be used in programming. The mixed type is used here to convey that the type can be of any type.

Return Value: The function returns True on success.

Below example illustrate the Imagick::floodFillPaintImage() Function in PHP:

Example: Let us consider two blocks, one black and one green (of 190×90 dimension):


Now, we will write a PHP program that illustrates the Imagick::floodFillPaintImage() function.

  • Program:




    <?php
       
     // Create new imagick objects for the blocks under one name: 
     $imagick = new Imagick(
    'https://media.geeksforgeeks.org/wp-content/uploads/20190720123909/black1.png"
         alt=""
         width="190" 
         height="90" 
         class="alignnone 
         size-full wp-image-1167659');
     $imagick = new Imagick(
     'https://media.geeksforgeeks.org/wp-content/uploads/20190720123923/green3.png"
         alt=""
         width="190"
         height="90"
         class="alignnone
         size-full wp-image-1167665');
                      
     // Append the images into one:
     $imagick->resetIterator();
     $combined = $imagick->appendImages(true);
       
     // Save the image for comparison:
     $combined->writeImage("blackgreenobjects.png");
       
     // Set (x, y) values for the target pixel to paint:
     $x = 1;
     $y = 1;
       
     // Get the color we are painting:
     $target = $combined->getImagePixelColor($x, $y);
       
     // Paints pixel in position (1, 1) and all neighboring pixels 
    //that match the target color (black) to green color:
     $combined->floodfillPaintImage("green", 1, $target, $x, $y, false);
       
     // Save the resulting image:
     $combined->writeImage("greengreenfill.png"); 
       
     // Display resulting image:
     echo $combined;                   
       
    ?>
    
    
  • Output: This code would first create two Imagick objects under one name, a black and a green block and then append them into one (consecutive objects get appended below the first object).

    Note: if you use the function again with the target color being green, both of our initially defined blocks would change to the fill color as because both of them have the same color now.


    Article Tags :