标签: sharp

为什么即使我更改了输入文件,Sharp 仍输出相同的文件?

使用 Sharp 对 JPG 进行缩放和着色后,我立即删除输入文件。当我上传同名的新文件时,Sharp 将输出旧文件。我在 Ubuntu 16.04 上运行 NodeJS。

这是编辑文件的代码:

sampleFile.mv(__dirname + "/" + name + "." + ext, function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');

    if (ext != "xlsx") {
      // This will attempt to resize the images
      console.log("Sharpening image")
      sharp(__dirname + "/" + name + "." + ext).resize({ height: 27 }).flatten( { background: '#ffffff' } ).toFile("/var/www/my_ip/file.jpg")
        .then(function(newFileInfo) {
          // newFileInfo holds the output file properties
          console.log("Success")
          try {
            fileSystem.unlinkSync(__dirname + "/" + name + "." + …
Run Code Online (Sandbox Code Playgroud)

javascript ubuntu node.js sharp

7
推荐指数
1
解决办法
3193
查看次数

NodeJS Sharp 节点包内存消耗问题

我正在开发一个用于sharp node package调整 JPEG/JPG 图像大小的 NodeJS 项目。

问题是Node进程不断地将处理后的文件添加到内存中并且从不释放它。

因此,消耗的内存量随着每个请求而增加,并且永远不会被释放。

调试应用程序后,我意识到Sharp toBuffer API导致了该问题。

我尝试通过创建 acustom writable stream并用管道来使用替代解决方案sharp Duplex stream,但最终遇到了同样的问题。

我不明白我是否在这里遗漏了任何东西或者这是一个错误。

分享下面的代码(我删除了不需要的代码以使其紧凑)-

const { Writable } = require("stream");
const { createServer } = require("http");
const { readFileSync } = require("fs");
const sharp = require("sharp");

async function resizeJpeg(input_buffer) {
    // initialise file, the response object
    const file = { normalisedImage: null, originalImage: null };
    // initialise sharp instance using original image buffer
    let image = …
Run Code Online (Sandbox Code Playgroud)

memory-leaks image-processing node.js sharp

7
推荐指数
1
解决办法
2789
查看次数

NodeJS 内存消耗统计问题 - 巨大的 RSS 和外部内存使用量

在为 Node 项目进行性能测试时,我遇到了一些内存报告,这些报告在性能和内存泄漏方面的解释有点令人困惑。

背景

因此,我使用process.memoryUsage() API生成了报告,该 API 提供了有关以下信息的信息 -

  • RSS - 总分配内存的子集,包括堆、代码段和堆栈
  • heapTotal - 分配给对象、字符串和闭包的内存
  • heapUsed - 对象、字符串和闭包使用的实际内存
  • external - 绑定到 V8 管理的 JavaScript 对象的 C++ 对象的内存使用

问题

我在每次向服务器发出请求后记录了内存快照,并绘制了 100 个请求的图表 -

图表显示以下信息 -

  1. RSS内存随着服务的每个请求而不断增加
  2. External Memory相比用法是显著Heap Memory
  3. External Memory一旦进程空闲,它就会关闭
  4. Total HeapUsed Heap受到控制的记忆是很好

发现

根据我的理解,Heap 之外的某些东西导致内存上升。

我正在使用sharp node package它调整大量大图像的大小,最终为这些图像生成巨大的内存缓冲区。

在内存方面,每个请求都提供 10 张图像,最终生成 80 MB 的缓冲区数据

这是我遇到这个 github 问题的地方,讨论了在 NodeJs 中创建缓冲区的影响

这个答案在同一个 github 问题中,解释了缓冲区如何可能占用garbage collector …

buffer garbage-collection memory-leaks node.js sharp

7
推荐指数
0
解决办法
1038
查看次数

如何使用选项“包含”调整图像大小但保留原始尺寸的纵横比?

我有一个 1000 像素 x 750 像素的图像。

我使用 lovell/sharp 来调整它的大小:

await sharp(image)
    .resize({
        fit: sharp.fit.contain,
        width: 800,
        height: 800
    })
    .jpeg({ quality: 80 })
    .toBuffer()
Run Code Online (Sandbox Code Playgroud)

这会生成一个 800 像素 x 800 像素的新图像,原始图像“包含”在该区域内。

我真正想要的是有一个 800 像素 x 600 像素的最终图像。IOW,调整图像大小并保持纵横比。

我意识到可以通过仅指定宽度来做到这一点。然而,有一个边界框来包含调整大小的图像是有用的,以避免创建大于特定高度的图像。

我可以在不同的设置下清晰地做到这一点吗?

javascript node.js sharp

7
推荐指数
2
解决办法
7507
查看次数

Cordova-res 未安装在 ionic cordova 中

我无法在 ionic 5.4.4v 中安装包 Cordova-res 并收到以下错误

**Z:\my program V2\ionicTest\testProj>npm i -g cordova-res**

C:\Users\USER\AppData\Roaming\npm\cordova-res -> C:\Users\USER\AppData\Roaming\npm\node_modules\cordova-res\bin\cordova-res

> sharp@0.22.1 install C:\Users\USER\AppData\Roaming\npm\node_modules\cordova-res\node_modules\sharp
> (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)

info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.7.4/libvips-8.7.4-win32-x64.tar.gz
C:\Users\USER\AppData\Roaming\npm\node_modules\cordova-res\node_modules\sharp\install\libvips.js:83
          throw err;
          ^

##Error: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:80##
    at ClientRequest.onError (C:\Users\USER\AppData\Roaming\npm\node_modules\cordova-res\node_modules\tunnel-agent\index.js:177:17)
    at Object.onceWrapper (events.js:300:26)
    at ClientRequest.emit (events.js:210:5)
    at Socket.socketErrorListener (_http_client.js:406:9)
    at Socket.emit (events.js:210:5)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) 
{
  code: 'ECONNRESET'
}

C:\Users\USER\AppData\Roaming\npm\node_modules\cordova-res\node_modules\sharp>if …
Run Code Online (Sandbox Code Playgroud)

android node.js cordova ionic-framework sharp

7
推荐指数
1
解决办法
2518
查看次数

尖锐错误:在 pkg-config 搜索路径中找不到 OpenEXR 包

npm install我在Mac上的gatsby js目录上执行。

  • macOS 就是大苏尔

  • 版本是11.4。

  • gatsby版本是3.11.0

  • gatsby-plugin-sharp 版本是 3.11.0

显示此错误日志。

npm ERR! code 1
npm ERR! path /Users/<username>/<path>/node_modules/sharp
npm ERR! command failed
npm ERR! command sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
npm ERR! sharp: Detected globally-installed libvips v8.11.2
npm ERR! sharp: Building from source via node-gyp
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp …
Run Code Online (Sandbox Code Playgroud)

node.js npm npm-install sharp

7
推荐指数
2
解决办法
1703
查看次数

使用 esbuild 为特定平台构建 Sharp 并使用 NodejsFunction lambda 进行部署

我正在尝试部署一个使用 Sharp 进行图像处理的 Lambda 函数。我不想使用 docker 构建——我必须使用 esbuild。这是我在 cdk 中的 lambda 定义:

  const processUploadedImageLambdaFunction = new NodejsFunction(
      this,
      `${deployEnv()}_processUploadedImage`,
      {
        runtime: lambda.Runtime.NODEJS_18_X,
        // handler: "index.handler",
        entry: `${__dirname}/../lambda-fns/lambdas/processUploadedImage/index.ts`,
        bundling: {
          target: "node18",
          sourceMap: true,
          sourceMapMode: SourceMapMode.INLINE,
          sourcesContent: false,
          externalModules: ["sharp"],
          nodeModules: ["sharp"],
          command: ["npm rebuild sharp --arch=x64 --platform=linux sharp"],
        },

        insightsVersion,
        logRetention,
        // memorySize: 10240,
        memorySize: 3008,
        timeout: cdk.Duration.seconds(30),
        environment: {
          ...config,
        },
      },
    )
Run Code Online (Sandbox Code Playgroud)

当函数运行时出现以下异常:

{
    "errorType": "Error",
    "errorMessage": "\nSomething went wrong installing the \"sharp\" module\n\nCannot find module '../build/Release/sharp-linux-x64.node'\nRequire stack:\n- …
Run Code Online (Sandbox Code Playgroud)

aws-lambda sharp esbuild

7
推荐指数
1
解决办法
652
查看次数

(node.js模块)清晰图像处理器保持源文件打开,调整大小后无法取消原始链接

我正在使用sharp来调整写入Node.js/ Expressapplication中的上传图像的大小Typescript.成功调整大小后,我想删除原始文件.对于pnggif输入图像,操作成功终止,我有调整大小的图像,原始文件被删除.对于jpgtif图像,调整大小成功,但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)

unlink node.js typescript sharp

6
推荐指数
1
解决办法
2293
查看次数

Sharp包Heroku部署问题

我正在将我的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 …

heroku node.js sharp

6
推荐指数
1
解决办法
578
查看次数

NodeJS:非法指令(核心转储)使用锐库后出错

sharp当我从包(即图像处理包)添加这段代码时:

\n
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));\n
Run Code Online (Sandbox Code Playgroud)\n

我收到此错误:

\n
Illegal instruction (core dumped)\n
Run Code Online (Sandbox Code Playgroud)\n

当我删除它时,一切都正常。我\xe2\x80\x99m很困惑。该代码有什么问题?

\n

node.js sharp

6
推荐指数
1
解决办法
2431
查看次数