Choosing watermark color based on the background luminosity : Create and modify images : Graphics Image PHP Source Code


PHP Source Code » Graphics Image » Create and modify images »

 

Choosing watermark color based on the background luminosity


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->setGravityImagick::GRAVITY_CENTER );
/* Use a custom truetype font */
$draw->setFont"./WCManoNegraBta.ttf" );
/* Set the font size */
$draw->setFontSize26 );
/* 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"] + );
$watermark['h'] = intval$properties["textHeight"] + );
/* 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->getPixelRegionIterator00$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$draw000$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->shadowImage8022);
/* Composite the text on the background */
$watermark->compositeImage$imImagick::COMPOSITE_OVER0);
/* Composite the watermark on the image to the top left corner */
$image->compositeImage$watermarkImagick::COMPOSITE_OVER0);
/* Display the results */
header"Content-Type: image/png" );
echo 
$image;
?>




HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Graphics Image
» Create and modify images