04-10-2009, 02:20 AM
|
#6 (permalink)
|
|
The Visitor
Join Date: Apr 2009
Location: Colorado
Posts: 3
Thanks: 0
|
So, I got this working and furthermore, managed to include rand to generate a random image:
PHP Code:
<?php
//Generate random number
$random = rand(1, 17);
//////Begin Image Analysis/////
/* The original image is the average colors */
$average = new Imagick( 'images/backgrounds/' . $random . '.png');
/* Reduce the amount of colors to 10 */
$average->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, false, false );
/* Only save one pixel of each color */
$average->uniqueImageColors();
/* Clone the average and modulate to brighter */
$bright = $average->clone();
$bright->modulateImage ( 125, 200, 100 );
/* Clone the average and modulate to darker */
$dark = $average->clone();
$dark->modulateImage ( 80, 100, 100 );
/* Helper function to create the mini-images */
function createImages( Imagick $composite, Imagick $im )
{
/* Get ImagickPixelIterator */
$it = $im->getPixelIterator();
/* Reset the iterator to begin */
$it->resetIterator();
/* Loop trough rows */
while( $row = $it->getNextIteratorRow() )
{
/* Loop trough columns */
foreach ( $row as $pixel )
{
/* Create a new image which contains the color */
$composite->newImage( 20, 20, $pixel );
$composite->borderImage( new ImagickPixel( "black" ), 1, 1 );
}
}
}
/* This object holds the color images */
$composite = new Imagick();
/* Create "icons" for each palette */
createImages( $composite, $dark );
createImages( $composite, $average );
createImages( $composite, $bright );
/* Montage the color images into single image
Ten images per row, three rows */
$montage = $composite->montageImage( new imagickdraw(), "10x3+0+0",
"20x20+4+3>", imagick::MONTAGEMODE_UNFRAME,
"0x0+3+3" );
/* Free some resources */
$composite->destroy();
/* Create an empty canvas */
$canvas = new Imagick();
$canvas->newImage( $montage->getImageWidth() + 55,
$montage->getImageHeight(),
new ImagickPixel( "white" ) );
/* Display the canvas as png */
$canvas->setImageFormat( "png" );
/* Set font size to 12 points */
$draw = new ImagickDraw();
$draw->setFontSize( 12 );
/* Create legends for each palette */
$canvas->annotateImage( $draw, 5, 20, 0, "Dark: " );
$canvas->annotateImage( $draw, 5, 45, 0, "Average: " );
$canvas->annotateImage( $draw, 5, 70, 0, "Bright: " );
/* Composite the montaged images next to texts */
$canvas->compositeImage( $montage, Imagick::COMPOSITE_OVER, 55, 0 );
/* Output the image */
header( "Content-Type: image/png" );
echo $canvas;
?>
What I can't figure out how to do is then turn around and echo out the actual image or even just some text in addition to the png palette that's created, i.e.:
PHP Code:
echo '<h1>Random is:[' . $random . ']</h1>';
The purpose for all of this is to generate a palette of colors that I can then select from for font and background colors in the associated stylesheet. Is there a reason I can't have multiple echo statements?
Thanks!
-C.
|
|
|
|