Skip to content

Commit fc3abf5

Browse files
committed
Add maxLogoWidth&maxLogoHeight options
Add maxLogoWidth&maxLogoHeight options
1 parent 98b85e0 commit fc3abf5

File tree

4 files changed

+63
-28
lines changed

4 files changed

+63
-28
lines changed

index.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* 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)
55
*
6-
* Version 4.2.7
6+
* Version 4.3.0
77
*
88
* @author [ [email protected] ]
99
*
@@ -1285,7 +1285,7 @@ Drawing.prototype.draw = function(oQRCode) {
12851285
nowDotScale = _htOption.dotScaleAO;
12861286
} else if (type == 'AI') {
12871287
nowDotScale = _htOption.dotScaleAI;
1288-
}else {
1288+
} else {
12891289
nowDotScale = 1;
12901290
}
12911291

@@ -1374,26 +1374,18 @@ Drawing.prototype.draw = function(oQRCode) {
13741374
imgContainerW = imgContainerH;
13751375
}
13761376

1377-
if (_htOption.logoWidth) {
1377+
if (_htOption.logoMaxWidth) {
1378+
imgContainerW = Math.round(_htOption.logoMaxWidth);
1379+
} else if (_htOption.logoWidth) {
13781380
imgContainerW = Math.round(_htOption.logoWidth);
13791381
}
1380-
if (_htOption.logoHeight) {
1382+
1383+
if (_htOption.logoMaxHeight) {
1384+
imgContainerH = Math.round(_htOption.logoMaxHeight);
1385+
} else if (_htOption.logoHeight) {
13811386
imgContainerH = Math.round(_htOption.logoHeight);
13821387
}
13831388

1384-
var imgContainerX = (_htOption.width + _htOption.quietZone * 2 - imgContainerW) / 2;
1385-
var imgContainerY = (_htOption.height + _htOption.titleHeight + _htOption.quietZone *
1386-
2 - imgContainerH) / 2;
1387-
1388-
// Did Not Use Transparent Logo Image
1389-
if (!_htOption.logoBackgroundTransparent) {
1390-
//if (!_htOption.logoBackgroundColor) {
1391-
//_htOption.logoBackgroundColor = '#ffffff';
1392-
//}
1393-
_oContext.fillStyle = _htOption.logoBackgroundColor;
1394-
1395-
_oContext.fillRect(imgContainerX, imgContainerY, imgContainerW, imgContainerH);
1396-
}
13971389
var nw;
13981390
var nh;
13991391
if (typeof img.naturalWidth == "undefined") {
@@ -1406,10 +1398,47 @@ Drawing.prototype.draw = function(oQRCode) {
14061398
nh = img.naturalHeight;
14071399
}
14081400

1401+
if (_htOption.logoMaxWidth || _htOption.logoMaxHeight) {
1402+
if (_htOption.logoMaxWidth && nw <= imgContainerW) {
1403+
imgContainerW = nw;
1404+
}
1405+
1406+
if (_htOption.logoMaxHeight && nh <= imgContainerH) {
1407+
imgContainerH = nh;
1408+
}
1409+
if (nw <= imgContainerW && nh <= imgContainerH) {
1410+
imgContainerW = nw;
1411+
imgContainerH = nh;
1412+
}
1413+
}
1414+
1415+
var imgContainerX = (_htOption.width + _htOption.quietZone * 2 - imgContainerW) / 2;
1416+
var imgContainerY = (_htOption.height + _htOption.titleHeight + _htOption.quietZone *
1417+
2 - imgContainerH) / 2;
1418+
14091419
var imgScale = Math.min(imgContainerW / nw, imgContainerH / nh);
14101420
var imgW = nw * imgScale;
14111421
var imgH = nh * imgScale;
14121422

1423+
if (_htOption.logoMaxWidth || _htOption.logoMaxHeight) {
1424+
imgContainerW = imgW;
1425+
imgContainerH = imgH;
1426+
imgContainerX = (_htOption.width + _htOption.quietZone * 2 - imgContainerW) / 2;
1427+
imgContainerY = (_htOption.height + _htOption.titleHeight + _htOption
1428+
.quietZone *
1429+
2 - imgContainerH) / 2;
1430+
1431+
}
1432+
1433+
// Did Not Use Transparent Logo Image
1434+
if (!_htOption.logoBackgroundTransparent) {
1435+
//if (!_htOption.logoBackgroundColor) {
1436+
//_htOption.logoBackgroundColor = '#ffffff';
1437+
//}
1438+
_oContext.fillStyle = _htOption.logoBackgroundColor;
1439+
1440+
_oContext.fillRect(imgContainerX, imgContainerY, imgContainerW, imgContainerH);
1441+
}
14131442
_oContext.drawImage(img, imgContainerX + (imgContainerW - imgW) / 2, imgContainerY +
14141443
(imgContainerH - imgH) / 2, imgW, imgH);
14151444

@@ -1566,6 +1595,8 @@ function QRCode(vOption) {
15661595
logo: undefined,
15671596
logoWidth: undefined,
15681597
logoHeight: undefined,
1598+
logoMaxWidth: undefined,
1599+
logoMaxHeight: undefined,
15691600
logoBackgroundColor: '#ffffff',
15701601
logoBackgroundTransparent: false,
15711602

index.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "easyqrcodejs-nodejs",
3-
"version": "4.2.7",
3+
"version": "4.3.0",
44
"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)",
55
"main": "index.min.js",
66
"scripts": {},

readme.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@ var qrcode = new QRCode(options);
174174

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

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

0 commit comments

Comments
 (0)