Skip to content

Inconsistent image size after read image and insert image back #2217

Description

@he-ho-ha

Description

相关函数:
func copyPictures(ctx context.Context, f *excelize.File, sourceSheet, targetSheet string) error {
// 获取所有包含图片的单元格
pictureCells, err := f.GetPictureCells(sourceSheet)
if err != nil {
return fmt.Errorf("获取图片单元格失败: %v", err)
}

g.Log().Debug(ctx, "找到图片单元格数量", len(pictureCells))

// 为每个包含图片的单元格复制图片
for _, cell := range pictureCells {
	// 获取单元格中的所有图片
	pictures, err := f.GetPictures(sourceSheet, cell)
	if err != nil {
		g.Log().Warning(ctx, "获取单元格中的图片失败", "单元格", cell, "错误", err)
		continue
	}

	// 处理每个图片
	for _, picture := range pictures {
		// 获取图片格式后缀
		extension := picture.Extension
		if extension == "" {
			extension = ".png" // 默认使用png格式
		}

		// 创建GraphicOptions并复制属性
		graphicOptions := &excelize.GraphicOptions{}
		if picture.Format != nil {
			graphicOptions.AltText = picture.Format.AltText
			graphicOptions.PrintObject = picture.Format.PrintObject
			graphicOptions.Locked = picture.Format.Locked
			graphicOptions.LockAspectRatio = picture.Format.LockAspectRatio
			graphicOptions.AutoFit = picture.Format.AutoFit
			graphicOptions.AutoFitIgnoreAspect = picture.Format.AutoFitIgnoreAspect
			graphicOptions.OffsetX = picture.Format.OffsetX
			graphicOptions.OffsetY = picture.Format.OffsetY
			// 确保保留原始缩放比例
			if picture.Format.ScaleX > 0 {
				graphicOptions.ScaleX = picture.Format.ScaleX
			} else {
				graphicOptions.ScaleX = 1.0 // 默认100%缩放
			}
			if picture.Format.ScaleY > 0 {
				graphicOptions.ScaleY = picture.Format.ScaleY
			} else {
				graphicOptions.ScaleY = 1.0 // 默认100%缩放
			}
			graphicOptions.Hyperlink = picture.Format.Hyperlink
			graphicOptions.HyperlinkType = picture.Format.HyperlinkType
			graphicOptions.Positioning = picture.Format.Positioning
		} else {
			// 如果没有格式信息,使用默认设置确保图片保持原始尺寸
			graphicOptions.ScaleX = 1.0
			graphicOptions.ScaleY = 1.0
			graphicOptions.LockAspectRatio = true
		}
		g.Log().Debug(ctx, targetSheet, "复制图片", "单元格", cell, "图片格式", extension, "graphicOptions", graphicOptions, "picture.Format", picture.Format)

		// 使用AddPictureFromBytes方法将图片添加到目标工作表
		err = f.AddPictureFromBytes(
			targetSheet,
			cell,
			&picture,
			// &excelize.Picture{
			// 	Extension: extension,
			// 	File:      picture.File,
			// 	Format:    graphicOptions,
			// },
		)
		if err != nil {
			g.Log().Warning(ctx, "添加图片到目标工作表失败", "单元格", cell, "错误", err)
		}
	}
}

return nil

}

Steps to reproduce the issue

执行下述函数:
func copyPictures(ctx context.Context, f *excelize.File, sourceSheet, targetSheet string) error {
// 获取所有包含图片的单元格
pictureCells, err := f.GetPictureCells(sourceSheet)
if err != nil {
return fmt.Errorf("获取图片单元格失败: %v", err)
}

g.Log().Debug(ctx, "找到图片单元格数量", len(pictureCells))

// 为每个包含图片的单元格复制图片
for _, cell := range pictureCells {
	// 获取单元格中的所有图片
	pictures, err := f.GetPictures(sourceSheet, cell)
	if err != nil {
		g.Log().Warning(ctx, "获取单元格中的图片失败", "单元格", cell, "错误", err)
		continue
	}

	// 处理每个图片
	for _, picture := range pictures {
		// 获取图片格式后缀
		extension := picture.Extension
		if extension == "" {
			extension = ".png" // 默认使用png格式
		}

		// 创建GraphicOptions并复制属性
		graphicOptions := &excelize.GraphicOptions{}
		if picture.Format != nil {
			graphicOptions.AltText = picture.Format.AltText
			graphicOptions.PrintObject = picture.Format.PrintObject
			graphicOptions.Locked = picture.Format.Locked
			graphicOptions.LockAspectRatio = picture.Format.LockAspectRatio
			graphicOptions.AutoFit = picture.Format.AutoFit
			graphicOptions.AutoFitIgnoreAspect = picture.Format.AutoFitIgnoreAspect
			graphicOptions.OffsetX = picture.Format.OffsetX
			graphicOptions.OffsetY = picture.Format.OffsetY
			// 确保保留原始缩放比例
			if picture.Format.ScaleX > 0 {
				graphicOptions.ScaleX = picture.Format.ScaleX
			} else {
				graphicOptions.ScaleX = 1.0 // 默认100%缩放
			}
			if picture.Format.ScaleY > 0 {
				graphicOptions.ScaleY = picture.Format.ScaleY
			} else {
				graphicOptions.ScaleY = 1.0 // 默认100%缩放
			}
			graphicOptions.Hyperlink = picture.Format.Hyperlink
			graphicOptions.HyperlinkType = picture.Format.HyperlinkType
			graphicOptions.Positioning = picture.Format.Positioning
		} else {
			// 如果没有格式信息,使用默认设置确保图片保持原始尺寸
			graphicOptions.ScaleX = 1.0
			graphicOptions.ScaleY = 1.0
			graphicOptions.LockAspectRatio = true
		}
		g.Log().Debug(ctx, targetSheet, "复制图片", "单元格", cell, "图片格式", extension, "graphicOptions", graphicOptions, "picture.Format", picture.Format)

		// 使用AddPictureFromBytes方法将图片添加到目标工作表
		err = f.AddPictureFromBytes(
			targetSheet,
			cell,
			&picture,
			// &excelize.Picture{
			// 	Extension: extension,
			// 	File:      picture.File,
			// 	Format:    graphicOptions,
			// },
		)
		if err != nil {
			g.Log().Warning(ctx, "添加图片到目标工作表失败", "单元格", cell, "错误", err)
		}
	}
}

return nil

}

Describe the results you received

复制图片尺寸无法和原图保持一致

Describe the results you expected

GetPictures 返回图片完整的属性配置

Go version

1.23.4

Excelize version or commit ID

2.9.0

Environment

win11,wps

Validations

  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • The provided reproduction is a minimal reproducible example of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions