Created
August 22, 2011 09:01
-
-
Save rodneyrehm/1161975 to your computer and use it in GitHub Desktop.
Watermark with IMagick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Watermark with Imagick | |
// load images | |
$image = new Imagick("image.jpg"); | |
$watermark = new Imagick("watermark.png"); | |
// translate named gravity to pixel position | |
$position = gravity2coordinates($image, $watermark, 'lowerRight', 5, 5); | |
// compose watermark onto image | |
$image->compositeImage( $watermark, $watermark->getImageCompose(), $position['x'], $position['y'] ); | |
// you'll never guess what this does… | |
$image->writeImage("result.jpg"); | |
function gravity2coordinates($image, $watermark, $gravity, $xOffset=0, $yOffset=0) { | |
// theoretically this should work | |
// $im->setImageGravity( Imagick::GRAVITY_SOUTHEAST ); | |
// but it doesn't so here goes the workaround | |
switch ($gravity) { | |
case 'upperLeft': | |
$x = $xOffset; | |
$y = $yOffset; | |
break; | |
case 'upperRight': | |
$x = $image->getImageWidth() - $watermark->getImageWidth() - $xOffset; | |
$y = $yOffset; | |
break; | |
case 'lowerRight': | |
$x = $image->getImageWidth() - $watermark->getImageWidth() - $xOffset; | |
$y = $image->getImageHeight() - $watermark->getImageHeight() - $yOffset; | |
break; | |
case 'lowerLeft': | |
$x = $xOffset; | |
$y = $image->getImageHeight() - $watermark->getImageHeight() - $yOffset; | |
break; | |
} | |
return array( | |
'x' => $x, | |
'y' => $y | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment