Imagick::setColor


wizard apg

SETS the color to an image.



<?php

bool 
public ImagickPixel::setColor(string $color);
                                            
?>

$color


The color described by the ImagickPixel object, with a string:

"color-name",
"#XXYYZZ",
rgb(X,Y,Z),
rbga(X%,Y%,Z%,A),
cmyck(X,Y,Z,K)
or other.





This function returns a TRUE on success.



  1 EXERCISE   

<?php

    $str1img 
= 'img/results/10new rc.png';
    
$backgroundColor = 'cyan';
        
    
$draw = new ImagickDraw();
    
$strokeColor = new ImagickPixel('red');
    
$fillColor = new ImagickPixel('blue');

    
$draw->setStrokeColor($strokeColor);
    
$draw->setFillColor($fillColor);
    
$draw->setStrokeOpacity(1);
    
$draw->setStrokeWidth(6.2);
    
    
$draw->rectangle(100, 100, 196, 196);
    
$imagick = new Imagick();
    
$imagick->newImage(200, 200, $backgroundColor);
    
$imagick->setImageFormat("png");

    
$imagick->drawImage($draw);
    
    
$str=$imagick->getImageBlob();
    
    
$img = imagecreatefromstring($str);
    
    
imagepng($img, $str1img);
    
    echo 
basename($str1img); ?>
<br><br>
<img src="<?php echo $str1img; ?>"
 alt="<?php echo $str1img; ?>" 
 title="<?php echo $str1img; ?>">

 RESULT   

10new rc.png

img/results/10new rc.png apr

  2 EXERCISE   

<?php

// Run multiple times

    
$str2img = 'img/results/10new sc.png';
    
    
$red = mt_rand(0, 100);
    
$green = mt_rand(0, 100);
    
$blue = mt_rand(0, 100);
    
$k = mt_rand(0, 100) /100;

    
$draw = new ImagickDraw();

    
$strokeColor = new ImagickPixel('red');
    
$fillColor = new ImagickPixel();
    
$fillColor->setColor("rgba($red%, $green%, $blue%, $k)");
    
    echo 
'setColor(rgba( ' . "$red%, $green%, $blue%, $k" . ' )' . ')<br><br>';

    
$draw->setStrokeWidth(200.0);
    
$draw->setStrokeColor($strokeColor);
    
$draw->setFillColor($fillColor);
    
$draw->rectangle(100, 100, 300, 300);

    
$image = new Imagick();
    
$image->newImage(400, 400, "red");
    
$image->setImageFormat("png");
    
    
$image->drawImage($draw);$draw = new ImagickDraw();

    
$strokeColor = new ImagickPixel('black');
    
$fillColor = new ImagickPixel();
    
$fillColor->setColor("rgba($red%, $green%, $blue%, $k)");

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

    
$image = new Imagick();
    
$image->newImage(400, 400, "green");
    
$image->setImageFormat("png");

    
$image->drawImage($draw);

    
$str=$image->getImageBlob();
    
    
$img = imagecreatefromstring($str);
    
    
imagepng($img, $str2img);
    
    echo 
'<br>' . basename($str2img); ?>
<br><br>
<img src="<?php echo $str2img; ?>"
 alt="<?php echo $str2img; ?>" 
 title="<?php echo $str2img; ?>">
 
 

setColor(rgba( 65%, 11%, 44%, 0.15 ))

10new sc.png ( 50% )

img/results/10new sc.png apr