简体   繁体   English

PHP 中的 ImageMagick 水印 - 如何使用原始图像大小的百分比而不是水印图像大小的百分比来缩放水印?

[英]ImageMagick Watermark in PHP - How can I scale watermark with % of original image size instead of % of watermark image size?

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.所以我把ImageMagick的水印在PHP代码可用www.rainbodesign.com然后进行调整,使其适合我的需求:将原始图像的右下角叠加水印图像。 This link triggers the conversion mywebsite.com/watermark.php?src=filepath-to-image and returns the new watermarked image.此链接触发转换mywebsite.com/watermark.php?src=filepath-to-image并返回新的水印图像。 Here's the full code:这是完整的代码:

    <?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;

?>

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!但是我需要水印图像是原始图像大小的 35%,而不是水印图像大小的 35%! (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 <3

I would get the size of the original with getimagesize() ( I know that is a GD command ) and use the width from that to resize the watermark.我会用 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!

There are other ways but this is a simple one.还有其他方法,但这是一个简单的方法。

It is always good to mention what version of Imagemagick you are using.提及您使用的是哪个版本的 Imagemagick 总是好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM