Skip to content

Commit aba0858

Browse files
leomp12claude
andcommitted
fix(tiny-erp): Calculate and save proper image dimensions for product pictures
Detect original image dimensions using image-size library and calculate proportional sizes for each thumbnail variant (zoom, big, normal, small) based on API response max-px values. Maintains aspect ratio and improves image metadata accuracy. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b7c84c8 commit aba0858

File tree

3 files changed

+59
-69
lines changed

3 files changed

+59
-69
lines changed

packages/apps/tiny-erp/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"@ecomplus/utils": "1.5.0-rc.6",
3131
"axios": "^1.12.2",
3232
"firebase-admin": "^13.5.0",
33-
"firebase-functions": "^6.4.0"
33+
"firebase-functions": "^6.4.0",
34+
"image-size": "^2.0.2"
3435
},
3536
"devDependencies": {
3637
"@cloudcommerce/types": "workspace:*",

packages/apps/tiny-erp/src/integration/parsers/product-from-tiny.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import api from '@cloudcommerce/api';
44
import axios from 'axios';
55
import ecomUtils from '@ecomplus/utils';
66
import getEnv from '@cloudcommerce/firebase/lib/env';
7+
import { imageSize } from 'image-size';
8+
9+
const tryImageSize = (data: any) => {
10+
try {
11+
const buffer = Buffer.from(data);
12+
const dimensions = imageSize(buffer);
13+
return dimensions;
14+
} catch {
15+
return null;
16+
}
17+
};
718

819
const removeAccents = (str: string) => str.replace(/áàãâÁÀÃÂ/g, 'a')
920
.replace(/éêÉÊ/g, 'e')
@@ -37,6 +48,7 @@ const tryImageUpload = async (
3748
responseType: 'arraybuffer',
3849
timeout: 20000,
3950
});
51+
const dimensions = tryImageSize(data);
4052
const formData = new FormData();
4153
formData.append('file', new Blob([data]), originImgUrl.replace(/.*\/([^/]+)$/, '$1'));
4254
const {
@@ -51,18 +63,46 @@ const tryImageUpload = async (
5163
timeout: 60000,
5264
});
5365
if (picture) {
54-
Object.keys(picture).forEach((imgSize) => {
55-
if (picture[imgSize]) {
56-
if (!picture[imgSize].url) {
57-
delete picture[imgSize];
58-
return;
66+
if (dimensions?.width && dimensions.height) {
67+
const { width: w, height: h } = dimensions;
68+
if (picture.zoom) {
69+
picture.zoom.size = `${w}x${h}`;
70+
}
71+
Object.keys(picture).forEach((imgSize) => {
72+
if (picture[imgSize]) {
73+
if (!picture[imgSize].url) {
74+
delete picture[imgSize];
75+
return;
76+
}
77+
const maxPx = picture[imgSize].size;
78+
if (maxPx > 0) {
79+
if (maxPx >= Math.max(w, h)) {
80+
picture[imgSize].size = `${w}x${h}`;
81+
} else {
82+
picture[imgSize].size = w > h
83+
? `${maxPx}x${Math.round((h * maxPx) / w)}`
84+
: `${Math.round((w * maxPx) / h)}x${maxPx}`;
85+
}
86+
} else if (picture[imgSize].size !== undefined) {
87+
delete picture[imgSize].size;
88+
}
89+
picture[imgSize].alt = `${product.name} (${imgSize})`;
5990
}
60-
if (picture[imgSize].size !== undefined) {
61-
delete picture[imgSize].size;
91+
});
92+
} else {
93+
Object.keys(picture).forEach((imgSize) => {
94+
if (picture[imgSize]) {
95+
if (!picture[imgSize].url) {
96+
delete picture[imgSize];
97+
return;
98+
}
99+
if (picture[imgSize].size !== undefined) {
100+
delete picture[imgSize].size;
101+
}
102+
picture[imgSize].alt = `${product.name} (${imgSize})`;
62103
}
63-
picture[imgSize].alt = `${product.name} (${imgSize})`;
64-
}
65-
});
104+
});
105+
}
66106
if (Object.keys(picture).length) {
67107
return {
68108
_id: ecomUtils.randomObjectId(),

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)