note 105829 added to function.imagick-queryfontmetrics
| From: | abcrdw at gmail dot com | Date: | Sun, 18 Sep 2011 17:49:24 +0000 |
| Subject: | note 105829 added to function.imagick-queryfontmetrics | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183260@lists.php.net to get a copy of this message | ||
This script will print a watermark on the middle of an image. The width of the watermark will depend
on the width of the image.
<?php
// Create a new Imagick object
$objImage = new Imagick( 'path/to/image.jpg' );
// Get the width and height of the image
$imgSize = $objImage->getImageGeometry();
$imgWidth = $imgSize['width'];
$imgHeight = $imgSize['height'];
// Create a new text object
$objText = new ImagickDraw();
// Set te text color
$objText->setFillColor( new ImagickPixel('grey') );
// Set the text transparency: 0 = transparent, 1 = opaque
$objText->setFillAlpha( 0.2 );
// Top left will be point of reference
$objText->setGravity( Imagick::GRAVITY_NORTHWEST );
// Create an array for the textwidth and textheight
$textProperties = array( 'textWidth' => 0 );
// Set the desired width of the watermark to 90% of the image width
$textDesiredWidth = intval( $imgWidth * 0.9 );
// Set an initial value for the fontsize, will be increased in the loop below
$fontSize = 0;
// We use the domain name of the server for the watermark text
$text = $_SERVER['SERVER_NAME'];
// Increase the fontsize until we have reached our desired width
while ( $textProperties['textWidth'] <= $textDesiredWidth ) {
$objText->setFontSize( $fontSize );
$textProperties = $objImage->queryFontMetrics( $objText, $text );
$fontSize++;
}
// Calculate the horizontal starting position
$watermarkPosX = intval( ($imgWidth - $textProperties['textWidth']) / 2 );
// Calculate the vertical starting position
$watermarkPosY = floor( ($imgHeight - $textProperties['textHeight']) / 2 );
// Composite the text on the image
$objImage->annotateImage( $objText, $watermarkPosX, $watermarkPosY, 0, $text );
// The browser must know this is JPEG image or else it will display garbled text
header( 'Content-Type: image/jpeg' );
// Display the image
echo $objImage;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 213.46.158.110
----
Manual Page -- http://www.php.net/manual/en/function.imagick-queryfontmetrics.php
Edit -- https://master.php.net/note/edit/105829
Del: integrated -- https://master.php.net/note/delete/105829/integrated
Del: useless -- https://master.php.net/note/delete/105829/useless
Del: bad code -- https://master.php.net/note/delete/105829/bad+code
Del: spam -- https://master.php.net/note/delete/105829/spam
Del: non-english -- https://master.php.net/note/delete/105829/non-english
Del: in docs -- https://master.php.net/note/delete/105829/in+docs
Del: other reasons-- https://master.php.net/note/delete/105829
Reject -- https://master.php.net/note/reject/105829
Search -- https://master.php.net/manage/user-notes.php