标签: sharp

多次调整一张图像的大小

我正在将给定的图像(保存在磁盘上)调整为不同的大小:

var image = 'photos/pic';
var sizes = [1440, 1080, 720, 480];

for (var i = 0; i < sizes.length; i++) {
    sharp(image + '.jpg')
    .resize(sizes[i], sizes[i])
    .toFile(image + '-' + sizes[i] + '.jpg');       
}
Run Code Online (Sandbox Code Playgroud)

这按预期工作,但我想还有改进的余地。

  • for 循环会导致任何问题吗?如果是,有没有更好的方法来解决这个问题?
  • 等待生成的图片调整大小并将其用于下一个调整大小过程会更快吗?假设原始图片是2000x2000. 如果有的话,从调整720x720480x480而不是2000x2000到的速度改进480x480是什么?考虑到我必须先读取720x720文件并等待调整大小完成。
  • 我应该在“主”节点线程上调整大小还是分叉子进程?无论如何,它们都是异步运行的,对吗?

node.js sharp

5
推荐指数
1
解决办法
2584
查看次数

尝试为 Gatsby 安装 Sharp 时出错

我刚开始使用 Gatsby。我可以使用 Gatsby 的默认启动器,但任何其他启动器似乎都依赖于 Sharp,并且无论我做什么,它的安装都失败了。

    info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.6.1/libvips-8.6.1-darwin-x64.tar.gz
/Users/djfriar/Sites/gatsby-pretty/node_modules/sharp/install/libvips.js:76
          throw err;
          ^

Error: self signed certificate in certificate chain
    at TLSSocket.onConnectSecure (_tls_wrap.js:1049:34)
    at TLSSocket.emit (events.js:182:13)
    at TLSSocket._finishInit (_tls_wrap.js:631:8)
gyp WARN install got an error, rolling back install
gyp ERR! configure error 
gyp ERR! stack Error: self signed certificate in certificate chain
gyp ERR! stack     at TLSSocket.onConnectSecure (_tls_wrap.js:1049:34)
gyp ERR! stack     at TLSSocket.emit (events.js:182:13)
gyp ERR! stack     at TLSSocket._finishInit (_tls_wrap.js:631:8)
gyp ERR! System Darwin 18.2.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)

gatsby sharp

5
推荐指数
1
解决办法
3019
查看次数

带有锐利的水印

我有一个图像,我在那里放了水印,但我希望水印的不透明度为 30%。

我的代码:

    let sharp = require('sharp');
    let buffer = null;

    await sharp(image)
        .composite([{ input: './logo.png', gravity: 'center' }])
        .sharpen()
        .withMetadata()
        .toBuffer()
        .then(function(outputBuffer) {
            buffer = outputBuffer;
        });
    return buffer;
Run Code Online (Sandbox Code Playgroud)

我怎么能说徽标的不透明度为 30%?

node.js sharp

5
推荐指数
1
解决办法
2916
查看次数

使用 Sharp 调整图像大小时,出现错误 Sharp.resize(...).max 不是函数

我正在尝试使用 调整图像大小sharp,版本是"sharp": "^0.23.0"。我使用了来自 的示例代码functions-samples。我的代码在这里

thumbnailGeneratorSharp: async (object) => {
    const fileBucket = object.bucket; // The Storage bucket that contains the file.
    const filePath = object.name; // File path in the bucket.
    const contentType = object.contentType; // File content type.

    // Exit if this is triggered on a file that is not an image.
    if (!contentType.startsWith('image/')) {
      console.log('This is not an image.');
      return null;
    }

    // Get the file name.
    const fileName = path.basename(filePath);
    // …
Run Code Online (Sandbox Code Playgroud)

node.js google-cloud-functions sharp

4
推荐指数
1
解决办法
4231
查看次数

错误:darwin-x64' 二进制文件无法在 'linux-x64' 平台上使用(AWS lambda + typescript + webpack Sharp 模块)

通过 webpack 捆绑时,带有 typescript 的 aws lambda 发生错误。

在捆绑 webpack 之前,我做了“npm i --arch=x64 --platform=linux --target=12.14.1 Sharp”并且 labmda 工作正常。

但是,lambda 上传的 zip 大小越来越大。

所以,我想使用 serverless-webpack 调整 lambda 上传 zip 大小。

除了使用 Sharp 模块之外,图像 lambda 运行良好。

我不知道该怎么办。

我做了

  1. 删除node_modules和package-lock.json并安装依赖项(也安装了sharp)
  2. 删除node_modules/sharp并安装sharp(lambda环境-linux,x64,node版本)
  3. 在 serverless 中设置 serverless-webpack 配置:packagerOptions ( scrips ) - 重建 Sharp lambda 环境

但是,lambda 无法正常工作。

而且我查了很多资料。

[lambda linux env]
在 AWS Lambda 函数内运行 Sharp 时出错:darwin-x64' 二进制文件无法在 'linux-x64' 平台上使用

[Serverless-webpack]
https://github.com/serverless-heaven/serverless-webpack/问题/396

谢谢你!


[编辑]

我的本地环境:Mac

生产环境:linux

也许,我认为带有“--platform”的 npm 命令在 mac 中不起作用。

此外,我使用 aws …

node.js webpack aws-lambda sharp

4
推荐指数
2
解决办法
4996
查看次数

Google Cloud Functions 错误:“找不到模块‘sharp’”,但它在我的 package.json 中

我正在尝试将函数部署到 Google Cloud Functions。我基于他们的 ImageMagick教程

每次,该函数都会因为出现错误而无法部署。查看日志,错误是:

    Provided module can't be loaded. 
    Did you list all required modules in the package.json dependencies? 
    Detailed stack trace: 
    Error: Cannot find module 'sharp' 
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会发生这种情况,因为这sharp 我的package.json依赖关系。如果我在 Google Cloud 控制台中打开该函数的 Web 编辑器,该函数package.json会作为文件之一存在并显示sharp为依赖项。我尝试运行npm installnpm install --save重新部署,但这并没有解决任何问题。

我将包包含在函数中const sharp = require('sharp');(这是日志显示发生错误的行),这是我的package.json

{
  "name": "Resize images",
  "version": "0.0.1",
  "private": true,
  "author": "James Tyner",
  "engines": {
    "node": ">=10.0.0"
  },
  "dependencies": {
    "@google-cloud/storage": …
Run Code Online (Sandbox Code Playgroud)

node.js npm google-cloud-functions sharp

4
推荐指数
1
解决办法
6388
查看次数

使用 Nodejs Sharp 模块时出错。模块解析失败:意外字符 '' (1: 0)

运行 webpack 时,我收到此错误“WARNING in ./node_modules/sharp/build/Release/sharp.node 1: 0 Module parse failed: Unexpected character '' (1: 0) You may need an适当的加载器来处理此文件类型,目前没有配置加载器来处理此文件。请参阅https://webpack.js.org/concepts#loaders (此二进制文件省略源代码)@ ./node_modules/sharp/lib/constructor.js 11: 2 -40 @ ./node_modules/sharp/lib/index.js ”

夏普与纱线添加一起安装。我的操作系统是 ubuntu 18.04 x86_64。我怎么解决这个问题?

node.js webpack aws-lambda sharp

4
推荐指数
1
解决办法
2853
查看次数

安装“sharp”模块时出错

我已经按照错误所说的做了。然后我把锐器取下来,npm install --ignore-scripts=false --verbose但它仍然不起作用。

我也尝试过:

rimraf node_modules 
rimraf yarn.lock 
yarn
Run Code Online (Sandbox Code Playgroud)

这里可能出现什么问题?

npm:6.13.4

节点:v10.18.0

蟒蛇:3.7.0

锐利:^0.26.2

Windows 10 专业版 64 位

!  Error: 
Something went wrong installing the "sharp" module      

\\?\C:\Users\ph2200009\Desktop\TM\cplus_incident_mgnt\functions\node_modules\sharp\build\Release\sharp.node is not a valid Win32 application.
\\?\C:\Users\ph2200009\Desktop\TM\cplus_incident_mgnt\functions\node_modules\sharp\build\Release\sharp.node     

- Remove the "node_modules/sharp" directory then run    
  "npm install --ignore-scripts=false --verbose" and look for errors

at Object.<anonymous> (C:\Users\ph2200009\Desktop\TM\cplus_incident_mgnt\functions\node_modules\sharp\lib\constructor.js:34:9)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)  
    at Object.<anonymous> …
Run Code Online (Sandbox Code Playgroud)

reactjs npm-install sharp

4
推荐指数
1
解决办法
8246
查看次数

在Nodejs Sharp包中如何将两个图像添加到单个图像中?

我尝试使用 NodeJS 中的 Sharp 包将两个图像合并为一个图像,但我无法使用 Sharp 包来完成此操作

append node.js sharp

4
推荐指数
1
解决办法
3452
查看次数

在 macOS 上使用 npm 安装 Sharp 时出错

如果我创建一个新的 npm 项目:

mkdir ~/test
cd ~/test
npm init -y
Run Code Online (Sandbox Code Playgroud)

然后尝试安装Sharp

npm install --save sharp
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

> sharp@0.18.4 install /Users/hsribei/tmp/test/node_modules/sharp
> node-gyp rebuild

sh: /usr/local/lib/node_modules/npm/bin/node-gyp-bin/node-gyp: Permission denied
npm WARN test@1.0.0 No description
npm WARN test@1.0.0 No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 126
npm ERR! sharp@0.18.4 install: `node-gyp rebuild`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the sharp@0.18.4 install script.
npm ERR! This is probably not a problem with npm. There …
Run Code Online (Sandbox Code Playgroud)

npm sharp

3
推荐指数
1
解决办法
4311
查看次数