watermark jQuery Plugin

repository·master·Indexed 41 days ago

https://github.com/lelinhtinh/watermark

A jQuery plugin (v1.0.2) that applies text or image watermarks to images client-side using HTML5 and JavaScript. It supports customizable positioning via gravity, output format control (jpeg, png, webp), and provides base64 URLs via callbacks for image exporting without a server.

Tokens
1.9K
Snippets
11
Records
12
Agent score
1%

What's inside watermark

  1. Resize output images with Watermark

    master

    You can control the dimensions of the resulting image using outputWidth or outputHeight.

    Important: Do not use both parameters simultaneously as it may distort the image. If you provide only one, the plugin will adjust the other dimension to maintain the original aspect ratio.

    $(function() {
      $('.img_awesome').watermark({
        outputWidth: 500
      });
    });
  2. Set up and initialize Watermark

    master

    The plugin requires jQuery 1.5 or above. Ensure jQuery is loaded before the watermark plugin in your HTML document.

    To use the plugin, call .watermark(OPTIONS) on a jQuery selector.

    <!-- jQuery 1.5+ -->
    <script src="jquery.js" type="text/javascript"></script>
    <!-- jQuery plugin Watermark -->
    <script src="jquery.watermark.js" type="text/javascript"></script>
    $(function() {
      $(SELECTOR).watermark(OPTIONS);
    });
    $(function() {
      $('.img_awesome').watermark();
    });
  3. Set up Watermark in your HTML

    master

    Include jQuery (1.5+) and the jquery.watermark.js file at the end of your HTML document before calling the plugin.

    <!-- jQuery 1.5+ -->
    <script src="jquery.js" type="text/javascript"></script>
    <!-- jQuery plugin Watermark -->
    <script src="jquery.watermark.js" type="text/javascript"></script>
  4. Troubleshoot CORS issues with watermark images

    master

    If you are using an image URL as a watermark and encounter errors, the server hosting that image must allow CORS. If you manage the server, configure Apache with the following headers:

    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Headers "referer, range, accept-encoding, x-requested-with"
  5. Resolve CORS issues for Watermark images

    master

    If you are using images from a different domain as watermarks, the server hosting those images must allow CORS (Cross-Origin Resource Sharing). If you use an Apache server, you can configure the following headers:

    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Headers "referer, range, accept-encoding, x-requested-with"
  6. Handle Watermark output URLs (Single and Batch)

    master

    Since the plugin processes images in the browser, it returns the watermarked image as a base64 URL via the done callback. This is useful for exporting images without a server.

    Single Image

    $(function() {
        $('<img>', {
            src: 'http://i.imgur.com/AAPx3rB.jpg'
        }).watermark({
            done: function (imgURL) {
                console.log(imgURL);
            }
        });
    });

    Multiple Images (Batch)

    To process multiple images, you can use a $.Deferred object to track when all images have finished processing.

    $(function() {
        var inputImages = ['http://i.imgur.com/AAPx3rB.jpg', 'http://i.imgur.com/39dfdPw.jpg', 'http://i.imgur.com/3OfclQY.jpg'];
        var outputImages = [];
        var defer = $.Deferred();
    
        $.when(defer).done(function () {
            console.log(outputImages);
        });
    
        $.each(inputImages, function (i, v) {
            $('<img>', {
                src: v
            }).watermark({
                done: function (imgURL) {
                    outputImages[i] = imgURL;
                    if (i + 1 === inputImages.length) {
                        defer.resolve();
                    }
                }
            });
        });
    });
    $(function() {
        var inputImages = ['http://i.imgur.com/AAPx3rB.jpg', 'http://i.imgur.com/39dfdPw.jpg', 'http://i.imgur.com/3OfclQY.jpg'];
    
        var outputImages = [];
    
        var defer = $.Deferred();
        $.when(defer).done(function () {
            console.log(outputImages);
        });
    
        $.each(inputImages, function (i, v) {
            $('<img>', {
                src: v
            }).watermark({
                done: function (imgURL) {
                    outputImages[i] = imgURL;
                    if (i + 1 === inputImages.length) {
                        defer.resolve();
                    }
                }
            });
        });
    });
  7. Apply a text-based watermark

    master

    To use text instead of an image, provide the text property. You can also customize the text size, background, and position using gravity.

    $(function() {
      $('.img_awesome').watermark({
        text: 'GOOGLE.COM',
        textWidth: 100,
        gravity: 'w',
        opacity: 1,
        margin: 12
      });
    });
  8. Handle watermarked images via callbacks

    master

    When you don't want to modify existing DOM elements directly, you can create new image elements and use the done callback to retrieve the resulting base64 URL.

    // Single image example
    $(function() {
        $('<img>', {
            src: 'http://i.imgur.com/AAPx3rB.jpg'
        }).watermark({
            done: function (imgURL) {
                console.log(imgURL);
            }
        });
    });
  9. Use text as a watermark

    master

    To use text instead of an image, provide a string to the text parameter. Note that providing text will disable the path parameter.

    $(function() {
      $('.img_awesome').watermark({
        text: 'GOOGLE.COM',
        textWidth: 100,
        gravity: 'w',
        opacity: 1,
        margin: 12
      });
    });
  10. Use the watermark() method

    master

    Apply the .watermark() method to a jQuery selector to process images. The plugin processes images client-side using HTML5 and JavaScript, meaning no server-side processing is required.

    $(function() {
      $(SELECTOR).watermark(OPTIONS);
    });
  11. Configure Watermark options

    master

    The .watermark() method accepts an options object to customize the watermark appearance, position, and output format.

    NameTypeDefaultDescription
    pathString'watermark.png'Path to the watermark image (URL or base64). Disabled if text is used.
    textString''Text to use as a watermark.
    textWidthNumber130Text frame width in px.
    textSizeNumber12Font size in px.
    textColorString'white'Text color (HEX or RGBA).
    textBgString'rgba(0, 0, 0, 0.4)'Background color (HEX or RGBA).
    gravityString'se'Position: nw, n, ne, w, e, sw, s, se, c.
    opacityNumber0.7Transparency (0 to 1).
    marginNumber10Distance from edge in px.
    outputWidthNumber'auto'Output width in px or 'auto'.
    outputHeightNumber'auto'Output height in px or 'auto'.
    outputTypeString'jpeg'Format: jpeg, png, or webp (webp is Chrome-only).
    doneFunctionfunction(imgURL){this.src=imgURL;}Callback after image is created. Receives imgURL.
    failFunctionfunction(){}Callback on error.
    alwaysFunctionfunction(){}Callback when processing finishes (both done and fail).