PHP 中的 ImageMagick 水印 - 如何使用原始图像大小的百分比而不是水印图像大小的百分比来缩放水印?
ImageMagick Watermark in PHP - How can I scale watermark with % of original image size instead of % of watermark image size?
所以我在 PHP 代码中使用了 ImageMagick 水印 www.rainbodesign.com 并对其进行了调整以使其符合我的需要:添加叠加水印图像在原始图像的右下角。此 link 触发转换 mywebsite.com/watermark.php?src=filepath-to-image
和 returns 新的带水印图像。这是完整的代码:
<?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'
有人知道如何实现吗?提前致谢!! <3
我会使用 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。
所以我在 PHP 代码中使用了 ImageMagick 水印 www.rainbodesign.com 并对其进行了调整以使其符合我的需要:添加叠加水印图像在原始图像的右下角。此 link 触发转换 mywebsite.com/watermark.php?src=filepath-to-image
和 returns 新的带水印图像。这是完整的代码:
<?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'
有人知道如何实现吗?提前致谢!! <3
我会使用 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。