Imagick::newImage


wizard apg

CREATES a new image and associates ImagickPixel value as background color.



<?php

bool 
public Imagick::newImage(
                                  
int $cols,
                                  
int $rows,
                            
mixed $background,
                            
string $format = ?
                                                  );

?>

$cols


Number of columns in the new image.



$rows


Number of rows in the new image.



$background


The background color used for this image.



$format


The image format.





This function returns true on success.



  1 EXERCISE   

<?php

$image1 
= new Imagick();

$str1img = 'img/results/7newI.jpg';

$image1->newimage(200, 200, new ImagickPixel('green'));

$image1->setimageFormat('jpg');

$image1 = $image1->getimageBlob();
  
$img = imagecreatefromstring($image1);

imagejpeg($img, $str1img);

echo 
basename($str1img); ?>
<br><br>
<img src="<?php echo $str1img; ?>"
 alt="<?php echo $str1img; ?>" 
 title="<?php echo $str1img; ?>">

 RESULT   

7newI.jpg

img/results/7newI.jpg apr

  2 EXERCISE   

<?php

$image2 
= new Imagick();

$str2img = 'img/results/7newL.png';

$image2->newimage(200, 200, 'yellow');

$image2->setimageFormat('png');

$image2 = $image2->getimageBlob();
  
$img = imagecreatefromstring($image2);

imagejpeg($img, $str2img);

echo 
basename($str2img); ?>
<br><br>
<img src="<?php echo $str2img; ?>"
 alt="<?php echo $str2img; ?>" 
 title="<?php echo $str2img; ?>">

7newL.png

img/results/7newL.png apr

  3 EXERCISE   

<?php

// Run several times

$image3 = new Imagick();

$str3img = 'img/results/7newK.jpg';

$cols = mt_rand(1, 300);
$rows = mt_rand(1, 300);

$r = mt_rand(0, 100);
$g = mt_rand(0, 100);
$b = mt_rand(0, 100);

$m = mt_rand(0, 99) /100;

$color = new ImagickPixel("rgba($r%, $g%, $b%, $m)");

$image3->newimage($cols, $rows, $color);

$image3->setimageFormat('jpg');

$image3 = $image3->getimageBlob();
  
$img = imagecreatefromstring($image3);

imagejpeg($img, $str3img);

echo 
basename($str3img); ?>
<br><br>
<img src="<?php echo $str3img; ?>"
 alt="<?php echo $str3img; ?>" 
 title="<?php echo $str3img; ?>">

COLUMNS = 190 ROWS = 141

ImagickPixel("rgba(84%, 12%, 33%, 0.21)

7newK.jpg

img/results/7newK.jpg apr