diff --git a/lib/core.js b/lib/core.js index 5f0c9ef..aef6e4a 100644 --- a/lib/core.js +++ b/lib/core.js @@ -334,10 +334,13 @@ class ThermalPrinter { async printImage(image) { try { // Check if file exists - fs.accessSync(image); + const file = fs.readFileSync(image); // Check for file type - if (image.slice(-4) === '.png') { + if ( + file[0] === 0x89 || // png + file[0] === 0xff // jpeg + ) { try { const response = await this.printer.printImage(image); this.append(response); @@ -346,7 +349,7 @@ class ThermalPrinter { throw error; } } else { - throw new Error('Image printing supports only PNG files.'); + throw new Error('Image printing supports only PNG and JPEG files.'); } } catch (error) { throw error; diff --git a/lib/types/epson.js b/lib/types/epson.js index 3aaf994..0f06563 100644 --- a/lib/types/epson.js +++ b/lib/types/epson.js @@ -241,13 +241,10 @@ class Epson extends PrinterType { // ----------------------------------------------------- PRINT IMAGE ----------------------------------------------------- // https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=88 - async printImage(image) { - const fs = require('fs'); - const { PNG } = require('pngjs'); + async printImage(imagePath) { try { - const data = fs.readFileSync(image); - const png = PNG.sync.read(data); - const buff = this.printImageBuffer(png.width, png.height, png.data); + const image = this.getImageData(imagePath); + const buff = this.printImageBuffer(image.width, image.height, image.data); return buff; } catch (error) { throw error; diff --git a/lib/types/printer-type.js b/lib/types/printer-type.js index 908f868..ad5fe82 100644 --- a/lib/types/printer-type.js +++ b/lib/types/printer-type.js @@ -1,3 +1,6 @@ +const {PNG} = require("pngjs"); +const jpeg = require("jpeg-js"); + class PrinterType { constructor() { console.error('Consturctor not set'); @@ -42,6 +45,30 @@ class PrinterType { console.error(new Error("'printImageBuffer' not implemented yet")); return null; } + + getImageData(imagePath) { + const fs = require('fs'); + + let image = null; + + const file = fs.readFileSync(imagePath); + if(!file){ + throw new Error(`Could not read image ${imagePath}.`); + } + + // Detect if file is jpg + if (file[0] === 0xff) { + image = jpeg.decode(file); + } else { + image = PNG.sync.read(file); + } + + if (image == null) { + throw new Error("Could not read image."); + } + + return image; + } } module.exports = PrinterType; diff --git a/lib/types/star.js b/lib/types/star.js index 70a50c3..3be6944 100644 --- a/lib/types/star.js +++ b/lib/types/star.js @@ -221,13 +221,10 @@ class Star extends PrinterType { } // ----------------------------------------------------- PRINT IMAGE ----------------------------------------------------- - async printImage(image) { - const fs = require('fs'); - const { PNG } = require('pngjs'); + async printImage(imagePath) { try { - const data = fs.readFileSync(image); - const png = PNG.sync.read(data); - const buff = this.printImageBuffer(png.width, png.height, png.data); + const image = this.getImageData(imagePath); + const buff = this.printImageBuffer(image.width, image.height, image.data); return buff; } catch (error) { throw error; diff --git a/lib/types/tanca.js b/lib/types/tanca.js index 4d7b4c3..3feae66 100644 --- a/lib/types/tanca.js +++ b/lib/types/tanca.js @@ -241,13 +241,10 @@ class Tanca extends PrinterType { // ----------------------------------------------------- PRINT IMAGE ----------------------------------------------------- // https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=88 - async printImage(image) { - const fs = require('fs'); - const { PNG } = require('pngjs'); + async printImage(imagePath) { try { - const data = fs.readFileSync(image); - const png = PNG.sync.read(data); - const buff = this.printImageBuffer(png.width, png.height, png.data); + const image = this.getImageData(imagePath); + const buff = this.printImageBuffer(image.width, image.height, image.data); return buff; } catch (error) { throw error; diff --git a/package.json b/package.json index bc2c850..fcc088e 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "homepage": "https://github.com/Klemen1337/node-thermal-printer", "dependencies": { "iconv-lite": "0.5.0", + "jpeg-js": "^0.4.4", "pngjs": "3.3.3", "unorm": "1.4.1", "write-file-queue": "0.0.1"