fix image resizing

This commit is contained in:
Júlio César Batista 2023-04-07 10:48:33 -03:00
parent 37c46b7435
commit 944fb0c476
No known key found for this signature in database
GPG key ID: 10C4B861BF314878
2 changed files with 18 additions and 8 deletions

View file

@ -39,14 +39,15 @@ func addFile(zipFile *zip.Writer, filePath, content string) error {
}
// https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
const SLIDE_WIDTH = 9144000
const SLIDE_HEIGHT = 5143500
const HEADER_HEIGHT = 392471
const SLIDE_WIDTH = 9_144_000
const SLIDE_HEIGHT = 5_143_500
const HEADER_HEIGHT = 392_471
const IMAGE_HEIGHT = SLIDE_HEIGHT - HEADER_HEIGHT
// keep the right aspect ratio: SLIDE_WIDTH / SLIDE_HEIGHT = IMAGE_WIDTH / IMAGE_HEIGHT
const IMAGE_WIDTH = IMAGE_HEIGHT * (SLIDE_WIDTH / SLIDE_HEIGHT)
const IMAGE_WIDTH = 8_446_273
const IMAGE_ASPECT_RATIO = float64(IMAGE_WIDTH) / float64(IMAGE_HEIGHT)
const RELS_SLIDE_XML = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout7.xml" /><Relationship Id="%s" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/%s.png" /></Relationships>`

View file

@ -55,14 +55,23 @@ func (p *Presentation) AddSlide(pngContent []byte, boardPath []string) error {
var width, height int
srcSize := src.Bounds().Size()
srcWidth, srcHeight := float64(srcSize.X), float64(srcSize.Y)
// compute the size and position to fit the slide
if srcSize.X > srcSize.Y {
width = IMAGE_WIDTH
height = int(float64(width) * (float64(srcSize.Y) / float64(srcSize.X)))
// if the image is wider than taller and its aspect ratio is, at least, the same as the available image space aspect ratio
// then, set the image width to the available space and compute the height
if srcWidth/srcHeight >= IMAGE_ASPECT_RATIO {
width = SLIDE_WIDTH
height = int(float64(width) * (srcHeight / srcWidth))
if height > IMAGE_HEIGHT {
// this would overflow with the title, so we need to adjust to use only IMAGE_WIDTH
width = IMAGE_WIDTH
height = int(float64(width) * (srcHeight / srcWidth))
}
} else {
// otherwise, this image could overflow the slide height/header
height = IMAGE_HEIGHT
width = int(float64(height) * (float64(srcSize.X) / float64(srcSize.Y)))
width = int(float64(height) * (srcWidth / srcHeight))
}
top := (IMAGE_HEIGHT - height) / 2
left := (SLIDE_WIDTH - width) / 2