© Efflare Systems
website : e-mail

index | examples | api | language
Example 4 • Watermark an Image
Watermarking is most often used to protect copyrighted images. Watermarking is typically used on an intermediate sized image, not thumbnails, for convenient previewing of the protected image. A good watermark fills enough of the image to protect it while remaining subtle.

Our watermark is a png with full alpha transparency. If shown on a white background it looks like the image to the right, otherwise the viewing application decides how to represent the transparency. Internet Explorer represents it with a shade of gray-blue, as you can probably see by viewing watermark.png.

Once you have a watermark, the concept is simple »

• Load an image...
• Load the watermark...
• Paste the watermark into the image.

The following code demonstrates this with our image, zoo.jpg :
    
<cfx_imageflare source="
  b = Bitmap( '#expandpath('.\zoo.jpg')#' )
  watermark = Bitmap( '#expandpath('.\watermark.png')#' )
  b.paste( watermark, b.width/2-watermark.width/2, b.height/2-watermark.height/2, 128, 4 )
  b.save( '#expandpath('.\example04a.jpg')#' )
">
The important line here is :

b.paste( watermark, b.width/2-watermark.width/2, b.height/2-watermark.height/2, 128, 4 )

The second and third arguments are dynamically calculated in order to center the watermark. The fourth argument, 128, controls the amount of transparency the pasted bitmap assumes when blending (0-255). The final argument represents the blending mode. This is the key to achieving a subtle watermarking effect. The value we are using, 4, uses the `overlay` blending mode. Values of 6, `soft light`, or 7, `hard light', are also common in watermarking.



With some modifications, we can make watermarks optimally fill the source image regardless of absolute dimensions :

<cfx_imageflare source="
  b = Bitmap( '#expandpath('.\zoo.jpg')#' )  
  watermark = Bitmap( '#expandpath('.\watermark.png')#' )
  if ( watermark.width/b.width > watermark.height/b.height ) then
    watermark.resize( b.width, 0 )
  else
    watermark.resize( 0, b.height )
  end
  b.paste( watermark, b.width/2-watermark.width/2, b.height/2-watermark.height/2, 128, 6 )
  b.save( '#expandpath('.\example04b.jpg')#' )
">


Or we can do the opposite and make source images fit optimally under a constant size watermark :

<cfx_imageflare source="
  b = Bitmap( '#expandpath('.\zoo.jpg')#' )
  watermark = Bitmap( '#expandpath('.\watermark.png')#' )
  if ( watermark.width/b.width > watermark.height/b.height ) then
    b.resize( watermark.width, 0 ) 
  else
    b.resize( 0, watermark.height )       
  end
  b.paste( watermark, b.width/2-watermark.width/2, b.height/2-watermark.height/2, 128, 4 )
  b.save( '#expandpath('.\example04c.jpg')#' )
">


In these two examples, we determine which dimension ratio, width or height, is larger so we can optimally fit by it.
If we did not the scripts would only work correctly with square images and watermarks.

As usual, feel free to contact us with questions.
Click here to return to the examples index, or continue on to Example 5.