fix: let sharp be optional

This commit is contained in:
ikechan8370 2023-02-25 15:58:16 +08:00
parent 5fceef37ec
commit 94c655367e
4 changed files with 15 additions and 2 deletions

0
.npmrc Normal file
View file

View file

@ -72,7 +72,11 @@ pnpm i
> 3. 使用vnc客户端连接至云桌面
>
> 右键Applications > Shells > Bash打开终端然后进入Yunzai目录下运行node app即可。
>
> 4. 执行pnpm i时sharp安装失败
>
> sharp不影响chatgpt聊天仅影响Dalle2绘图功能。ubuntu可以执行`apt install libvips-dev`之后再`pnpm i`
>
> 实测该方案资源占用低运行稳定基本1核2G的轻量云服务器就足够了。
---

View file

@ -13,8 +13,10 @@
"puppeteer-extra-plugin-recaptcha": "^3.6.6",
"puppeteer-extra-plugin-stealth": "^2.11.1",
"random": "^4.1.0",
"sharp": "^0.31.3",
"undici": "^5.19.1",
"uuid": "^9.0.0"
},
"optionalDependencies": {
"sharp": "^0.31.3"
}
}

View file

@ -2,7 +2,7 @@ import { Configuration, OpenAIApi } from 'openai'
import { Config } from './config.js'
import fs from 'fs'
import { mkdirs } from './common.js'
import sharp from 'sharp'
export async function createImage (prompt, n = 1, size = '512x512') {
const configuration = new Configuration({
apiKey: Config.apiKey
@ -56,6 +56,13 @@ export async function imageVariation (imageUrl, n = 1, size = '512x512') {
async function resizeAndCropImage (inputFilePath, outputFilePath, size = 512) {
// Determine the maximum dimension of the input image
let sharp
try {
sharp = await import('sharp')
} catch (e) {
logger.error('sharp未安装请执行 pnpm install sharp@0.31.3')
throw new Error('sharp未安装请执行 pnpm install sharp@0.31.3')
}
const metadata = await sharp(inputFilePath).metadata()
const maxDimension = Math.max(metadata.width, metadata.height)
logger.mark(`original picture size is ${metadata.width} x ${metadata.height}`)