我正在使用sharp来调整写入Node.js/ Expressapplication中的上传图像的大小Typescript.成功调整大小后,我想删除原始文件.对于png和gif输入图像,操作成功终止,我有调整大小的图像,原始文件被删除.对于jpg和tif图像,调整大小成功,但unlink命令失败并出现以下错误:
EBUSY:资源繁忙或已锁定,取消链接'...'
仿佛sharp().resize()仍然保持锁定输入文件,甚至调整大小操作完成后.
以下是测试所描述行为的代码:
import { existsSync, unlinkSync } from "fs";
import { normalize, parse } from "path";
var argv = require("yargs").argv;
var sharp = require("sharp");
var appRoot = require("app-root-path") + "/";
let resizeTest = async function (filename: string): Promise<boolean> {
try {
let nameParts = parse(filename);
let source = appRoot + filename;
let destination = appRoot + nameParts.name + …Run Code Online (Sandbox Code Playgroud) 我正在开发 Imgix 的类似服务,并且正在使用 Sharp。
\n\n但Imgix的webp无损压缩比Sharp获得更好的效果。相同宽度和高度的相同图像在 Imgix 中为 453\xc2\xa0KB,而在 Sharp 中为 1.3\xc2\xa0MB。
\n\n有什么建议可以在不损失质量的情况下增加压缩吗?
\n\n我正在使用的代码:
\n\nhttps.get(url, function (response) {\n let transform = sharp().toFormat('webp').resize(width, height);\n return response.pipe(transform).webp({lossless:true}).pipe(res);\n});\nRun Code Online (Sandbox Code Playgroud)\n 我正在将我的NodeJs应用程序部署到Heroku。在本地使用nodemon运行应用程序没有问题。
但是,当我尝试使用Heroku Master时,似乎是“ Sharp ”软件包引起了问题:
远程:----->安装依赖项
远程:安装节点模块(package.json + package-lock)
远程:
远程:> sharp@0.22.0 install / tmp / build_4cfeda2379ea362c72ac083b57638cce / node_modules / sharp
远程:>(节点安装/ libvips和&节点安装/ dll复制和&预构建安装)(node-gyp重建&&节点安装/ dll-copy)
远程:
远程:信息清晰下载 https://github.com/lovell/sharp-libvips/releases/download/v8.7.4/libvips-8.7.4-linux-x64.tar.gz
远程:
/tmp/build_4cfeda2379ea362c72ac083b57638cce/node_modules/sharp/install/libvips.js : 86远程:抛出新的Error(
Status ${response.statusCode});远程:^
远程:
远程:错误:状态403
远程:在/tmp/build_4cfeda2379ea362c72ac083b57638cce/node_modules/sharp/install/libvips.js:86:17
远程:在f(/tmp/build_4cfeda2379ea362c72ac083b57638cce/node_modules/once/once.js:25:25)
远程:位于ClientRequest.protocol.request.res(/tmp/build_4cfeda2379ea362c72ac083b57638cce/node_modules/simple-get/index.js:63:5)
远程:位于Object.onceWrapper(events.js:277:13)
远程:在ClientRequest.emit(events.js:189:13)
远程:位于HTTPParser.parserOnIncomingClient [as onIncoming](_http_client.js:556:21)
远程:位于HTTPParser.parserOnHeadersComplete(_http_common.js:109:17)
远程:位于TLSSocket.socketOnData(_http_client.js:442:20)
远程:位于TLSSocket.emit(events.js:189:13)
远程:在addChunk(_stream_visible.js:284:12)
远程:make:输入目录'/ tmp / build_4cfeda2379ea362c72ac083b57638cce / node_modules / sharp / build'
远程:TOUCH Release / obj.target / libvips-cpp.stamp
远程:CXX(目标)发行版/obj.target/sharp/src/common.o
远程:../src/common.cc:25:10:致命错误:vips / vips8:没有这样的文件或目录
远程:#include
远程的:^ ~~~~~~~~~~~~
远程:编译终止。
远程:Sharp.target.mk:128:目标'Release / obj.target / sharp …
我无法使用sudo npm install sharp.
终端输出:
sharp@0.23.1 install /Users/kozakiewicz/node_modules/sharp (node install/libvips && node install/dll-copy && prebuild-install) || (节点gyp重建&&节点安装/dll-copy)
info sharp Using cached /Users/kozakiewicz/.npm/_libvips/libvips-8.8.1-darwin-x64.tar.gz
ERR! sharp EACCES: permission denied, mkdir '/Users/kozakiewicz/node_modules/sharp/vendor'
info sharp Attempting to build from source via node-gyp but this may fail due to the above error
info sharp Please see https://sharp.pixelplumbing.com/page/install for required dependencies
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/Users/kozakiewicz/node_modules/sharp/build'
gyp ERR! System Darwin 18.7.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Node.js API 中的 @aws-sdk/v3 调整从 S3 上传的图像的大小。
首先,我从 S3 获取对象(图像),如下所示: https: //github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascriptv3/example_code/s3/src/s3_getobject.ts
...
const params = { Bucket: AWS_BUCKET_NAME, Key: key }
const data = await s3.send(new GetObjectCommand(params))
console.log(` >> Success, bucket returned:`, data) // Log correctly
Run Code Online (Sandbox Code Playgroud)
然后我尝试调整数据(图像)的大小:
const convert = sharp(data).resize(size, size).webp()
Run Code Online (Sandbox Code Playgroud)
但它抛出:
>> data.type: object
(node:35583) UnhandledPromiseRejectionWarning: Error: Input file is missing
...
Run Code Online (Sandbox Code Playgroud)
而且我不知道该如何上传回来?
我究竟做错了什么?
我正在尝试在我的公司系统上使用 Next.js,但是每当我运行“npm run build”时,构建都会失败并显示以下消息: Error: Cannot find module 'sharp'
我尝试安装sharp,但出现此错误:
info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.10.5/libvips-8.10.5-win32-x64.tar.br
ERR! sharp getaddrinfo ENOTFOUND github.com
info sharp Attempting to build from source via node-gyp but this may fail due to the above error
info sharp Please see https://sharp.pixelplumbing.com/install for required dependencies
Run Code Online (Sandbox Code Playgroud)
这可能是因为出于安全原因,我的系统终端上阻止了 github URL。有没有办法在不从github中获取的情况下安装sharp?我可以在浏览器上访问 GitHub,所以如果从那里下载文件然后以某种方式安装它是可能的,我可以这样做。
操作系统:Windows 10
sharp当我从包(即图像处理包)添加这段代码时:
await sharp(req.file.path)\n .resize(500)\n .jpeg({ quality: 50 })\n .toFile(path.resolve(req.file.destination, \xe2\x80\x9cresized\xe2\x80\x9d, filename));\nRun Code Online (Sandbox Code Playgroud)\n我收到此错误:
\nIllegal instruction (core dumped)\nRun Code Online (Sandbox Code Playgroud)\n当我删除它时,一切都正常。我\xe2\x80\x99m很困惑。该代码有什么问题?
\n我在为 Dockerfile 实现 Sharp 时遇到问题。
Error: 'sharp' is required to be installed in standalone mode for the image
optimization to function correctly
Run Code Online (Sandbox Code Playgroud)
Next.js 和 Sharp 非常适合本地开发:
next.config.js
module.exports = {
experimental: {
outputStandalone: true,
},
Run Code Online (Sandbox Code Playgroud)
}
Dockerfile:
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# …Run Code Online (Sandbox Code Playgroud) 'win32-x64' 二进制文件不能在 'linux-x64' 平台上使用,在 Windows 系统上安装 node 时出现此错误。这在执行时破坏了我在 aws 上的 lambda 代码。
我曾经使用本地安装的 Node.js v16.x 来运行 Strapi,效果很好。
但最近由于另一个项目,我不得不将其更新到 Node.js v18.x,因此我安装了 NVM 来管理我的 PC 上的多个 Node.js 版本 - 现在我想将 Strapi 与新的 Node.js 一起使用,但我可以甚至不再启动开发模式。夏普的包装有问题。
我不确定 Sharp 包的问题是否是由于新的 Node 版本或 NVM 引起的。由于安装的 Node (v18.17.0) 在 Shap 包 npm 页面(此处)上被列为兼容,我猜问题出在 NVM 上。
nvm list
* 18.17.0 (Currently using 64-bit executable)
16.14.2
Run Code Online (Sandbox Code Playgroud)
我的依赖项:
"dependencies": {
"@strapi/plugin-i18n": "4.16.2",
"@strapi/plugin-users-permissions": "4.16.2",
"@strapi/provider-email-amazon-ses": "4.16.2",
"@strapi/provider-upload-aws-s3": "4.16.2",
"@strapi/strapi": "4.16.2",
"@strapi/utils": "4.16.2",
"pg": "8.11.3",
"reach": "1.0.1",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-router-dom": "5.2.0",
"sharp": "^0.33.1",
"slugify": "1.6.6",
"strapi-plugin-config-sync": "1.2.3",
"strapi-plugin-email-designer": "2.2.1", …Run Code Online (Sandbox Code Playgroud) sharp ×10
node.js ×8
next.js ×2
amazon-s3 ×1
docker ×1
heroku ×1
javascript ×1
nextjs-image ×1
npm ×1
nvm ×1
reactjs ×1
strapi ×1
typescript ×1
unlink ×1