标签: sharp

在node.js中使用Sharp压缩图像

我想调整和使用压缩图像sharpnode.js

锐利的jpeg存在单独的压缩,因为webp存在单独的压缩,并且存在单独的压缩png

WEBP

sharp('a.jpg')
.resize(1000)
.webp({quality: 80})
Run Code Online (Sandbox Code Playgroud)

JPEG格式

sharp('_4_.jpg')
 .resize(1000)
 .jpeg({quality: 80})
Run Code Online (Sandbox Code Playgroud)

PNG

sharp('_4_.jpg')
 .resize(1000)
 .png({compressionLevel: 8})
Run Code Online (Sandbox Code Playgroud)

基本上,我想压缩图像并调整大小,而不检查它们的格式。

有什么用sharp吗?

javascript image-processing image-compression node.js sharp

5
推荐指数
3
解决办法
3815
查看次数

如何在使用 gridfs 保存之前调整大小和图像?

我是 mongoDB 和 gridfs 的初学者。我有一个代码可以在数据库中保存传入的图像。但我想在保存之前调整图像的大小。它尝试使用 Sharp,但我不太明白它是如何工作的。

我尝试了以下代码,但它不起作用

const storage = new GridFsStorage({
  url: mongoURI,

  file: (req, file) => {
    return new Promise((resolve, reject) => {
      const filename = req.params.userID;
      const fileInfo = {
        filename: filename,
        bucketName: "ProfilePic", 
      };
      resolve(fileInfo);
    });
  }
});

const upload = multer({ storage });

router.post("/setProfilePic/:userID", upload.single("picture"), async(req, res) => {
//code not working
  await sharp(req.file.path)
    .resize(500)
    .jpeg({quality: 50})
    .toFile(
        path.resolve(req.file.destination,'resized',image)
    )
// end of non working code
  return res.status(200).json({state: "ok"})
});
Run Code Online (Sandbox Code Playgroud)

mongodb node.js gridfs multer sharp

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

win32-x64 二进制文件不能在“linux-x64”平台上使用

'win32-x64' 二进制文件不能在 'linux-x64' 平台上使用,在 Windows 系统上安装 node 时出现此错误。这在执行时破坏了我在 aws 上的 lambda 代码。

node.js sharp

5
推荐指数
0
解决办法
653
查看次数

如何在graphql gatsby中允许可选的清晰图像?

我通常有frontmatter,它将包含一个对象数组,每个对象内部将是一个图像,该图像将引用与markdown文件相关的文件字符串。

问题是,数组有时可能是空的,这意味着 graphql 必须通过将所有值设置为非空来确定架构是什么,我已经能够使用 Gatsby 的字符串等简单类型来做到这一点createSchemaCustomization,但是我希望能够声明一个引用图像的字符串以使用 Image Sharp(因此 gatsby-transformer-sharp 可以在组件接收图像之前压缩图像)。

在 Gatsby 文档或图像清晰插件中似乎没有任何地方有用于此的模式类型。

我尝试将其File!用作一种在数组为空时有效的类型,但是当您实际尝试引用真实图像时,它只是返回{ image: childImageSharp: null }意味着 gatsby-transformer-sharp 不会像File!未声明时那样在它们上运行。

以下是我的架构的声明方式:

exports.createSchemaCustomization = ({ actions }) => {
    const { createTypes } = actions;
    const typeDefs = `
        type MarkdownRemark implements Node {
            frontmatter: Frontmatter
        }
        type Frontmatter {
          features: [Feature!]!
        }
        type Feature {
            title: String!
            description: String!
            image: File!
        }
    `;

    createTypes(typeDefs);
};
Run Code Online (Sandbox Code Playgroud)

这是我的 graphql 查询:

export const query = …
Run Code Online (Sandbox Code Playgroud)

javascript graphql gatsby sharp gatsby-image

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

在 AWS Lambda 和 Node.js 12.x 中使用自定义字体

我正在尝试创建一个 AWS Lambda 函数来对图像应用水印效果。原始图像将从 S3 中提取,应用水印并将结果上传回 S3。

我使用Sharp通过以下代码创建这些组合:

await sharp(inputImage)
              .rotate()
              .composite([{ input: new Buffer.from(watermark), tile: true, gravity: 'northwest' }])
Run Code Online (Sandbox Code Playgroud)

带有自定义水印 SVG

watermark = `<svg ... <text x="50%" y="100%" style="font-family:'Open Sans';font-weight:400;font-size:75px;font-style:normal;fill:rgba(128,128,128,1);stroke:none;" text-anchor="middle">${event.watermarkName}</text></g></svg>`;
Run Code Online (Sandbox Code Playgroud)

SVG 内部有一个文本元素,我可以动态更改该元素以包含指定的字符串。例如,我可以放置(c) Company Name一张图像和(c) John Doe另一张图像。

