how to use ImagickPixelIterator to get the average luminosity of the background and chosing text color according to it.
<?php
/* Allowed images */
$allowed = array( "strawberry.png", "Image32.png" );
$img = ( isset( $_GET['img'] ) && in_array( $_GET['img'], $allowed ) )
? $_GET['img'] : 'strawberry.png';
/* Read the image in. This image will be watermarked. */
$image = new Imagick( $img );
$image->setImageFormat( "png" );
/* The text to write on the mark */
$text = "www.valokuva.org";
/* This object will hold the font properties */
$draw = new ImagickDraw();
/* Setting gravity to the center changes the origo
where annotation coordinates are relative to */
$draw->setGravity( Imagick::GRAVITY_CENTER );
/* Use a custom truetype font */
$draw->setFont( "./WCManoNegraBta.ttf" );
/* Set the font size */
$draw->setFontSize( 26 );
/* Create a new imagick object */
$im = new imagick();
/* Get the text properties */
$properties = $im->queryFontMetrics( $draw, $text );
/* Region size for the watermark.
Add some extra space on the sides */
$watermark['w'] = intval( $properties["textWidth"] + 5 );
$watermark['h'] = intval( $properties["textHeight"] + 5 );
/* Create a canvas using the font properties.
Add some extra space on width and height */
$im->newImage( $watermark['w'], $watermark['h'],
new ImagickPixel( "transparent" ) );
/* Get a region pixel iterator to get the pixels in the watermark area */
$it = $image->getPixelRegionIterator( 0, 0, $watermark['w'], $watermark['h'] );
$luminosity = 0;
$i = 0;
/* Loop trough rows */
while( $row = $it->getNextIteratorRow() )
{
/* Loop trough each column on the row */
foreach ( $row as $pixel )
{
/* Get HSL values */
$hsl = $pixel->getHSL();
$luminosity += $hsl['luminosity'];
$i++;
}
}
/* If we are closer to white, then use black font and
the other way around */
$textColor = ( ( $luminosity / $i )> 0.5 ) ?
new ImagickPixel( "black" ) :
new ImagickPixel( "white" );
/* Use the same color for the shadow */
$draw->setFillColor( $textColor );
/* Use png format */
$im->setImageFormat( "png" );
/* Annotate some text on the image */
$im->annotateImage( $draw, 0, 0, 0, $text );
/* Clone the canvas to create the shadow */
$watermark = $im->clone();
/* Set the image bg color to black. (The color of the shadow) */
$watermark->setImageBackgroundColor( $textColor );
/* Create the shadow (You can tweak the parameters
to produce "different" kind of shadows */
$watermark->shadowImage( 80, 2, 2, 2 );
/* Composite the text on the background */
$watermark->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );
/* Composite the watermark on the image to the top left corner */
$image->compositeImage( $watermark, Imagick::COMPOSITE_OVER, 0, 0 );
/* Display the results */
header( "Content-Type: image/png" );
echo $image;
?>
|