php - PHP 中的 ImageMagick Watermark - 如何使用原始图像大小的 % 而不是水印图像大小的 % 来缩放水印?
问题描述
因此,我采用了www.rainbodesign.com中提供的 PHP 代码中的ImageMagick Watermark并对其进行了调整以符合我的需求:在原始图像的右下角添加叠加水印图像。此链接触发转换并返回新的水印图像。这是完整的代码: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;
?>
使用此代码,我设法将水印叠加图像添加到原始图像中。但我需要水印图像是原始图像大小的 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 版本总是很好。
推荐阅读
- wordpress - 在外部应用程序中显示来自 Woocommerce 的产品
- angularjs - 使用路由解析路由到组件 | AngularJS
- swift - 在 UIDatePicker 中设置时间默认值
- python - 缺少 Django 管理仪表板 CSS
- oracle-apex - 在 ajax 回调中设置的 Oracle APEX 页面项目不会持续存在
- c# - 如何在触发新 Windows 窗体的 Outlook (VSTO) 中进行长轮询
- laravel - 如何在laravel中的表下获取不在belongsToMany关系下的用户列表?
- javascript - 使用列和行的 Javascript/html 游戏
- spring-boot - 如何在反序列化时删除变量的可见性但在序列化时启用可见性?
- centos7 - Apache VirtualHost 在 Centos 7 服务器上无法正常运行