github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/app/image.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "image" 8 9 "github.com/disintegration/imaging" 10 ) 11 12 func genThumbnail(img image.Image) image.Image { 13 thumb := img 14 w := img.Bounds().Dx() 15 h := img.Bounds().Dy() 16 17 if h > ImageThumbnailHeight || w > ImageThumbnailWidth { 18 ratio := float64(h) / float64(w) 19 if ratio < ImageThumbnailRatio { 20 // we pre-calculate the thumbnail's width to make sure we are not upscaling. 21 targetWidth := int(float64(ImageThumbnailHeight) * float64(w) / float64(h)) 22 if targetWidth <= w { 23 thumb = imaging.Resize(img, 0, ImageThumbnailHeight, imaging.Lanczos) 24 } else { 25 thumb = imaging.Resize(img, ImageThumbnailWidth, 0, imaging.Lanczos) 26 } 27 } else { 28 // we pre-calculate the thumbnail's height to make sure we are not upscaling. 29 targetHeight := int(float64(ImageThumbnailWidth) * float64(h) / float64(w)) 30 if targetHeight <= h { 31 thumb = imaging.Resize(img, ImageThumbnailWidth, 0, imaging.Lanczos) 32 } else { 33 thumb = imaging.Resize(img, 0, ImageThumbnailHeight, imaging.Lanczos) 34 } 35 } 36 } 37 38 return thumb 39 } 40 41 func genPreview(img image.Image) image.Image { 42 preview := img 43 w := img.Bounds().Dx() 44 45 if w > ImagePreviewWidth { 46 preview = imaging.Resize(img, ImagePreviewWidth, 0, imaging.Lanczos) 47 } 48 49 return preview 50 }