Skip to content

Instantly share code, notes, and snippets.

@umtkas
Created January 19, 2021 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umtkas/e91c4644a8675b77723f72b1b7136248 to your computer and use it in GitHub Desktop.
Save umtkas/e91c4644a8675b77723f72b1b7136248 to your computer and use it in GitHub Desktop.
func GetResizedImages(configuration configs.Configuration, sourceImagePath string) []string {
var resizedImagePaths []string
for _, imageSize := range configuration.ImageSizes {
resizedImagePath := resize(configuration, imageSize, sourceImagePath)
resizedImagePaths = append(resizedImagePaths, resizedImagePath)
}
return resizedImagePaths
}
// resizeImage resizes the image and returns path
func resize(configuration configs.Configuration, imageSize configs.ImageSize, sourceImagePath string) string {
sourceImage, err := imaging.Open(sourceImagePath)
if err != nil {
log.Fatalf("failed to open image: %v", err)
}
var resizedImage *image.NRGBA
if configuration.IsSaveWithAspectRatio {
resizedImage = imaging.Resize(sourceImage, imageSize.Width, 0, imaging.Lanczos)
} else {
resizedImage = imaging.Resize(sourceImage, imageSize.Width, imageSize.Height, imaging.Lanczos)
}
resizedImageName := getResizedImageName(configuration, imageSize, sourceImagePath)
imagePath := configuration.LocalImageDirectory + "/" + resizedImageName
err = imaging.Save(resizedImage, imagePath)
if err != nil {
log.Fatalf("resized image is not saved, %v", err)
}
return resizedImageName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment