watermarking and resizing (animated) images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
cassy123

watermarking and resizing (animated) images

Post by cassy123 »

hi there,

I am currently trying to modify a Joomla component to support IM for image uploads incl animated gifs

$waterfile = is a partially transparent png file ftp'd to a predefined location, created by me on my local puter (size 32x32px - the site's logo)
$origfile = is the original image (png, jpg, animated gif) created by a submitter on their local puter (any size eg 1280:850)

the component to allows both display of thumbs and large images

large images are set to max width / height : 1000/700px
thumbs are set to max width / height : 250 / 200px

IM should be used for watermarking and resizing the uploaded image preserving possible animations with $waterfile being added to the bottom left corner of $origfile

for resizing images I have so far:

$commands = ' -coalesce -resize "'.$thumb_w.'x'.$thumb_h.'" -quality "'.$quality.'" -unsharp "2x1.4+1.7+0.07"';
$convert = $this->_escapeshellcmd($path . 'convert').' "'.$origfile.'" '.$commands.' "'.$newfile.'" ';
$output = null;
$status = null;
@exec($convert, $output, $status);

while I have read
http://www.imagemagick.org/Usage/annota ... ark_symbol
http://www.imagemagick.org/Usage/annota ... mark_image
this does not quite seem to be what I'm looking for

and while this
http://www.imagemagick.org/Usage/anim_mods/#annotating
is making a lot more sense - I could use it both for animated and non-animated images -
-annotate appears to only be for adding text to an (animated) image while I would like to use a partially transparent png


when should the watermark be added? before or after resizing the image?
is it possible to add the watermark only after having resized $origfile to the large image while at the same time adding the watermark to $origfile prior to resizing it ?
can it be included in the above snippet ? or would it require an additional snippet

Could somebody please help me out?
I would majorly appreciate you taking the time to do so.

------------------------------------------------------------------------------------------------------------
my setup: Joomla 1.5.20 / IM 6.3.7 - latest stable for Debian Lenny (according to my host)
cassy123

Re: watermarking and resizing (animated) images

Post by cassy123 »

did I provide insufficient information for getting help ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: watermarking and resizing (animated) images

Post by fmw42 »

I am not all that used to reading PHP code. Have you tried doing it simply on the command line? Can you provide a simple example command line or at least links to your files and a description of what you want the result to look like and/or the functional steps you want to apply?

Please correct me. Do you simply want to take an existing animation and add a static watermark on each frame of the animation?

see
http://www.imagemagick.org/Usage/anim_m ... mpose_draw to overlay one image on an animation

or

layers composite -- single image composition
http://www.imagemagick.org/Usage/anim_m ... ite_single
cassy123

Re: watermarking and resizing (animated) images

Post by cassy123 »

as said above I want to adde a watermark (partially transparent png) to an animated gif
the original image should however first be resized before the watermark image is being added

I have this so far:

$commands = ' -coalesce -resize "'.$thumb_w.'x'.$thumb_h.'" -quality "'.$quality.'" -unsharp "2x1.4+1.7+0.07"';
$watercmd1 = ' -gravity SouthWest -geometry -5+5 null:';
$watercmd2 = ' -layers composite -layers optimize';
$convert = $path . 'convert' .' "'.$origfile.'" '.$commands.$watercmd1.' "'.$waterfile.'" '.$watercmd2.' "'.$newfile.'" ';

only shouldn't there be some paranthesis somewhere ? due to the fact that I would want the original image to being resized prior to the watermark application?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: watermarking and resizing (animated) images

Post by fmw42 »

post links to your animation and the watermark image, so that others can work with it to get you want you want. again it is best to work in command line mode first before trying to port to PHP, if you can.
when should the watermark be added? before or after resizing the image?
is it possible to add the watermark only after having resized $origfile to the large image while at the same time adding the watermark to $origfile prior to resizing it ?
Not quite sure what you want to do here. The watermark can be added to the original and then resized or after the resize, if done properly. It depends upon just what you want to do.
cassy123

Re: watermarking and resizing (animated) images

Post by cassy123 »

as command line the above would be:
convert $animation -coalesce -resize A x B -quality 100 -unsharp 2x1.4+1.7+0.07 -gravity SouthWest -geometry -5+5 null: $waterfile -layers composite -layers optimize $adjusted_animation

as I would like to get the hang of this I am trying to figure out two things:
a - resize $animation first then add $watermark
b - add watermark first then resize the combined image
cassy123

Re: watermarking and resizing (animated) images

Post by cassy123 »

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: watermarking and resizing (animated) images

Post by Bonzo »

Image

Code: Select all

<?php
$animation = "aniamtion.gif";

$watermark = "watermark.png";

$watermarked_animation = "output.gif";

exec("convert -resize 200x200 $animation -coalesce -gravity South -geometry +0+0 null: $watermark -layers composite -layers optimize $watermarked_animation ");
?> 
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: watermarking and resizing (animated) images

Post by anthony »

WARNING:
read and coalesce the animation BEFORE using -resize.

See IM examples, Animation Modifications, Resize
http://www.imagemagick.org/Usage/anim_mods/#resize

If you don't and your animation has any form of optimization such as 'frame' or 'transparency', then it will go screwy!!! It only worked in your case as your 'morphed' animation is already a coalesced animation.

See Animation Types. Also look at Animation Optimizations, to get an idea of what these optimizations are
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cassy123

Re: watermarking and resizing (animated) images

Post by cassy123 »

thanks for getting back to me! :)
anthony said:
read and coalesce the animation BEFORE using -resize.
would you mean the command line posted by Bonzo should rather look like this:

Code: Select all

exec("convert $animation -coalesce -resize 200x200 -gravity South -geometry +0+0 null: $watermark -layers composite -layers optimize $watermarked_animation ");
if so I would still have a couple more questions:

1) where would I then go with the quality option and unsharpening mask ? should it go before or after the layers options or anywhere else ?

Code: Select all

exec("convert $animation -coalesce -resize 200x200 -gravity SouthWest -geometry +0+0 null: $waterfile -quality 100 -unsharp 2x1.4+1.7+0.07 -layers composite -layers optimize $watermarked_animation");
or

Code: Select all

exec("convert $animation -coalesce -resize 200x200 -gravity SouthWest -geometry +0+0 null: $waterfile -layers composite -layers optimize -quality 100 -unsharp 2x1.4+1.7+0.07 $watermarked_animation");
2) would you mind a hint on how to determine geometry to reflect eg a margin bottom of 15px between the watermark and the bottom edge of $animation with gravity set to a southern position or a margin right with gravity set to an eastern position ?

3) the extension I'm working on would create 2 images upon submission of $animation: a large image and a thumb with the large image opening in a popup on click of the thumb. its max width / height are config settings. upon creation of the thumb I would like to use a proportionally downsized watermark
eg
$image_w = 900
$image_h = 650
$thumb_w = 250
$thumb_h = 200
$water_ratio = round ( min ($thumb_w/$image_w, $thumb_h/$image_h).'%' = 28%

would the following be working in such a fashion that the first resizing option would resize $animation only and the second one $watermark only ?

Code: Select all

exec("convert $animation -coalesce -resize 250x200 -gravity SouthWest -geometry +0+0 null: $waterfile -resize 28% -layers composite -layers optimize -quality 100 -unsharp 2x1.4+1.7+0.07 $watermarked_animation");
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: watermarking and resizing (animated) images

Post by anthony »

cassy123 wrote:1) where would I then go with the quality option and unsharpening mask ? should it go before or after the layers options or anywhere else ?
-quality is an output setting and is not used by GIF animations. It is not needed

-unsharp is generally applied after -resize to finish off and improve the final result of a resize operation.

See IM Examples, Resize, Unshapr technique
http://www.imagemagick.org/Usage/resize/#resize_unsharp
yes this is in a sort of odd place though it is related to use of blurry filters.
2) would you mind a hint on how to determine geometry to reflect eg a margin bottom of 15px between the watermark and the bottom edge of $animation with gravity set to a southern position or a margin right with gravity set to an eastern position?
-gravity south -geometry +0+15

The direction of geometry is reversed by the gravity setting (weird but true)
See Image annotation, Image positioning with gravity
http://www.imagemagick.org/Usage/annota ... ge_gravity

3) the extension I'm working on would create 2 images upon submission of $animation: a large image and a thumb with the large image opening in a popup on click of the thumb. its max width / height are config settings. upon creation of the thumb I would like to use a proportionally downsized watermark
I see two ways. either resize the watermark before it is overlaid on the resized animation. OR overlay it on the animation
at full size then resize the resultign animation. The later may result in different sized watermarks depending on the original size of the original animation. It looks like you are attempting the first solution.

