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.
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:
- fill (Mixed type): This parameter is an ImagickPixel object or a string containing the fill color, the color we require to fill in on the pixel(s) using this function.
- fuzz (Float type): This parameter defines the amount of fuzz. The fuzz value is based on the intensity of the image colors.
- target (Mixed type): This parameter is an ImagickPixel object or a string containing the target color to paint.
- x (Int type): This parameter gives the X-coordinate start position of the floodfill.
- y(Int type): This parameter gives the Y-coordinate start position of the floodfill.
- invert (Mixed type): If set to TRUE paints any pixel that does not match the target color. In short, this works the other way around (hence invert), to fill the remaining pixels apart from the target color. If we want the target color to be filled only, then we would set it to FALSE by default.
- channel (Int type): This parameter is used to provide any channel constant that is valid as per our requirement. If no channel is required, we don’t need to define it (can use the function with the above six parameters only).
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.
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).
- The saved image looks like:
- Then using floodfillPaintImage() function, we color/fill the pixel at (x=1, y=1) position and its neighboring pixels with the same color to green. the pixel at (1, 1) position is colored black (as its the first block) and hence this pixel along with its neighboring black colored pixels (in a chain till the color changes, the filling goes like a ‘flood’ spreading) get filled with our given green color.
The saved image looks like:
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.