HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelTutorial Source codeReport an issue
Category
Example

Set color

← isPixelSimilar   setColorFromPixel →
Sets the color described by the ImagickPixel object, with a string (e.g. "blue", "#0000ff", "rgb(0,0,255)", "cmyk(100,100,100,10)", etc.).
// Example 1
function setColor()
{
    $draw = new \ImagickDraw();

    $strokeColor = new \ImagickPixel('green');
    $fillColor = new \ImagickPixel();
    $fillColor->setColor('rgba(100%, 75%, 0%, 1.0)');

    $draw->setstrokewidth(3.0);
    $draw->setStrokeColor($strokeColor);
    $draw->setFillColor($fillColor);
    $draw->rectangle(200, 200, 300, 300);

    $image = new \Imagick();
    $image->newImage(500, 500, "SteelBlue2");
    $image->setImageFormat("png");

    $image->drawImage($draw);

    header("Content-Type: image/png");
    echo $image->getImageBlob();
}
// Example 2 - FromPixel
function setColorFromPixel()
{
    $image = new \Imagick();
    $draw = new \ImagickDraw();

    $stroke_color = new \ImagickPixel('black');

    $fill_color = new \ImagickPixel('red');
    $blue_color = new \ImagickPixel('blue');

    $fill_color->setColorFromPixel($blue_color);

    $draw->setstrokewidth(1.0);
    $draw->setStrokeColor($stroke_color);
    $draw->setFillColor($fill_color);
    $draw->rectangle(200, 200, 300, 300);

    $image->newImage(500, 500, "gray");
    $image->setImageFormat("png");
    $image->drawImage($draw);

    header("Content-Type: image/png");
    echo $image->getImageBlob();
}
// Example 3 - Value
function setColorValue()
{
    $image = new \Imagick();
    $draw = new \ImagickDraw();

    $color = new \ImagickPixel('blue');
    $color->setcolorValue(\Imagick::COLOR_RED, 128);

    $draw->setstrokewidth(1.0);
    $draw->setStrokeColor($color);
    $draw->setFillColor($color);
    $draw->rectangle(200, 200, 300, 300);

    $image->newImage(500, 500, "SteelBlue2");
    $image->setImageFormat("png");
    $image->drawImage($draw);

    header("Content-Type: image/png");
    echo $image->getImageBlob();
}
// Example 4 - ValueQuantum
function setColorValueQuantum()
{
    $image = new \Imagick();

    $quantumRange = $image->getQuantumRange();

    $draw = new \ImagickDraw();
    $color = new \ImagickPixel('blue');
    $color->setcolorValueQuantum(\Imagick::COLOR_RED, 128 * $quantumRange['quantumRangeLong'] / 256);

    $draw->setstrokewidth(1.0);
    $draw->setStrokeColor($color);
    $draw->setFillColor($color);
    $draw->rectangle(200, 200, 300, 300);

    $image->newImage(500, 500, "SteelBlue2");
    $image->setImageFormat("png");

    $image->drawImage($draw);

    header("Content-Type: image/png");
    echo $image->getImageBlob();
}