/* istanbul ignore file */ 'use strict' const appRoot = require( 'app-root-path' ) const sharp = require( 'sharp' ) const xmlescape = require( 'xml-escape' ) var pixelWidth = require( 'string-pixel-width' ) module.exports = { async makeOgImage( previewBufferOrFilename, streamName ) { let imgWidth = 1200 let imgHeight = 627 let panelPadding = 20 let panelWidth = imgWidth - 2 * panelPadding let panelHeight = 80 let title = '/ ' + streamName let maxTitleSize = 750 if ( pixelWidth( title, { font: 'open sans', size: 48 } ) > maxTitleSize ) { while ( pixelWidth( title, { font: 'open sans', size: 48 } ) > maxTitleSize ) { title = title.slice( 0, -1 ) } title += '...' } // console.log( streamName, pixelWidth( title, { font: 'open sans', size: 48 } ), ' / ', imgWidth - 2 * panelPadding - 305 ) const logo = await sharp( `${appRoot}/modules/previews/assets/speckle_logo_and_text.png` ).resize( { height: panelHeight } ).toBuffer() const topPanel = Buffer.from( ` ${xmlescape( title )} ` ) // return await sharp( previewBufferOrFilename ).resize( { width: imgWidth, height: imgHeight } ).composite( [ { input: topPanel, top: 0, left: 0 }, { input: logo, left: panelPadding + 10, top: panelPadding }, ] ).png().toBuffer() } }