diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..2328fcc --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,26 @@ +name: npm-publish + +on: + release: + types: [published] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: '12.x' + registry-url: 'https://registry.npmjs.org' + + - name: 'Install dependencies' + run: npm ci + + - name: 'Build typescript' + run: npm run build + + - name: 'Publish library to NPM' + run: npm publish --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} diff --git a/README.md b/README.md index 052b892..18d0882 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,50 @@ # QR Image -Simple library allowing you to generate an image in middle of an QR code. +Simple library allowing you to generate an image in middle of an QR code or without it. -This library contains only 1 method and that is `generateQRWithImage` +This library contains only 1 method and that is `generateQRWithImage` and uses libraries +[qrcode](https://www.npmjs.com/package/qrcode) and [canvas](https://www.npmjs.com/package/canvas) to generate the +resulting image. + +## Quick Example + +```js + + const imageBuffer; + + // for option values please visit https://www.npmjs.com/package/qrcode#qr-code-options library + const options = { + errorCorrectionLevel: 'Q', + color: { + dark: '#0CC8A8' + } + }; + + + // This will create QR with 'https://trisbee.com' encoded in it and with centered image also color will be #0CC8A8 + const resultWithImage = generateQRWithImage('https://trisbee.com', 500, 100, imageBuffer, options) + // This will create just QR code with 'https://trisbee.com' encoded in it in color #0CC8A8 + const resultWithoutImage = generateQRWithImage('https://trisbee.com', 500, 100, null, options) + // This will create classic B&W QR code with 'https://trisbee.com' encoded in it + const resultWithoutImageAndOptions = generateQRWithImage('https://trisbee.com', 500, 100, null, null) + + // The buffer of outputed QR code + const imageBuffer = resultWithImage.buffer; + + // The precentage of how much the inserted image area of QR code image covers + // if 'errorCorrectionLevel' is not specified theoretical maxium of how much the image can cover is 30% + // but we recommend to keep it under 20 and make sure the image doesn't touch the big rectangles in the corners + // of QR + const imageQRCoverage = resultWithImage.coverage + + + +``` + +### Typings +```ts + generateQRWithImage(qrCodeContent: string, width: number, margin: number, imageBuffer?: Buffer, options?: QRCodeToBufferOptions) +``` + +### Options + +For options please visit documentation of [qrcode](https://www.npmjs.com/package/qrcode#qr-code-options) library diff --git a/package.json b/package.json index 02a274e..a3eebd6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@trisbee/qr-image-nodejs", - "version": "1.0.0", + "version": "1.0.2", "types": "dist/index.d.ts", "description": "Allows you to generate image in middle of QR code", "main": "dist/index.js", @@ -9,8 +9,6 @@ "url": "git+https://github.com/trisbee/qr-image-nodejs.git" }, "scripts": { - "run": "ts-node -r tsconfig-paths/register src/index.ts", - "dev": "nodemon -r tsconfig-paths/register -e ts,html -w ./src -x ts-node src/index.ts", "build": "tsc" }, "dependencies": { diff --git a/src/index.ts b/src/index.ts index 171ae35..e8cd03d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,14 +4,13 @@ import {QRCodeToBufferOptions} from "qrcode"; const {createCanvas, loadImage} = require('canvas') +interface QRResponse { + buffer: Buffer, + coverage: number +} - - -export const generateQRWithImage = async (qrCodeContent: string, width: number, margin: number, imageBuffer?: Buffer | null, options?: QRCodeToBufferOptions) : Promise<{ - buffer: Buffer, - qrImageCoverage: number -}> => { +export const generateQRWithImage = async (qrCodeContent: string, width: number, margin: number, imageBuffer?: Buffer | null, options?: QRCodeToBufferOptions): Promise => { const fullHeight = width + margin; @@ -45,14 +44,13 @@ export const generateQRWithImage = async (qrCodeContent: string, width: number, } - const qrVolume = width * width; const result = (imageVolume / qrVolume) * 100; return { buffer: canvas.toBuffer(), - qrImageCoverage: result, + coverage: result, }