watermarking and resizing (animated) images
watermarking and resizing (animated) images
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)
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)
Re: watermarking and resizing (animated) images
did I provide insufficient information for getting help ?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: watermarking and resizing (animated) images
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
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
Re: watermarking and resizing (animated) images
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?
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?
- 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 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.
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.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 ?
Re: watermarking and resizing (animated) images
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
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
Re: watermarking and resizing (animated) images
you can grab the images here:
http://www.4shared.com/file/0Siv7Xwz/ImageMagick.html
http://www.4shared.com/file/0Siv7Xwz/ImageMagick.html
Re: watermarking and resizing (animated) images

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 ");
?>
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: watermarking and resizing (animated) images
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
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/
https://imagemagick.org/Usage/
Re: watermarking and resizing (animated) images
thanks for getting back to me!
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 ?
or
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 ?

would you mean the command line posted by Bonzo should rather look like this:anthony said:
read and coalesce the animation BEFORE using -resize.
Code: Select all
exec("convert $animation -coalesce -resize 200x200 -gravity South -geometry +0+0 null: $watermark -layers composite -layers optimize $watermarked_animation ");
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");
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");
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");
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: watermarking and resizing (animated) images
-quality is an output setting and is not used by GIF animations. It is not neededcassy123 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 ?
-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.
-gravity south -geometry +0+152) 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?
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
I see two ways. either resize the watermark before it is overlaid on the resized animation. OR overlay it on the animation3) 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
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 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/
https://imagemagick.org/Usage/
Re: watermarking and resizing (animated) images
thanks again for getting back to me
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
if I understand your explanations above correctly the command line would then need to be adjusted for
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?

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
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.-quality is an output setting and is not used by GIF animations. It is not needed
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
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?
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: watermarking and resizing (animated) images
You have two solutions. make your watermark image semi-transparent first (probably the better solution)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?
For example
Code: Select all
-channel A -evaluate multiply 0.5 +channel A
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/
https://imagemagick.org/Usage/
Re: watermarking and resizing (animated) images
thanks again! 
when saying

when saying
would you mean modifying the command lind line like thismake your watermark image semi-transparent first
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
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: watermarking and resizing (animated) images
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
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/
https://imagemagick.org/Usage/