Source: print-image.js

/**
 * @function printImage
 * @summary
 * Use this function to print specified image
 * @description
 * <strong>Note: Only designed to work on desktop currently.</strong>
 *
 * Required Parameters:
 * * width:
 *    * Type: Number
 *    * Requirements: Required
 *    * Description: width to display image on print image page
 *      Notes: can be set larger or smaller than original image width if needed
 * * height:
 *    *  Type: Number
 *    *  Requirements: Required
 *    *  Description: height to display image on print image page
 *          * Notes: can be set larger or smaller than original image height if needed
 *
 *
 * Optional Parameters:
 * * image:
 *    * Type: String
 *    * If not specified, you must specify a fullPath (the next option param).
 *    * Description: filename of image file in images folder
 * * fullPath:
 *    * Type: String
 *    * If not specified, you must speciy a image (the previous option param)
 *    * Description: full path to a separately hosted image file
 *      Should be full image path, including image file name + extension
 *    * Example:
 *    ```
 *      {fullPath: "https://tremorvideodsp.com/assets/imgs/web_logo.png"} // (Tremor Video DSP Logo)
 *    ```
 * * top:
 *    * Type: Number
 *    * Description: This is the y-axis (vertical) position that the new print image window will be displayed at.
 *        * Notes: Default is center (Probably leave this at the default, center of the screen)
 * * left:
 *    *  Type: Number
 *    *  Requirements: Optional (See Notes Below)
 *    *  Description: This is the x-axis (horizontal) position that the new print image window will be displayed at.
 *          * Note: Default is center (You should probably leave this at the default, center of the screen)
 *          * Chrome Bug: There is a bug in Chrome (Still Existing in Version 66.0.3359.181).
 *              * On Multi-Monitor setup, the window will display the new window flush left, regardless of the left position when opened on secondary, tertiary monitors.
 *                  * This bug has gone un-resolved for several years. It is uknown at this time when it will be resolved.
 *                  * Note this bug does not exist on primary monitor, only monitor 2, 3+.
 * * delay: amount of time before print is called. Default is 1000. This allows the user time to see the image to be printed when the new window opens, before the print dialog box comes up.
 *
 * @param {Object} opts
 * @param {string=} opts.image
 * @param {string=} opts.fullPath full path to a separately hosted image file
 * @param {number} opts.width width
 * @param {number} opts.height height
 * @param {number=} opts.top top
 * @param {number=} opts.left left
 * @param {number=} opts.delay milliseconds
*/

function printImage (opts) {
  if ($b.isMobile() === false) {
    // Delay Before window.print() is called in the print image window
    var delay = opts.delay || 1000

    // Check for image path (either 'local' image folder or fullPath URL for image)
    var image
    if (opts.image) {
      image = $b.baseUrl + 'images/' + opts.image
    } else if (opts.fullPath) {
      image = opts.fullPath
    } else {
      console.error('You must provide an image path: Can either be a filename of an image in your images folder [opts.image] or a full url path to an image file [opts.fullPath]')
      return
    }

    // Check for specified width & height of image to print
    if (opts.width && opts.height) {
      var width = opts.width + 20
      var height = opts.height + 20
      var imgWidth = opts.width
      var imgHeight = opts.height
    } else {
      console.error('You must provide a width and height to display your image')
      return
    }

    // Create new window feature list
    var windowFeatures = ['width=' + String(width), 'height=' + String(height)]

    // Checks for top & left positions for the new window
    // If none are specified, attempts to position the new window in the center of the screen
    // Note: Bug in Chrome with multi-monitor setup (see snippet notes above)
    var top = (typeof (opts.top) !== 'undefined' ? opts.top : (screen.height / 2) - (height / 2))
    var left = (typeof (opts.left) !== 'undefined' ? opts.left : (screen.width / 2) - (width / 2))

    windowFeatures.push('top=' + String(top))
    windowFeatures.push('left=' + String(left))
    windowFeatures.push('menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=yes')

    // Create new page, open it, and open print dialog box
    var printPage = window.open('about:blank', 'coupon', String(windowFeatures))
    var content = '<head><style>html { padding: 0px!important; margin: 0px!important; } body { padding:0px!important; margin:0px!important; }</style><script> window.onload = function() { setTimeout(function(){ window.print(); window.close(); },' + delay + ");}</script></head><body><img style='width: " + imgWidth + 'px; height:' + imgHeight + "px; display:block; margin:0px auto;' src='" + image + "'></body>"
    printPage.document.open().write(content)
    printPage.document.close()
    printPage.focus()
  }
}

window.$b = window.$b || {}
window.$b.snippets = window.$b.snippets || {}
var snippets = window.$b.snippets
snippets.printImage = printImage

if (window.module) {
  window.module.exports = printImage
}