Created
January 19, 2021 19:28
-
-
Save umtkas/e91c4644a8675b77723f72b1b7136248 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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