Strapi:更新图像尺寸时出现问题

Strapi: Problems updating image dimensions

提问人:Chris Michael 提问时间:11/15/2023 更新时间:11/15/2023 访问量:28

问:

错误报告

所需的系统信息

  • Node.js版本:v14.19.3

  • NPM 版本:6.14.17

  • Strapi 版本:v4.11.4

  • 数据库:postgres

  • 操作系统:(macbook pro m1)

  • 您的项目是 Javascript 还是 Typescript:javascript

描述错误

我创建了一个端点,该端点专注于将图像上传和更新到用户表中的字段(头像),并使用 sharp 库将尺寸调整为 1000x1000

我已经上传了 DIMENSIONS 2768✕2208 的图像,但是当将图像值更新为 width: 1000, height: 1000 时,更改未应用。我一直看到 2768✕2208

Original Dimensions: {
   format: 'jpeg',
   width: 2768,
   height: 2208,
   space: 'srgb',
   channels: 3,
   depth: 'uchar',
   density: 72,
   chromaSubsampling: '4:2:0',
   isProgressive: false,
   resolutionUnit: 'inch',
   hasProfile: false,
   hasAlpha: false,
   orientation: 6,
   exif: <Buffer 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 0c 00 00 01 04 00 01 00 00 00 d0 0a 00 00 01 01 04 00 01 00 00 00 a0 08 00 00 0f 01 02 00 08 00 00 00 9e 00 ... 27422 more bytes>,
   xmp: <Buffer 3c 78 3a 78 6d 70 6d 65 74 61 20 78 6d 6c 6e 73 3a 78 3d 22 61 64 6f 62 65 3a 6e 73 3a 6d 65 74 61 2f 22 20 78 3a 7 8 6d 70 74 6b 3d 22 41 64 6f 62 65 ... 1197 more bytes>
}
New Dimensions: {
   format: 'jpeg',
   size: 111518,
   width: 1000,
   height: 1000,
   space: 'srgb',
   channels: 3,
   depth: 'uchar',
   density: 72,
   chromaSubsampling: '4:2:0',
   isProgressive: false,
   resolutionUnit: 'inch',
   hasProfile: true,
   hasAlpha: false,
   orientation: 6,
   exif: <Buffer 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 0c 00 00 01 04 00 01 00 00 00 d0 0a 00 00 01 01 04 00 01 00 00 00 a0 08 00 00 0f 01 02 00 08 00 00 00 9e 00 ... 27434 more bytes>,
   icc: <Buffer 00 00 1b 0a 6c 63 6d 73 02 30 00 00 6d 6e 74 72 52 47 42 20 58 59 5a 20 07 d4 00 08 00 0d 00 0c 00 12 00 06 61 63 73 70 4d 53 46 54 00 00 00 00 6c 63 ... 6872 more bytes>,
   xmp: <Buffer 3c 78 3a 78 6d 70 6d 65 74 61 20 78 6d 6c 6e 73 3a 78 3d 22 61 64 6f 62 65 3a 6e 73 3a 6d 65 74 61 2f 22 20 78 3a 7 8 6d 70 74 6b 3d 22 41 64 6f 62 65 ... 1197 more bytes>
}

对新尺寸的更改正在正确应用,但是当我签入strapi时,值尚未更新

截图

代码片段

"use strict";

const { first } = require("lodash");
const sharp = require("sharp");
const { getPostgreSQLError } = require("../../../utils/error-lookup");

/**
 * A set of functions called "actions" for `upload-user-avatar`
 */

module.exports = {
  upload: async (ctx) => {
    try {
      const { user } = ctx.state;
      if (!user) {
        ctx.status = 401;
        ctx.body = { message: "..." };
        return;
      }

      const file = ctx.request.files;
      const avatar = file["files.avatar"];

      if (!avatar) {
        ctx.status = 400;
        ctx.body = { message: "..." };
        return;
      }

      // Calculate the dimensions for a square avatar (e.g., 1000x1000)
      const AVATAR_SIZE = 1000;

      const originalDimensions = await sharp(avatar.path).metadata();
      console.log("Original Dimensions:", originalDimensions);

      // Resize the image to the desired dimensions and maintain orientation
      const resizedBuffer = await sharp(avatar.path)
        .resize({
          width: AVATAR_SIZE,
          height: AVATAR_SIZE,
          fit: "cover", // This option ensures the image is resized without changing the aspect ratio
          position: "center",
        })
        .withMetadata()
        .toBuffer();

      const newDimensions = await sharp(resizedBuffer).metadata();
      console.log("New Dimensions:", newDimensions);

      // Upload the resized image using Strapi's upload service
      const resizedFile = await strapi.plugins.upload.services.upload.upload({
        data: resizedBuffer,
        files: {
          name: avatar.name,
          path: avatar.path,
          type: avatar.type,
        },
      });

      const resizedFileAsObject = first(resizedFile);

      // Save the resized image URL to the user's avatar field
      await strapi.db.query("plugin::users-permissions.user").update({
        where: { id: user.id },
        data: {
          avatar: {
            id: resizedFileAsObject.id,
            width: newDimensions.width,
            height: newDimensions.height,
          },
        },
      });

      ctx.status = 200;
      ctx.body = {
        message: "...",
      };
    } catch (error) {
      const errorMessage = getPostgreSQLError(error?.code);
      ctx.body = errorMessage ? errorMessage : error;
    }
  },
};

在文件表中检查宽度/高度值具有默认值,而不是 1000X1000,它们不会更新。

enter image description here

JavaScript strapi 图像大小调整 sharp

评论


答: 暂无答案