如何解决PHP中的ImageMagick水印-如何使用原始图像尺寸的%代替水印图像尺寸的%缩放水印?
因此,我使用了www.rainbodesign.com中可用的PHP代码中的 ImageMagick 水印,并对其进行了调整,使其适合我的需要:在原始图像的右下角添加一个叠加水印图像。此链接触发转换mywebsite.com/watermark.PHP?src=filepath-to-image
并返回新的带水印的图像。这是完整的代码:
<?PHP
// ImageMagick Watermark in PHP
// copyright 2010-2011 by Richard L. Trethewey rick@rainbo.net
// 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;
?>
使用此代码,我设法将水印叠加图像添加到原始图像。但是我需要将watermak图像的大小设为原始图像大小的35%,而不是水印图像大小的35%! (当然,要保留水印图像的长宽比)
// 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'
有人知道如何实现这一目标吗?提前致谢!!
解决方法
我将使用getimagesize()获得原始图像的大小(我知道这是GD命令),然后使用该宽度来调整水印的大小。
$size = getimagesize($old_image);
$watermakSize = $size[0]*.3; ( you can work out what 35% is )
$wmCmd .= " -resize {$watermakSize}x"; // here is where I need the magic to happen!
还有其他方法,但这是一个简单的方法。
总是很高兴提及您使用的Imagemagick版本。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。