该代码在我的机器上运行时有效,但一旦进入具有 Node.js 12.x 运行时的 Lambda 函数,这就是结果 文本水印示例

正如你所看到的,我指定了Open Sans要使用的字体。事实上,我将OpenSans-Regular.ttf文件放入/fonts/,创建了一个fonts.conf文件并按照这个问题FONTCONFIG_PATH='/var/task/fonts'中的描述设置了 a 。

尽管如此,字体仍然无法工作。有人对我如何解决这个问题有建议吗?

fonts amazon-web-services aws-lambda sharp

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

尖锐错误:输入缓冲区包含不支持的图像格式

我想对从 AWS S3 获取的图像创建图像操作,并希望对其执行操作操作。我正在使用流来解决加载大文件的问题。

import AWS from 'aws-sdk'
import sharp from 'sharp'

const s3 = new AWS.S3()
const transformer = (w, res, next) =>
    sharp()
        .resize(w)
        .on('data', (data) => {
            console.log(data)
            res.write(data, 'binary')
        })
        .on('error', (err) => next(err))
        .on('end', () => {
            console.log('finished')
            res.status(200).end()
        })

const readStream = s3
    .getObject({
        Bucket: process.env.UPLOAD_BUCKET_NAME,
        Key: 'test.jpg'
        // Key: `${req.uid.uid}/${req.param('img')}`
    })
    .createReadStream()

const getImage = (w, res, next) => {
    readStream.pipe(transformer(w, res, next))
    readStream.on('error', (err) => next(err))
}

export default getImage
Run Code Online (Sandbox Code Playgroud)

我在路线上调用 getImage …

amazon-s3 node.js sharp

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

如何在 React 和 Webpack 项目中使用来自sharp模块的图像

这是一个codeandbox,我希望其中的图像由sharp模块处理。我希望它这样做是因为我希望这个模块对我的 React 项目中的所有图像进行网络优化,并将它们转换为 webm。

这是我尝试使用 ImageSharp的相同代码和框

reactjs webpack sharp

5
推荐指数
2
解决办法
285
查看次数

调用 .toBuffer() 时,Sharp 输入文件包含不支持的图像格式

在尖锐物体上调用该.toBuffer()方法时,出现以下错误:

Input buffer contains unsupported image format
Run Code Online (Sandbox Code Playgroud)

我正在从 S3 获取图像,并且我确信它会作为缓冲区检索,然后传递给 Sharp。

此外,当我console.log缓冲区的base64并检查后面的内容时,我确实得到了原始图像并且文件类型是jpeg,所以我似乎无法弄清楚为什么它会说输入缓冲区包含不支持的图像格式。

const imageBuffer = fileFromS3.Body
console.log(imageBuffer)
console.log(imageBuffer.toString('base64'))

let result = await sharp(imageBuffer)
  .toColourspace('b-w')
  .sharpen()
  .toBuffer()
Run Code Online (Sandbox Code Playgroud)

当我在运行 toBuffer() 之前检查尖锐对象时,我在对象中获取了以下数据:

let result = await sharp(imageBuffer)
  .toColourspace('b-w')
  .sharpen()

console.log(result)
Run Code Online (Sandbox Code Playgroud)

印刷:

...
input: {
      failOnError: true,
      limitInputPixels: 268402689,
      sequentialRead: false,
      buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 02 …
Run Code Online (Sandbox Code Playgroud)

node.js sharp

5
推荐指数
0
解决办法
2003
查看次数

找不到全局 Sharp-cli 包使用的 Sharp 实例

webpack.config.js当尝试运行 Web 版 Expo 时,在我的应用程序文件夹的根目录中创建一个名为的文件后,我收到错误“无法找到全局 Sharp-cli 包使用的 Sharp 实例” 。文件的内容可以像默认的 expo 实现一样简单,但仍然失败:

module.exports = async function(env, argv) {
    const loadDefaultConfigAsync = require('@expo/webpack-config');
    return await loadDefaultConfigAsync(env, argv);
};
Run Code Online (Sandbox Code Playgroud)

节点版本:v16.3.1

NPM版本:v8.1.2

{
  "name": "foo",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "test": "jest --watchAll"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "@expo/vector-icons": "^12.0.0",
    "@expo/webpack-config": "^0.16.11",
    "@react-navigation/bottom-tabs": "^6.0.5",
    "@react-navigation/native": "^6.0.2",
    "@react-navigation/native-stack": "^6.1.0",
    "expo": "~43.0.2", …
Run Code Online (Sandbox Code Playgroud)

node.js webpack sharp expo

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

无法使用 win32-x64 运行时 (Strapi) 加载“sharp”模块

我曾经使用本地安装的 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)

node.js nvm strapi sharp

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