I have two functions that generate random patterns of different types, but am struggling to color both the black and white sections different colors.
Code: Select all
<?php
function generate_first_pattern($base_color, $pattern_color)
{
// create the base
$base = new \Imagick();
$base->newPseudoImage(500, 500, 'plasma:fractal');
// create the pattern on the base
$base->thresholdImage(0.5 * \Imagick::getQuantum());
$base->blurImage(0, 1);
// clone
$pattern = clone $base;
// base
$base->setImageBackgroundColor($base_color);
$base->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);
// pattern
$pattern->setImageBackgroundColor($pattern_color);
$pattern->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);
// layer
$base->compositeImage($pattern, \Imagick::COMPOSITE_DEFAULT, 0, 0);
// set format to png
$pattern->setImageFormat('png');
header('Content-Type: image/png');
echo $pattern->getImageBlob();
}
function generate_second_pattern($base_color, $pattern_color)
{
// create the base
$base = new \Imagick();
$base->newImage(500, 500, new \ImagickPixel('white'));
$base->setImageVirtualPixelMethod(\Imagick::VIRTUALPIXELMETHOD_TILE);
// first steps
$base->addNoiseImage(\Imagick::NOISE_RANDOM);
$base->blurImage(0, 25);
$base->normalizeImage();
$base->separateImageChannel(\Imagick::CHANNEL_GRAY);
$base->posterizeImage(3, \Imagick::DITHERMETHOD_FLOYDSTEINBERG);
$base->thresholdImage(0.5 * \Imagick::getQuantum());
// clone
$pattern = clone $base;
// base
$base->setImageBackgroundColor($base_color);
$base->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);
// pattern
$pattern->setImageBackgroundColor($pattern_color);
$pattern->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);
// layer
$base->compositeImage($pattern, \Imagick::COMPOSITE_DEFAULT, 0, 0);
// set format to png
$base->setImageFormat('png');
header('Content-Type: image/png');
echo $base->getImageBlob();
}
//generate_first_pattern('#2ecc71', '#cc4444');
generate_second_pattern('#2ecc71', '#cc4444');
Q2. Are there any other ways to generate random patterns using ImageMagick?