diff --git a/lib/pptx/pptx.go b/lib/pptx/pptx.go index 9deea80b6..1d0c0f44c 100644 --- a/lib/pptx/pptx.go +++ b/lib/pptx/pptx.go @@ -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 = `` diff --git a/lib/pptx/presentation.go b/lib/pptx/presentation.go index ac16ad52a..a79e6253d 100644 --- a/lib/pptx/presentation.go +++ b/lib/pptx/presentation.go @@ -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