Tuesday, June 16, 2026
HomeLanguagesPHP | ImagickDraw color() Function

PHP | ImagickDraw color() Function

The ImagickDraw::color() function is an inbuilt function in PHP which is used to draw color on the image using the current fill color, starting at the specified position, and using specified paint method.

Syntax:

bool ImagickDraw::color( float $x, float $y, int $paintMethod )

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

  • $x: It specifies the x-coordinate of the paint.
  • $y: It specifies the y-coordinate of the paint.
  • $paintMethod: It specifies an integer corresponding to one of PAINT constants.
    List of PAINT constants are given below:

    • imagick::PAINT_POINT (1)
    • imagick::PAINT_REPLACE (2)
    • imagick::PAINT_FLOODFILL (3)
    • imagick::PAINT_FILLTOBORDER (4)
    • imagick::PAINT_RESET (5)

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickDraw::color() function in PHP:

Program 1:




<?php
  
//Create a new Imagick object
$imagick = new Imagick();
   
// Create a image on imagick object
$imagick->newImage(800, 250, 'white');
   
// Create a new ImagickDraw object
$draw = new ImagickDraw();
   
$x = 0;
while ($x < 900) {
    // Draw lines using imagick::PAINT_POINT
    $draw->color($x, 0, 1);
    $draw->color($x, 30, 1);
    $draw->color($x, 60, 1);
    $draw->color($x, 90, 1);
    $draw->color($x, 120, 1);
    $draw->color($x, 150, 1);
    $draw->color($x, 180, 1);
    $draw->color($x, 210, 1);
    $draw->color($x, 240, 1);
    $x++;
}
   
//  Render the draw commands
$imagick->drawImage($draw);
   
// Show the output
$imagick->setImageFormat("png");
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Program 2:




<?php
  
//Create a new Imagick object
$imagick = new Imagick();
  
// Create a image on imagick object
$imagick->newImage(800, 250, 'white');
  
// Create a new ImagickDraw object
$draw = new ImagickDraw();
  
// Set the fill color
$draw->setFillColor('green');
  
// Color the image using Imagick::PAINTFILL
$draw->color(1, 1, Imagick::PAINT_FLOODFILL);
  
//  Render the draw commands
$imagick->drawImage($draw);
  
// Show the output
$imagick->setImageFormat("png");
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Reference: https://www.php.net/manual/en/imagickdraw.color.php

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS