Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
600 changes: 343 additions & 257 deletions apps/ai-image-generator/backend/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/ai-image-generator/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@contentful/node-apps-toolkit": "^2.6.0",
"contentful-management": "^11.6.1",
"openai": "^4.96.2",
"sharp": "^0.33.0"
"sharp": "0.35.4"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as nodeFetch from 'node-fetch';
import { default as sharp } from 'sharp';
import { areEqualColors, constrainDimensions, toDimensions, toRGBA, toSharp } from '../utils';
import { areEqualColors, constrainDimensions, RGBA, toDimensions, toRGBA, toSharp } from '../utils';
import { Dimensions } from '../types';

export const ERASE_COLOR: sharp.RGBA = { r: 231, g: 235, b: 238 };
export const BAR_COLOR: sharp.RGBA = { r: 0, g: 0, b: 0, alpha: 1 };
export const ERASE_COLOR: RGBA = { r: 231, g: 235, b: 238 };
export const BAR_COLOR: RGBA = { r: 0, g: 0, b: 0, alpha: 1 };
export const MAX_SIDE = 1024;

interface PreparedImageData {
Expand Down Expand Up @@ -91,13 +91,12 @@ export class PrepareImageForEdit {
}

private async replaceColorWithTransparent(
colorToReplace: sharp.RGBA,
colorToReplace: RGBA,
sharpImage: sharp.Sharp
): Promise<sharp.Sharp> {
const { data: imagePixels, info } = await sharpImage
.ensureAlpha()
.raw()
.pipe(this.maskImage)
.toBuffer({ resolveWithObject: true });

for (let i = 0; i < imagePixels.length; i += 4) {
Expand Down
13 changes: 10 additions & 3 deletions apps/ai-image-generator/backend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { AppActionCallContext } from '@contentful/node-apps-toolkit';
import { default as sharp } from 'sharp';
import { Dimensions } from './types';

export interface RGBA {
r?: number;
g?: number;
b?: number;
alpha?: number;
}

export interface AreEqualColorOpts {
tolerance?: number;
compareAlpha?: boolean;
Expand Down Expand Up @@ -31,8 +38,8 @@ export const difference = (a: number, b: number): number => {

// note: we don't care about alpha channel here, only the RGB color
export const areEqualColors = (
colorA: sharp.RGBA,
colorB: sharp.RGBA,
colorA: RGBA,
colorB: RGBA,
opts: AreEqualColorOpts = {}
): boolean => {
const { tolerance, compareAlpha } = { tolerance: 0, compareAlpha: false, ...opts };
Expand All @@ -53,7 +60,7 @@ export const areEqualColors = (
return true;
};

export const toRGBA = (rawPixels: Buffer): sharp.RGBA => {
export const toRGBA = (rawPixels: Buffer): RGBA => {
return {
r: rawPixels[0],
g: rawPixels[1],
Expand Down
4 changes: 2 additions & 2 deletions apps/ai-image-generator/backend/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import fs from 'fs';
import path from 'path';
import { Readable } from 'stream';
import { Response } from 'node-fetch';
import { AreEqualColorOpts, areEqualColors, toRGBA } from '../src/utils';
import { AreEqualColorOpts, areEqualColors, RGBA, toRGBA } from '../src/utils';

// a little utility function that computes the proportion of pixels in a given image that equal the provided
// color. useful for testing
export async function findColorProportion(
sharpImage: sharp.Sharp,
color: sharp.RGBA,
color: RGBA,
opts: AreEqualColorOpts = {}
): Promise<number> {
let matchingPixelsFound = 0;
Expand Down
62 changes: 25 additions & 37 deletions apps/ai-image-generator/bundle-sharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,32 @@ const fs = require('fs/promises');

console.log('Copying sharp files from node_modules to build/node_modules');

// sharp is esbuild-external, so its whole runtime closure must be copied by
// hand. Keep in sync with sharp's `dependencies` on every upgrade.
const modules = [
'@img/sharp-libvips-linux-x64',
'@img/sharp-linux-x64',
'@img/colour',
'sharp',
'detect-libc',
'semver',
];

const copySharp = async () => {
await fs.cp(
'./node_modules/@img/sharp-libvips-linux-x64',
'./build/node_modules/@img/sharp-libvips-linux-x64',
{
recursive: true,
for (const mod of modules) {
const src = `./node_modules/${mod}`;
try {
await fs.access(src);
} catch {
throw new Error(
`Expected ${mod} in node_modules but it is missing. A sharp upgrade likely changed its dependencies; update the list in bundle-sharp.js.`
);
}
);
await fs.cp('./node_modules/@img/sharp-linux-x64', './build/node_modules/@img/sharp-linux-x64', {
recursive: true,
});
await fs.cp('./node_modules/sharp', './build/node_modules/sharp', {
recursive: true,
});
await fs.cp('./node_modules/color', './build/node_modules/color', {
recursive: true,
});
await fs.cp('./node_modules/color-convert', './build/node_modules/color-convert', {
recursive: true,
});
await fs.cp('./node_modules/color-name', './build/node_modules/color-name', {
recursive: true,
});
await fs.cp('./node_modules/color-string', './build/node_modules/color-string', {
recursive: true,
});
await fs.cp('./node_modules/detect-libc', './build/node_modules/detect-libc', {
recursive: true,
});
await fs.cp('./node_modules/is-arrayish', './build/node_modules/is-arrayish', {
recursive: true,
});
await fs.cp('./node_modules/semver', './build/node_modules/semver', {
recursive: true,
});
await fs.cp('./node_modules/simple-swizzle', './build/node_modules/simple-swizzle', {
recursive: true,
});
await fs.cp(src, `./build/node_modules/${mod}`, { recursive: true });
}
};

copySharp();
copySharp().catch((err) => {
console.error(err.message);
process.exit(1);
});
Loading
Loading