HOWEVER: you want to restrict the resize operation to JUST the watermark as such your command needs parentheses so you can work on the watermark separate (on the side) to the already read in and resized main animation. Otherwise that second resizes the original animation again, so that it is 28% of its already resized size (very small).

Code: Select all

convert $animation -coalesce -resize 250x200 \
            null:  \( $waterfile -resize 28% \)   -unsharp 2x1.4+1.7+0.07 \
            -gravity SouthWest -geometry +0+15  -layers composite \
            -layers optimize  $watermarked_animation
The above is split up into lines based on the major image processing operations (see RubbleWebs Notes on writing PHP code near bottom for how to do this in PHP).

The first line reads and resizes the animation
The second reads and resizes the watermark image (with null: separator for later).
The unsharp is NOT in parenthesis so it gets applied to BOTH animation and watermark
Then we 'compose' the watermark on the animation
and the last line gives all the final output settings and where to save the result.

using multi-lines make the whole thing a LOT clearer!

Remember IM only does what you tell it to do, in the order you tell it to do it!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cassy

Re: watermarking and resizing (animated) images

Post by cassy »

thanks again for getting back to me :D
the hint on RubbleWebs notes has been a big help for getting it to work under PHP.

I have yet another couple questions though:

1) I was referring explicitely to animated gifs because I would like the command line to work for those as well. the default allowed image file types would be png, jpg, jpeg, gif (config setting) which might be amended by other types such as bmp if the respective site admin chooses to do so
-quality is an output setting and is not used by GIF animations. It is not needed
therefore while I'm aware of the quality setting not being 'used' for gif images I would still like to include it for use with other image file types.
if I understand your explanations above correctly the command line would then need to be adjusted for

Code: Select all

convert $origfile -coalesce -resize $resize \
            null:  \( $waterfile -resize $water_resize \)   -unsharp $unsharp \
            -gravity $position -geometry $geometry -layers composite \
            -quality $quality -layers optimize  $newfile
2) in addition I would like to make $watermark semi-transparent. my sample watermark has fully transparent pixels and solid colorized pixels.
when opening $watermark in photoshop and in the layers palette setting opacity to 75% the fully transparent pixels would still be fully transparent while the formerly solid colorized pixels would be semi-transparent
I was checking out -transparency and -alpha and I have also read your usage chapter about Channels, Masks, and Transparency but somehow I cannot figure out a way to achieve the above result

would you mind a hint on how to get there - if possible?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: watermarking and resizing (animated) images

Post by anthony »

cassy wrote:2) in addition I would like to make $watermark semi-transparent. my sample watermark has fully transparent pixels and solid colorized pixels.
when opening $watermark in photoshop and in the layers palette setting opacity to 75% the fully transparent pixels would still be fully transparent while the formerly solid colorized pixels would be semi-transparent
I was checking out -transparency and -alpha and I have also read your usage chapter about Channels, Masks, and Transparency but somehow I cannot figure out a way to achieve the above result

would you mind a hint on how to get there - if possible?
You have two solutions. make your watermark image semi-transparent first (probably the better solution)
For example

Code: Select all

 -channel A -evaluate multiply 0.5 +channel A
would make the current image 50% transparent.

The other is to use a Disolve composition. You do this in the exact same way you would use a Composition method containing arguments in convert, just use -layers composite rather than -composite.
See example in http://www.imagemagick.org/Usage/compos ... d_dissolve
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cassy

Re: watermarking and resizing (animated) images

Post by cassy »

thanks again! :)

when saying
make your watermark image semi-transparent first
would you mean modifying the command lind line like this

Code: Select all

convert $origfile -coalesce -resize $resize \
            null:  \( $waterfile -channel A -evaluate multiply $multiply +channel A -resize $water_resize \)   -unsharp $unsharp \
            -gravity $position -geometry $geometry -layers composite \
            -quality $quality -layers optimize  $newfile
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: watermarking and resizing (animated) images

Post by anthony »

Yeap that should do it!

Though if you do it directly to your actual watermark file (resize and unsharp on it too)
then you don't need to repeat it for every image you watermark! More pre-processing - faster watermarking
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply