So I took ImageMagick Watermark in PHP code as available in www.rainbodesign.com and adjusted it so that it fits my needs: adding an overlay watermark image on the bottom right corner of the original image. This link triggers the conversion mywebsite.com/watermark.php?src=filepath-to-image
and returns the new watermarked image. Here's the full code:
<?php
// ImageMagick Watermark in PHP
// Copyright 2010-2011 by Richard L. Trethewey [email protected]
// http://www.rainbodesign.com/pub/watermark/
// 10/28/11 RLT
// If you use this script, I'd appreciate a link! Thanks!
// 05/16/11 Updated to support text rotation, top/bottom/center
// 05/28/11 Updated to sanitize inputs
// 10/25/11 Added 'auto' color, inspired by Mikko's Blog http://valokuva.org/?p=59
// RGB to HSL code by EasyRGB.com and http://serennu.com/colour/rgbtohsl.php
// 11/10/20 Deleted all references to text overlay and its settings
// Added bottomright support
// Constants
$bContent = "Content-type: image/bmp"; // for BMP files
$jContent = "Content-type: image/jpeg"; // for JPEG files
$gContent = "Content-type: image/gif"; // for GIF files
$pContent = "Content-type: image/png"; // for PNG files
$tContent = "Content-type: image/tiff"; // for TIFF files
$old_image = ''; // Default base image filepath.
// User Settings and Defaults
$wm_image = 'images/watermark.png'; // Default watermark image file (include a path/URL, if necessary)
$wm_type = 'img'; // Set to 'msg' for text watermark. Set to 'img' for image watermark.
$position = 'bottomright'; // Default watermark position - bottom left corner
$wm_opacity = 80; // Default watermark image overlay opacity
$wm_rotate = 0; // Default watermark image overlay rotation (in degrees)
// $gravity = 'SouthWest'; // Default watermark Imagemagick gravity - bottom left corner
$padding = 0; // Default padding in pixels for watermark positioning. Both top/bottom and left/right.
// Handle Query String option parameters
// src
if (isset($_GET['src'])) { $old_image = $_GET['src']; }
// Block script hacking.
$matches = array();
if (preg_match('/([;\n\r])/i', $old_image, $matches)) { die; }
switch(strtolower($position)) {
case('bottom'):
$gravity = 'SouthWest';
break;
case('bottomcenter'):
$gravity = 'South';
break;
case('bottomright'):
$gravity = 'SouthEast';
break;
case('top'):
$gravity = 'NorthWest';
break;
case('topcenter'):
$gravity = 'North';
break;
case('center'):
$gravity = 'Center';
break;
} // end switch($position)
$dot = strrpos($old_image, '.'); // Set image type based on file name extension. I know. Sorry, but it's fastest.
$extension = substr($old_image, $dot);
switch($extension) {
case('.jpg'):
case('.jpeg'):
$theContent = $jContent;
$theWMType = 'jpg:-';
break;
case('.gif'):
$theContent = $gContent;
$theWMType = 'gif:-';
break;
case('.png'):
$theContent = $pContent;
$theWMType = 'png:-';
break;
case('.bmp'):
$theContent = $bContent;
$theWMType = 'bmp:-';
break;
case('.tif'):
$theContent = $tContent;
$theWMType = 'tif:-';
break;
} // end switch($extension)
// Image Overlay Watermark
if ($wm_type == 'img') {
$wmCmd = "convert $wm_image -background transparent -rotate $wm_rotate";
$wmCmd .= " -scale 35%";
$wmCmd .= " miff:- |";
$wmCmd .= " composite -gravity $gravity -geometry +0+0 -blend " . $wm_opacity . "%";
$wmCmd .= " - $old_image $theWMType";
} // endif $wm_type == 'img'
$test = 0; // set to 1 to view ImageMagick command being executed
if ($test) {
header('Content-type: text/plain');
echo("$wmCmd\n");
die;
}
header($theContent);
passthru($wmCmd); // use passthru() to output binary data
exit;
?>
With this code I managed to add a watermark overlay image to the original images. But I need the watermak image to be 35% of original image size, not 35% of watermark image size! (Of course, with the watermark image mantaining it's aspect ratio)
// Image Overlay Watermark
if ($wm_type == 'img') {
$wmCmd = "convert $wm_image -background transparent -rotate $wm_rotate";
$wmCmd .= " -scale 35%"; // here is where I need the magic to happen!
$wmCmd .= " miff:- |";
$wmCmd .= " composite -gravity $gravity -geometry +0+0 -blend " . $wm_opacity . "%";
$wmCmd .= " - $old_image $theWMType";
} // endif $wm_type == 'img'
Does anybody know how to achieve this? Thanks in advance!! <3