kanu mahajan
kanu mahajan

Reputation: 109

How to adjust watermark according to image?

I have done almost everything but i am facing one issue. I am creating multiple watermark at multiple locations. That is running fine but actually problem is when the image is having good resolution and watermark is looking very small. I want whatever the image resolution watermark should be zoom and visible here is my exec function and i am using laravel framework and i am using imagick library

$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");

Upvotes: 2

Views: 1654

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207365

Here is a large watermark, with enough resolution for any picture as it is 1,000 pixels square.

enter image description here

Now, if we have a 1000x800 pixel image like this, we can resize the watermark to say 15% of that before compositing it (15% of 1000 is the 150 in the code):

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

But, if we have a smaller image like this 400x300 image:

enter image description here

when we apply the watermark, we first resize it to 15% of 400, or 60:

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

So, you need to get the size of your image how Andreas kindly showed you:

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

and then multiply that by 0.15 (to get say 15%) and use that in your -resize parameter.


If the "aside processing" inside the parentheses above is upsetting or confusing, you can achieve the same result by loading up and resizing the watermark first, on its own, putting it into an MPR and then loading the main image and overlaying the MPR four times. It is just a different, maybe simpler, syntax:

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

Upvotes: 5

Andreas
Andreas

Reputation: 23958

Use imagesize and get the image size.
Choose a correct sized watermark an add that to the picture.

$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");

Upvotes: 1

Related Questions