Skip to content

Commit

Permalink
Add maxLogoWidth&maxLogoHeight options
Browse files Browse the repository at this point in the history
Add maxLogoWidth&maxLogoHeight options
  • Loading branch information
ushelp committed May 26, 2021
1 parent 98b85e0 commit fc3abf5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
65 changes: 48 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)
*
* Version 4.2.7
* Version 4.3.0
*
* @author [ [email protected] ]
*
Expand Down Expand Up @@ -1285,7 +1285,7 @@ Drawing.prototype.draw = function(oQRCode) {
nowDotScale = _htOption.dotScaleAO;
} else if (type == 'AI') {
nowDotScale = _htOption.dotScaleAI;
}else {
} else {
nowDotScale = 1;
}

Expand Down Expand Up @@ -1374,26 +1374,18 @@ Drawing.prototype.draw = function(oQRCode) {
imgContainerW = imgContainerH;
}

if (_htOption.logoWidth) {
if (_htOption.logoMaxWidth) {
imgContainerW = Math.round(_htOption.logoMaxWidth);
} else if (_htOption.logoWidth) {
imgContainerW = Math.round(_htOption.logoWidth);
}
if (_htOption.logoHeight) {

if (_htOption.logoMaxHeight) {
imgContainerH = Math.round(_htOption.logoMaxHeight);
} else if (_htOption.logoHeight) {
imgContainerH = Math.round(_htOption.logoHeight);
}

var imgContainerX = (_htOption.width + _htOption.quietZone * 2 - imgContainerW) / 2;
var imgContainerY = (_htOption.height + _htOption.titleHeight + _htOption.quietZone *
2 - imgContainerH) / 2;

// Did Not Use Transparent Logo Image
if (!_htOption.logoBackgroundTransparent) {
//if (!_htOption.logoBackgroundColor) {
//_htOption.logoBackgroundColor = '#ffffff';
//}
_oContext.fillStyle = _htOption.logoBackgroundColor;

_oContext.fillRect(imgContainerX, imgContainerY, imgContainerW, imgContainerH);
}
var nw;
var nh;
if (typeof img.naturalWidth == "undefined") {
Expand All @@ -1406,10 +1398,47 @@ Drawing.prototype.draw = function(oQRCode) {
nh = img.naturalHeight;
}

if (_htOption.logoMaxWidth || _htOption.logoMaxHeight) {
if (_htOption.logoMaxWidth && nw <= imgContainerW) {
imgContainerW = nw;
}

if (_htOption.logoMaxHeight && nh <= imgContainerH) {
imgContainerH = nh;
}
if (nw <= imgContainerW && nh <= imgContainerH) {
imgContainerW = nw;
imgContainerH = nh;
}
}

var imgContainerX = (_htOption.width + _htOption.quietZone * 2 - imgContainerW) / 2;
var imgContainerY = (_htOption.height + _htOption.titleHeight + _htOption.quietZone *
2 - imgContainerH) / 2;

var imgScale = Math.min(imgContainerW / nw, imgContainerH / nh);
var imgW = nw * imgScale;
var imgH = nh * imgScale;

if (_htOption.logoMaxWidth || _htOption.logoMaxHeight) {
imgContainerW = imgW;
imgContainerH = imgH;
imgContainerX = (_htOption.width + _htOption.quietZone * 2 - imgContainerW) / 2;
imgContainerY = (_htOption.height + _htOption.titleHeight + _htOption
.quietZone *
2 - imgContainerH) / 2;

}

// Did Not Use Transparent Logo Image
if (!_htOption.logoBackgroundTransparent) {
//if (!_htOption.logoBackgroundColor) {
//_htOption.logoBackgroundColor = '#ffffff';
//}
_oContext.fillStyle = _htOption.logoBackgroundColor;

_oContext.fillRect(imgContainerX, imgContainerY, imgContainerW, imgContainerH);
}
_oContext.drawImage(img, imgContainerX + (imgContainerW - imgW) / 2, imgContainerY +
(imgContainerH - imgH) / 2, imgW, imgH);

Expand Down Expand Up @@ -1566,6 +1595,8 @@ function QRCode(vOption) {
logo: undefined,
logoWidth: undefined,
logoHeight: undefined,
logoMaxWidth: undefined,
logoMaxHeight: undefined,
logoBackgroundColor: '#ffffff',
logoBackgroundTransparent: false,

Expand Down
4 changes: 2 additions & 2 deletions index.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easyqrcodejs-nodejs",
"version": "4.2.7",
"version": "4.3.0",
"description": "NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)",
"main": "index.min.js",
"scripts": {},
Expand Down
20 changes: 12 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ var qrcode = new QRCode(options);

// ====== Logo
/*
logo:"../demo/logo.png", // Relative address, relative to `easy.qrcode.min.js`
logo:"http://127.0.0.1:8020/easy-qrcodejs/demo/logo.png",
logoWidth:80, // width. default is automatic width
logoHeight:80, // height. default is automatic height
logoBackgroundColor:'#fffff', // Logo backgroud color, Invalid when `logBgTransparent` is true; default is '#ffffff'
logoBackgroundTransparent:false, // Whether use transparent image, default is false
logo: "../demo/logo.png", // Relative address, relative to `easy.qrcode.min.js`
logo: "http://127.0.0.1:8020/easy-qrcodejs/demo/logo.png",
logoWidth: 80, // fixed logo width. default is `width/3.5`
logoHeight: 80, // fixed logo height. default is `heigth/3.5`
logoMaxWidth: undefined, // Maximum logo width. if set will ignore `logoWidth` value
logoMaxHeight: undefined, // Maximum logo height. if set will ignore `logoHeight` value
logoBackgroundColor: '#fffff', // Logo backgroud color, Invalid when `logBgTransparent` is true; default is '#ffffff'
logoBackgroundTransparent: false, // Whether use transparent image, default is false
*/

// ====== Backgroud Image
Expand Down Expand Up @@ -279,8 +281,10 @@ var qrcode = new QRCode(options);
| **quietZoneColor** | N | String | `rgba(0,0,0,0)` | Background CSS color to Quiet Zone |
| Logo options| --- | ---|---|---|
| **logo** | N | String | `undefined` | Logo Image Path or Base64 encoded image. If use relative address, relative to `easy.qrcode.min.js` |
| **logoWidth** | N | Number | `undefined` | Height |
| **logoHeight** | N | Number | `undefined` | Width |
| **logoWidth** | N | Number | `width/3.5` | Fixed logo width. |
| **logoHeight** | N | Number | `height/3.5` | fixed logo height. |
| **maxLogoWidth** | N | Number | `undefined` | Maximum logo width. if set will ignore `logoWidth` value. |
| **maxLogoHeight** | N | Number | `undefined` | Maximum logo height. if set will ignore `logoHeight` value. |
| **logoBackgroundTransparent** | N | Boolean | `false` | Whether the background transparent image(`PNG`) shows transparency. When `true`, `logoBackgroundColor` is invalid |
| **logoBackgroundColor** | N | String | `#ffffff` | Set Background CSS Color when image background transparent. Valid when `logoBackgroundTransparent` is `false` |
| Backgroud Image options| ---|--- |---|---|
Expand Down

0 comments on commit fc3abf5

Please sign in to comment.