如何根据图像调整水印?

时间:2017-07-19 06:31:52

标签: php laravel imagemagick imagick imagemagick-convert

我几乎完成了所有事情,但我面临一个问题。我在多个位置创建多个水印。运行正常但实际上问题是图像具有良好的分辨率并且水印看起来非常小。我想要任何图像分辨率水印应该缩放和可见这里是我的exec功能,我正在使用laravel框架,我正在使用imagick库

$path = storage_path('app/images/TestImages/');
$mediumFileName = $path.str_random(4)."medium".str_random(4).".".$ext;
$watermarkImage = storage_path('watermark.png');
$saveWatermark = $path."image_watermark.jpg";
exec("convert $mediumFileName \( $watermarkImage -write MPR:wm \) \
-define compose:args=30,100 -compose dissolve            \
      -gravity NorthWest -geometry +3+3 -composite      \
MPR:wm -gravity NorthEast -geometry +3+3 -composite      \
MPR:wm -gravity SouthEast -geometry +3+3 -composite      \
MPR:wm -gravity Center -geometry +3+3 -composite      \
MPR:wm -gravity SouthWest -geometry +3+3 -composite $saveWatermark");

2 个答案:

答案 0 :(得分:5)

这是一个很大的水印,对于任何图片都有足够的分辨率,因为它是1000像素的正方形。

enter image description here

现在,如果我们有一个像这样的1000x800像素图像,我们可以在合成之前将水印调整为15%(1000%的15%是代码中的150):

convert image.jpg \( watermark.png -resize 150x -write MPR:wm \) \
          -gravity northwest -geometry +10+10 -composite         \
   MPR:wm -gravity northeast -geometry +10+10 -composite         \
   MPR:wm -gravity southwest -geometry +10+10 -composite         \
   MPR:wm -gravity southeast -geometry +10+10 -composite result.png

enter image description here

enter image description here

但是,如果我们有一个像400x300这样的小图像:

enter image description here

当我们应用水印时,我们首先将其调整为400或15的15%:

convert image.jpg \( watermark.png -resize 60x -write MPR:wm \) \
          -gravity northwest -geometry +10+10 -composite         \
   MPR:wm -gravity northeast -geometry +10+10 -composite         \
   MPR:wm -gravity southwest -geometry +10+10 -composite         \
   MPR:wm -gravity southeast -geometry +10+10 -composite result.png

enter image description here

因此,您需要获得Andreas亲切地向您展示的图像大小:

list($width, $height, $type, $attr) = getimagesize($mediumFileName);

然后将其乘以0.15(比如说15%)并在-resize参数中使用它。

如果上面括号内的“旁边处理”令人沮丧或混淆,你可以通过首先加载和调整水印大小来实现相同的结果,将其放入MPR并且然后加载主图像并将MPR重叠四次。它只是一种不同的,可能更简单的语法:

convert watermark.png -resize 60x -write MPR:wm +delete image.jpg \
   MPR:wm -gravity northwest -geometry +10+10 -composite          \
   MPR:wm -gravity northeast -geometry +10+10 -composite          \
   MPR:wm -gravity southwest -geometry +10+10 -composite          \
   MPR:wm -gravity southeast -geometry +10+10 -composite result.png

答案 1 :(得分:1)

使用imagesize并获取图像大小 选择正确大小的水印并将其添加到图片中。

$path = storage_path('app/images/TestImages/');
$mediumFileName =  $path.str_random(4)."medium".str_random(4).".".$ext;
$watermarkImage = storage_path('watermark.png');

list($width, $height, $type, $attr) = getimagesize($mediumFileName);

if ($height * $width < some Mpx){
    $watermarkImage = storage_path('watermarkSMALL.png');
} elseif($height*$width >some larger Mpx)
    $watermarkImage = storage_path('watermarkLARGE.png');
}

exec("convert 
$mediumFileName \( 
$watermarkImage -write     MPR:wm \) \
-define compose:args=30,100 -compose dissolve            \
  -gravity NorthWest -geometry +3+3 -composite      \
MPR:wm -gravity NorthEast -geometry +3+3 -composite      \
MPR:wm -gravity SouthEast -geometry +3+3 -composite      \
MPR:wm -gravity Center -geometry +3+3 -composite      \
MPR:wm -gravity SouthWest -geometry +3+3 -composite $saveWatermark");