-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/13 create alt sizes for uploaded files (#15)
* Logging with Slog + Fixed Get File (#8) * logging with slog * added path prefix to file being returned * Added default cors config (#14) * setup empty func 'createAltSizes' that will be called at the end of 'saveFile' * removed a couple comments * new file to house functions for resizing uploaded files * printing out dimensions of src file * creating new image with half-sized dimensions. wip: copying over image contents to resized img * successfully copied image using same dimensions * able to create image at 1/4 the og dimensions * renamed functions, now creates 2 alts sizes on file upload * fixed file prefix
- Loading branch information
Showing
2 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
"image/jpeg" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func writeNewImg(img *image.Image, scale int, filename string) error { | ||
// create destination file | ||
dstFile, err := os.Create(fmt.Sprintf("%s-%d.jpeg", filename, scale)) | ||
if err != nil { | ||
return err | ||
} | ||
defer dstFile.Close() | ||
|
||
// create new image | ||
maxX := (*img).Bounds().Dx() / scale | ||
maxY := (*img).Bounds().Dy() / scale | ||
// newImg := image.NewRGBA((*img).Bounds()) | ||
newImg := image.NewRGBA(image.Rect(0, 0, maxX, maxY)) | ||
fmt.Printf("New image bounds: %v\n", newImg.Bounds()) | ||
|
||
for y := range (*newImg).Bounds().Dy() { | ||
for x := range (*newImg).Bounds().Dx() { | ||
newImg.Set(x, y, (*img).At(x*scale, y*scale)) | ||
} | ||
} | ||
|
||
err = jpeg.Encode(dstFile, newImg, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func createAltSizes(srcPath string) { | ||
slog.Info(fmt.Sprintf("Creating alt sizes for %s", srcPath)) | ||
srcFile, err := os.Open(srcPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer srcFile.Close() | ||
|
||
img, err := jpeg.Decode(srcFile) | ||
if err != nil { | ||
slog.Error("Error decoding image") | ||
panic(err) | ||
} | ||
|
||
filename := srcFile.Name() | ||
// Create medium img at 1/4 size | ||
err = writeNewImg(&img, 4, filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// Create icon image at 1/10 size | ||
err = writeNewImg(&img, 10, filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |