nuxt-og-image 2.0.0-beta.53 → 2.0.0-beta.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
1
2
|
import { withBase } from "ufo";
|
|
3
|
+
import sizeOf from "image-size";
|
|
2
4
|
import { defineSatoriTransformer } from "../utils.mjs";
|
|
3
5
|
import { readPublicAssetBase64, toBase64Image } from "../../../utils.mjs";
|
|
4
6
|
export default defineSatoriTransformer({
|
|
@@ -8,8 +10,10 @@ export default defineSatoriTransformer({
|
|
|
8
10
|
if (src && src.startsWith("/")) {
|
|
9
11
|
let updated = false;
|
|
10
12
|
const file = await readPublicAssetBase64(src);
|
|
13
|
+
let dimensions;
|
|
11
14
|
if (file) {
|
|
12
|
-
node.props.src = file;
|
|
15
|
+
node.props.src = file.src;
|
|
16
|
+
dimensions = { width: file.width, height: file.height };
|
|
13
17
|
updated = true;
|
|
14
18
|
}
|
|
15
19
|
if (!updated) {
|
|
@@ -22,9 +26,22 @@ export default defineSatoriTransformer({
|
|
|
22
26
|
});
|
|
23
27
|
if (valid) {
|
|
24
28
|
node.props.src = toBase64Image(src, response);
|
|
29
|
+
const imageSize = await sizeOf(Buffer.from(response));
|
|
30
|
+
dimensions = { width: imageSize.width, height: imageSize.height };
|
|
25
31
|
updated = true;
|
|
26
32
|
}
|
|
27
33
|
}
|
|
34
|
+
if (dimensions?.width && dimensions?.height) {
|
|
35
|
+
const naturalAspectRatio = dimensions.width / dimensions.height;
|
|
36
|
+
if (node.props.width && !node.props.height) {
|
|
37
|
+
node.props.height = Math.round(node.props.width / naturalAspectRatio);
|
|
38
|
+
} else if (node.props.height && !node.props.width) {
|
|
39
|
+
node.props.width = Math.round(node.props.height * naturalAspectRatio);
|
|
40
|
+
} else if (!node.props.width && !node.props.height) {
|
|
41
|
+
node.props.width = dimensions.width;
|
|
42
|
+
node.props.height = dimensions.height;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
28
45
|
if (!updated) {
|
|
29
46
|
node.props.src = `${withBase(src, `${options.requestOrigin}`)}?${Date.now()}`;
|
|
30
47
|
}
|
|
@@ -9,6 +9,10 @@ export declare function wasmLoader(asyncModuleLoad: Promise<any> | Buffer | stri
|
|
|
9
9
|
export declare function fetchOptions(e: H3Event, path: string): Promise<RuntimeOgImageOptions>;
|
|
10
10
|
export declare function base64ToArrayBuffer(base64: string): ArrayBuffer;
|
|
11
11
|
export declare function readPublicAsset(file: string, encoding?: BufferEncoding): Promise<string | Buffer | undefined>;
|
|
12
|
-
export declare function readPublicAssetBase64(file: string): Promise<
|
|
12
|
+
export declare function readPublicAssetBase64(file: string): Promise<{
|
|
13
|
+
src: string;
|
|
14
|
+
width?: number;
|
|
15
|
+
height?: number;
|
|
16
|
+
} | undefined>;
|
|
13
17
|
export declare function toBase64Image(fileName: string, data: string | ArrayBuffer): string;
|
|
14
18
|
export * from './utils-pure';
|
|
@@ -3,6 +3,7 @@ import { Buffer } from "node:buffer";
|
|
|
3
3
|
import { getQuery } from "h3";
|
|
4
4
|
import { join } from "pathe";
|
|
5
5
|
import { prefixStorage } from "unstorage";
|
|
6
|
+
import sizeOf from "image-size";
|
|
6
7
|
import { useRuntimeConfig, useStorage } from "#imports";
|
|
7
8
|
export * from "./util-hostname.mjs";
|
|
8
9
|
export function wasmLoader(asyncModuleLoad, fallback) {
|
|
@@ -84,8 +85,13 @@ export async function readPublicAsset(file, encoding) {
|
|
|
84
85
|
}
|
|
85
86
|
export async function readPublicAssetBase64(file) {
|
|
86
87
|
const base64 = await readPublicAsset(file, "base64");
|
|
87
|
-
if (base64)
|
|
88
|
-
|
|
88
|
+
if (base64) {
|
|
89
|
+
const dimensions = await sizeOf(Buffer.from(base64, "base64"));
|
|
90
|
+
return {
|
|
91
|
+
src: toBase64Image(file, base64),
|
|
92
|
+
...dimensions
|
|
93
|
+
};
|
|
94
|
+
}
|
|
89
95
|
}
|
|
90
96
|
export function toBase64Image(fileName, data) {
|
|
91
97
|
const base64 = typeof data === "string" ? data : Buffer.from(data).toString("base64");
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-og-image",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.55",
|
|
5
5
|
"packageManager": "pnpm@8.6.0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": "https://github.com/sponsors/harlan-zw",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"flatted": "^3.2.7",
|
|
40
40
|
"fs-extra": "^11.1.1",
|
|
41
41
|
"globby": "^13.1.4",
|
|
42
|
+
"image-size": "^1.0.2",
|
|
42
43
|
"inline-css": "^4.0.2",
|
|
43
44
|
"launch-editor": "^2.6.0",
|
|
44
45
|
"nuxt-icon": "^0.4.1",
|