20 Commits

Author SHA1 Message Date
d4446c1f24 upload support for new images 2024-08-01 22:33:29 -04:00
9200bd16db Minor refactor
* Move namegen to seperate file
* Simplify Makefile
2023-09-19 22:59:47 -04:00
acc9e87f2c Merge pull request 'Cleanup for 1.0' (#18) from cleanup into main
Reviewed-on: #18

Fixes #10
2023-09-14 15:13:32 -04:00
5c8f6bf443 remove dead code and comments 2023-09-14 15:12:28 -04:00
2b1253e416 Replace croppie.js with croppie.min.js 2023-09-14 15:12:28 -04:00
445ec79a7a Remove unwanted files 2023-09-14 15:12:28 -04:00
a6abe54dbc Remove live reload js when not in dev mode
Fixes #16
2023-09-14 15:12:28 -04:00
ab636df83d Highlight selected template 2023-09-14 14:57:33 -04:00
61a8d4ca5c Add anyhttp support (#17)
Reviewed-on: #17
Fixes #11
2023-09-11 21:16:10 -04:00
6854a3e1f1 Merge pull request 'Implement selecting template for collage' (#14) from template-chooser into main
Reviewed-on: #14
2023-09-06 23:59:07 -04:00
b7fe2e8192 cleanup 2023-09-06 23:58:21 -04:00
8034acb504 Implement selecting template for collage 2023-09-06 23:44:29 -04:00
8a56639920 Add jsdoc typehints 2023-09-06 18:56:02 -04:00
00bc166148 add page size select 2023-09-05 18:42:47 -04:00
9dceae06a7 Move css to seperate file and add no-cache for dev assets 2023-09-05 09:04:54 -04:00
a8f9d87b47 css cleanup 2023-09-01 23:12:52 -04:00
1662ff226b minor refactor 2023-09-01 15:13:54 -04:00
faa413c9b1 Snap button generates collage and shows link
fixes #3
2023-08-31 23:31:51 -04:00
1c742809a1 Serve web files in go 2023-08-31 20:27:43 -04:00
e1e6e3650a Add post target to create collage
fixes #1
2023-08-28 23:35:32 -04:00
22 changed files with 687 additions and 2035 deletions

View File

@ -1,7 +1,10 @@
# livereload: github.com/omeid/go-livereload/cmd/livereload@v0.0.0-20180903043807-18d58b752b26
livereload: livereload:
livereload . & go run github.com/omeid/go-livereload/cmd/livereload@latest web | ts
server: devserver:
python3 -m http.server 8082 & go run . --dev --images-dir w.tmp/images --collages-dir w.tmp
update_croppie:
curl -Lo web/croppie.min.js https://github.com/Foliotek/Croppie/raw/v2.6.5/croppie.min.js
curl -Lo web/croppie.css https://github.com/Foliotek/Croppie/raw/v2.6.5/croppie.css

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# Collage Maker

View File

@ -1,56 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"os"
"go.balki.me/collage-maker/collage"
)
func main() {
req := collage.Request{}
/*
reqStr := []byte(`
{
"background_image": "",
"aspect": { "width": 4224, "height": 3264 },
"dimension": { "width": 1187, "height": 848 },
"photos": [
{
"image": "img1.jpg",
"crop": {
"start": { "x": 419, "y": 667 },
"end": { "x": 2707, "y": 3389 }
},
"frame": {
"start": { "x": 0, "y": 0 },
"end": { "x": 712, "y": 848 }
}
},
{
"image": "img2.jpg",
"crop": {
"start": { "x": 331, "y": 44 },
"end": { "x": 1132, "y": 1468 }
},
"frame": {
"start": { "x": 712, "y": 0 },
"end": { "x": 1187, "y": 848 }
}
}
]
}
`)
*/
// {"background_image":"","aspect":{"width":4224,"height":3264},"dimension":{"width":1187,"height":848},"photos":[{"image":"img1.jpg","crop":{"start":{"x":528,"y":3},"end":{"x":2696,"y":2583}},"frame":{"start":{"x":0,"y":0},"end":{"x":712,"y":848}}},{"image":"img2.jpg","crop":{"start":{"x":410,"y":0},"end":{"x":1014,"y":1074}},"frame":{"start":{"x":712,"y":0},"end":{"x":1187,"y":848}}}]}
reqStr := []byte(`
{"background_image":"","aspect":{"width":4224,"height":3264},"dimension":{"width":1097,"height":848},"photos":[{"image":"img1.jpg","crop":{"start":{"x":448,"y":595},"end":{"x":2721,"y":3560}},"frame":{"start":{"x":0,"y":0},"end":{"x":649,"y":848}}},{"image":"img2.jpg","crop":{"start":{"x":418,"y":1},"end":{"x":1022,"y":1180}},"frame":{"start":{"x":665,"y":0},"end":{"x":1098,"y":848}}}]}
`)
err := json.Unmarshal(reqStr, &req)
fmt.Println(err)
out, err := os.Create("./collage.jpg")
fmt.Println(err)
err = collage.Make(req, os.DirFS("."), out)
fmt.Println(err)
}

View File

@ -48,7 +48,7 @@ type Request struct {
Photos []Photo `json:"photos"` Photos []Photo `json:"photos"`
} }
func Make(req Request, source fs.FS, output io.Writer) error { func Make(req *Request, source fs.FS, output io.Writer) error {
rec := image.Rect(0, 0, int(req.Aspect.Width), int(req.Aspect.Height)) rec := image.Rect(0, 0, int(req.Aspect.Width), int(req.Aspect.Height))
canvas := image.NewRGBA64(rec) canvas := image.NewRGBA64(rec)
white := color.RGBA{255, 255, 255, 255} white := color.RGBA{255, 255, 255, 255}

View File

@ -67,7 +67,7 @@ func TestMake(t *testing.T) {
t.Fatalf("failed to create ouput image file %v", err) t.Fatalf("failed to create ouput image file %v", err)
} }
err = Make(req, testDataFS, out) err = Make(&req, testDataFS, out)
if err != nil { if err != nil {
t.Fatalf("failed to make collage %v", err) t.Fatalf("failed to make collage %v", err)
} }
@ -104,17 +104,6 @@ func TestCrop(t *testing.T) {
if string(refImgPrefix) != croppedImgPrefix { if string(refImgPrefix) != croppedImgPrefix {
t.Fatalf("Cropped image is not the same as reference image") t.Fatalf("Cropped image is not the same as reference image")
} }
// SavePrefix(cropped)
// expectedImage, err := GetImage(testDataFS, "test_output.jpg")
// if err != nil {
// t.Fatalf("failed to get reference crop image %v", err)
// }
// fmt.Printf("%v\n", cropped)
// fmt.Printf("%v\n", expectedImage)
// if fmt.Sprintf("%#v", cropped) != fmt.Sprintf("%#v", expectedImage) {
// t.Fatalf("Cropped image is not the same as reference image")
// }
// SaveImage(cropped)
} }
// Save first 1000 bytes of string representation to compare against reference // Save first 1000 bytes of string representation to compare against reference

1625
croppie.js

File diff suppressed because it is too large Load Diff

View File

@ -1 +0,0 @@
hello world

2
go.mod
View File

@ -3,7 +3,7 @@ module go.balki.me/collage-maker
go 1.21 go 1.21
require ( require (
github.com/oliamb/cutter v0.2.2 go.balki.me/anyhttp v0.3.0
go.oneofone.dev/resize v1.0.1 go.oneofone.dev/resize v1.0.1
) )

4
go.sum
View File

@ -1,6 +1,6 @@
github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k=
github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.balki.me/anyhttp v0.3.0 h1:WtBQ0rnkg567sX/O4ij/+qBbdCIUt5VURSe718sITBY=
go.balki.me/anyhttp v0.3.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s=
go.oneofone.dev/resize v1.0.1 h1:HjpVar/4pxMGrjO44ThaMX1Q5UOBw0KxzbxxRDZPQuA= go.oneofone.dev/resize v1.0.1 h1:HjpVar/4pxMGrjO44ThaMX1Q5UOBw0KxzbxxRDZPQuA=
go.oneofone.dev/resize v1.0.1/go.mod h1:zGFmn7q4EUZVlnDmxqf+b0mWpxsTt0MH2yx6ng8tpq0= go.oneofone.dev/resize v1.0.1/go.mod h1:zGFmn7q4EUZVlnDmxqf+b0mWpxsTt0MH2yx6ng8tpq0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

View File

@ -1,86 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="croppie.css" />
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
<script src="http://localhost:35729/livereload.js"></script>
<script src="croppie.js"></script>
<script src="index.js"></script>
<!--
width: 3264px;
height: 4224px;
width: 8.5in;
height: 11in;
-->
<style>
.image-surface {
overflow: hidden;
margin: auto;
background-color: lightgreen;
}
.imagebox {
grid-area: image;
display:flex;
align-items: center;
justify-content: center;
padding: 50px;
flex: 70%;
}
.container {
display: flex;
background-color: lightyellow;
height: calc(100vh - 20px);
}
.controls {
background-color: lightgrey;
display: flex;
grid-area: controls;
align-items: center;
justify-content: center;
flex: 30%;
}
.showbuton {
font-size: 2rem;
}
.letter-portrait {
height: 100%;
aspect-ratio: 85 / 110;
}
.letter-portrait .collage-img {
height: 50%;
}
.letter-landscape-2 {
display:flex;
// width: 80vh;
height: 100%;
// margin: auto;
aspect-ratio: 110 / 85;
// aspect-ratio: 7 / 5;
gap: 1rem;
}
.letter-landscape-2 .img1 {
flex: 60%;
}
.letter-landscape-2 .img2 {
flex: 40%;
}
</style>
</head>
<body onload="main()">
<div class="container">
<div class="controls">
<button class="showbuton" onClick="snap()">Snap Collage</button>
</div>
<div class="imagebox">
<div id="collage" class="image-surface letter-landscape-2">
<div class="collage-img img1" data-collage-image-url="img1.jpg">
<!-- <img src="img1.jpg"> -->
</div>
<div class="collage-img img2" data-collage-image-url="img2.jpg">
<!-- <img src="img2.jpg"> -->
</div>
</div>
</div>
</div>
</body>
</html>

107
index.js
View File

@ -1,107 +0,0 @@
function main() {
initCollage("collage")
}
function makeCroppieElem(elem, imgUrl) {
cpie = new Croppie(elem, {
viewport: {
width: elem.clientWidth,
height: elem.clientHeight,
type: 'square'
},
showZoomer: false,
});
cpie.bind({
url: imgUrl
});
return cpie
}
// collage state
var collageDivId;
var crops = [];
function initCollage(divId) {
collageDivId = divId
const collageDiv = document.getElementById(collageDivId)
for(elem of collageDiv.getElementsByClassName("collage-img")) {
const cpie = makeCroppieElem(elem, elem.dataset.collageImageUrl)
const lastLen = crops.push(cpie)
elem.dataset.collageCropieIndex = lastLen - 1
}
}
function showCrop() {
for(cpie of crops) {
console.log(cpie.get())
console.log(cpie.element.clientWidth)
console.log(cpie.element.clientHeight)
}
}
async function ip() {
const resp = await fetch("dummy.txt")
console.log(await resp.text())
}
function snap() {
const collageDiv = document.getElementById(collageDivId)
col = collageDiv.offsetLeft;
cot = collageDiv.offsetTop;
console.log("----------------------")
req = {
background_image: "",
aspect: {
width: 528 * 4 * 2,
height: 816 * 4,
},
dimension: {
width: collageDiv.clientWidth,
height: collageDiv.clientHeight,
},
photos: [],
};
for(elem of collageDiv.getElementsByClassName("collage-img")) {
const cpie = crops[elem.dataset.collageCropieIndex]
// console.log(cpie.get().points)
// console.log(elem.offsetLeft - col)
// console.log(elem.offsetTop - cot)
// console.log(elem.clientWidth)
// console.log(elem.clientHeight)
const fsx = elem.offsetLeft - col
const fsy = elem.offsetTop - cot
const [sx, sy, ex, ey] = cpie.get().points;
const photo = {
image: elem.dataset.collageImageUrl,
crop: {
start: {
x: parseInt(sx),
y: parseInt(sy),
},
end: {
x: parseInt(ex),
y: parseInt(ey),
},
},
frame: {
start: {
x: fsx,
y: fsy,
},
end: {
x: fsx + elem.clientWidth,
y: fsy + elem.clientHeight,
},
},
};
req.photos.push(photo)
}
console.log(JSON.stringify(req));
ip()
}

239
main.go
View File

@ -1,99 +1,188 @@
package main package main
import ( import (
"bytes"
"context"
"embed"
"encoding/json"
"flag"
"fmt" "fmt"
"image" "io"
"image/draw" "io/fs"
"image/jpeg" "net/http"
"os" "os"
"path"
"regexp"
"time"
"github.com/oliamb/cutter" "log/slog"
"go.oneofone.dev/resize"
"go.balki.me/anyhttp"
"go.balki.me/anyhttp/idle"
"go.balki.me/collage-maker/collage"
) )
/** var (
"431" imagesDir string
"697" collageDir string
"2514" devMode bool
"2047" collageNameGen *nameGen
zoom: 0.392 imagesDirFs fs.FS
816 listenAddr string
528
------------- //go:embed web
"153" webFS embed.FS
"9" )
"1331"
"772" const (
zoom: 0.6949 kb = 1024
816 mb = 1024 * kb
528 )
*/
func main() { func main() {
const width = 816 * 4 flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir")
const height = 528 * 4 flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir")
imgFile1, err := os.Open("img1.jpg") flag.BoolVar(&devMode, "dev", false, "Serve local assets during development")
if err != nil { flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax")
fmt.Println(err)
} flag.Parse()
imgFile2, err := os.Open("img2.jpg")
if err != nil { collageNameGen = NewNameGen()
fmt.Println(err) imagesDirFs = os.DirFS(imagesDir)
} imagesURLPath := "images"
img1, _, err := image.Decode(imgFile1) collagesPath := "collages"
if err != nil {
fmt.Println(err) addFileServer := func(path, dir string) {
} httpFileServer := http.FileServer(http.Dir(dir))
img2, _, err := image.Decode(imgFile2) http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer))
if err != nil {
fmt.Println(err)
} }
img1, err = cutter.Crop(img1, cutter.Config{ addFileServer(imagesURLPath, imagesDir)
Width: 2514 - 431, addFileServer(collagesPath, collageDir)
Height: 2047 - 697,
Anchor: image.Point{431, 697}, if devMode {
Mode: cutter.TopLeft, // optional, default value httpFileServer := http.FileServer(http.Dir("web"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-cache")
httpFileServer.ServeHTTP(w, r)
}) })
} else {
indexModTime := time.Now()
indexHTML := func() io.ReadSeeker {
indexHTMLContent, err := webFS.ReadFile("web/index.html")
if err != nil { if err != nil {
panic(err) panic(err)
} }
devOnlyRegex := regexp.MustCompile("\n[^\n]*<!-- DEVONLY[^\n]*")
img1 = resize.Resize(width, height, img1, resize.Lanczos3) return bytes.NewReader(devOnlyRegex.ReplaceAllLiteral(indexHTMLContent, nil))
}()
img2, err = cutter.Crop(img2, cutter.Config{ httpFileServer := func() http.Handler {
Width: 1331 - 153, webrootFs, err := fs.Sub(webFS, "web")
Height: 772 - 9,
Anchor: image.Point{153, 9},
Mode: cutter.TopLeft, // optional, default value
})
if err != nil { if err != nil {
panic(err) panic(err)
} }
return http.FileServer(http.FS(webrootFs))
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.ServeContent(w, r, "index.html", indexModTime, indexHTML)
} else {
httpFileServer.ServeHTTP(w, r)
}
})
}
img2 = resize.Resize(width, height, img2, resize.Lanczos3) http.HandleFunc("/make-collage", func(w http.ResponseWriter, r *http.Request) {
collageReq := collage.Request{}
//starting position of the second image (bottom left) body, err := io.ReadAll(r.Body)
//sp2 := image.Point{img1.Bounds().Dx(), 0}
//new rectangle for the second image
//r2 := image.Rectangle{sp2, sp2.Add(img2.Bounds().Size())}
//rectangle for the big image
r := image.Rectangle{image.Point{0, 0}, image.Point{width, height + height}}
r2 := image.Rectangle{image.Point{0, height + 1}, image.Point{width, height + height}}
rgba := image.NewRGBA(r)
draw.Draw(rgba, img1.Bounds(), img1, image.Point{0, 0}, draw.Src)
draw.Draw(rgba, r2, img2, image.Point{0, 0}, draw.Src)
out, err := os.Create("./output.jpg")
if err != nil { if err != nil {
fmt.Println(err) slog.Error("failed to read request body", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := json.Unmarshal(body, &collageReq); err != nil {
slog.Error("failed to unmarshal json request", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
collageFile, err := MakeCollage(&collageReq)
if err != nil {
slog.Error("failed to make collage", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write([]byte(collageFile)); err != nil {
slog.Error("Failed to write collageFile", "error", err)
}
})
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
err := func() error {
err := r.ParseMultipartForm(500 * mb)
if err != nil {
return err
}
files, ok := r.MultipartForm.File["photos"]
if !ok {
return fmt.Errorf("photos not found in request")
}
for i, f := range files {
imgFile, err := f.Open()
if err != nil {
return err
}
defer imgFile.Close()
imagePath := path.Join(imagesDir, fmt.Sprintf("img%d.jpg", i+1))
os.Remove(imagePath)
if err != nil {
return err
}
op, err := os.Create(imagePath)
if err != nil {
return err
}
defer op.Close()
_, err = io.Copy(op, imgFile)
if err != nil {
return err
}
}
return nil
}()
if err != nil {
slog.Error("Image upload failed", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
})
addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil))
if err != nil {
slog.Error("anyhttp Serve failed", "error", err)
} }
var opt jpeg.Options if addrType == anyhttp.SystemdFD {
opt.Quality = 80 if err := idle.Wait(30 * time.Minute); err != nil {
slog.Error("Failed to wait for idler", "error", err)
jpeg.Encode(out, rgba, &opt) }
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
defer cancel()
if err := server.Shutdown(ctx); err != nil {
slog.Error("http server Shutdown failed", "error", err)
}
} else {
<-done
}
}
func MakeCollage(req *collage.Request) (string, error) {
collageFile := fmt.Sprintf("collage-%s.jpg", collageNameGen.Next())
out, err := os.Create(path.Join(collageDir, collageFile))
if err != nil {
return "", fmt.Errorf("failed to create collage output file, err: %w", err)
}
defer out.Close()
if err := collage.Make(req, imagesDirFs, out); err != nil {
return "", fmt.Errorf("failed to make collage, err: %w", err)
}
return collageFile, nil
} }

9
main_test.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "testing"
func TestNameGen(t *testing.T) {
g := NewNameGen()
t.Logf("next id: %s", g.Next())
t.Logf("next id: %s", g.Next())
}

35
namegen.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"crypto/rand"
"fmt"
"sync/atomic"
"time"
)
type nameGen struct {
prefix string
counter atomic.Uint64
}
func NewNameGen() *nameGen {
currentTime := time.Now().Unix()
randBytes := make([]byte, 8)
_, err := rand.Read(randBytes)
if err != nil {
panic(err)
}
alpha := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
uniqRunID := ""
for _, b := range randBytes {
uniqRunID += string(alpha[int(b)%len(alpha)])
}
return &nameGen{
prefix: fmt.Sprintf("%d-%s", currentTime, uniqRunID),
counter: atomic.Uint64{},
}
}
func (n *nameGen) Next() string {
return fmt.Sprintf("%s-%d", n.prefix, n.counter.Add(1))
}

View File

@ -1,24 +0,0 @@
package main
type Resolution struct {
X uint
Y uint
}
// increased by factor 1.5
// https://www.adorama.com/alc/pixels-and-printing-size-matters/
var Letter = Resolution{
X: 3264,
Y: 4224,
}
var FiveXSeven = Resolution{
X: 2250,
Y: 3150,
}
var FourXSix = Resolution{
X: 1800,
Y: 2700,
}

View File

@ -1,36 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
<style>
.paper {
width: 8.5in;
height: 11in;
overflow: hidden;
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
background-color: gainsboro;
}
.img1, .img2 {
width: 7in;
height: 5in;
}
.img1 {
background-color: blue;
}
.img2 {
background-color: yellow;
}
</style>
</head>
<body>
<div class="paper">
<div class="img1"></div>
<div class="img2"></div>
</div>
</body>
</html>

1
web/croppie.min.js vendored Normal file

File diff suppressed because one or more lines are too long

137
web/index.css Normal file
View File

@ -0,0 +1,137 @@
.container {
display: flex;
background-color: lightyellow;
height: calc(100vh - 20px);
}
.controls {
background-color: lightgrey;
display: flex;
align-items: center;
justify-content: space-around;
flex-direction: column;
flex: 25%;
overflow: scroll;
}
.img-container {
/** margin: 30px; **/
display: flex;
gap: 1rem;
flex-wrap: wrap;
justify-content: center;
}
.img-container img {
width: 100px;
height: 100px;
border: 5px solid #999;
}
.img-container p {
text-align: center;
font-size: 2rem;
}
.toolbar {
height: 30px;
display: flex;
gap: 1rem;
justify-content: center;
align-items: flex-start;
}
.selected-img {
border: 5px solid lightgreen !important;
}
.imagebox {
padding: 2rem;
flex: 75%;
}
.image-surface {
overflow: hidden;
margin: auto;
border: 1px solid;
height: 100%;
width: auto;
--collage-ap: 110 / 85;
aspect-ratio: var(--collage-ap);
}
.showbuton {
font-size: 2rem;
}
.templates {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.templates li {
list-style-type: none;
}
#page_size_selector {
width: 100%;
}
.template {
width: 100px;
height: 100px;
}
.template-selected {
background-color: green;
}
.template div {
border: 1px solid;
}
.single div {
width: 100%;
height: 100%;
}
.one-two {
display: grid;
grid-template-areas:
"one two"
"one three";
}
.one-two .img1{
grid-area: one;
}
.half-leftright {
display:flex;
}
.half-leftright .img {
flex: 50%;
}
.half-topbottom .img {
width: 100%;
height: 50%;
}
.two-one-two-leftright {
display: grid;
grid-template-areas:
"one two two three"
"four two two five";
}
.two-one-two-leftright .img2 {
grid-area: two;
}
.two-one-two-topbottom {
display: grid;
grid-template-areas:
"one two"
"three three"
"three three"
"four five"
}
.two-one-two-topbottom .img3 {
grid-area: three;
}

97
web/index.html Normal file
View File

@ -0,0 +1,97 @@
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
<!-- DEVONLY --> <script src="http://localhost:35729/livereload.js"></script>
<link rel="stylesheet" href="croppie.css" />
<script src="croppie.min.js" defer></script>
<script src="index.js" defer></script>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="container">
<div class="controls">
<label>
<span>Paper size</span>
<select id="page_size_selector" size=8>
<option value="letter-portrait" > Letter (Portrait) </option>
<option selected value="letter-landscape" > Letter (Landscape) </option>
<option value="fiveseven-portrait" > 5 × 7 (Portrait) </option>
<option value="fiveseven-landscape" > 7 × 5 (Landscape) </option>
<option value="foursix-portrait" > 4 × 6 (Portrait) </option>
<option value="foursix-landscape" > 4 × 6 (Landscape) </option>
</select>
</label>
<div>
<div class="toolbar">
<button>Left</button>
<button>Right</button>
</div>
<div class="img-container">
<div> <img src="images/img1.jpg"/> </div>
<div> <img class="selected-img" src="images/img2.jpg"/> </div>
<div> <img src="images/img3.jpg"/> </div>
<div> <img src="images/img4.jpg"/> </div>
<div> <img src="images/img5.jpg"/> </div>
<div> <img src="images/img6.jpg"/> </div>
</div>
</div>
<ul class="templates">
<li>
<div id="default_template" class="template single" data-collage-template="single">
<div class="img img1"></div>
</div>
</li>
<li>
<div class="template one-two" data-collage-template="one-two">
<div class="img img1"></div>
<div class="img img2"></div>
<div class="img img3"></div>
</div>
</li>
<li>
<div class="template half-leftright" data-collage-template="half-leftright">
<div class="img img1"></div>
<div class="img img2"></div>
</div>
</li>
<li>
<div class="template half-topbottom" data-collage-template="half-topbottom">
<div class="img img1"></div>
<div class="img img2"></div>
</div>
</li>
<li>
<div class="template two-one-two-leftright" data-collage-template="two-one-two-leftright">
<div class="img img1"></div>
<div class="img img2"></div>
<div class="img img3"></div>
<div class="img img4"></div>
<div class="img img5"></div>
</div>
</li>
<li>
<div class="template two-one-two-topbottom" data-collage-template="two-one-two-topbottom">
<div class="img img1"></div>
<div class="img img2"></div>
<div class="img img3"></div>
<div class="img img4"></div>
<div class="img img5"></div>
</div>
</li>
</ul>
<button id="snapper" class="showbuton">Snap Collage</button>
<p><a href="" target="_blank" id="collage-url"></a></p>
</div>
<div class="imagebox">
<div id="collage" class="image-surface"></div>
</div>
</div>
</body>
</html>

213
web/index.js Normal file
View File

@ -0,0 +1,213 @@
"use strict";
const pageSizes = {
"letter-portrait" : {
"ap": "85 / 110",
"width": 3264,
"height": 4224,
},
"letter-landscape" : {
"ap": "110 / 85",
"width": 4224,
"height": 3264,
},
"fiveseven-portrait" : {
"ap": "5 / 7",
"width": 2250,
"height": 3150,
},
"fiveseven-landscape" : {
"ap": "7 / 5",
"width": 3150,
"height": 2250,
},
"foursix-portrait" : {
"ap": "4 / 6",
"width": 1800,
"height": 2700,
},
"foursix-landscape" : {
"ap": "6 / 4",
"width": 2700,
"height": 1800,
},
}
// elements
/** @type {HTMLButtonElement} */
let snapButton
/** @type {HTMLDivElement} */
let collageDiv
/** @type {HTMLSelectElement} */
let pageSizeSelect
/** @type {HTMLAnchorElement} */
let collageUrlA
// collage state
let crops = [];
let pageSize = "letter-landscape";
const imageUrls = [
, // images start with index 1
"images/img1.jpg",
"images/img2.jpg",
"images/img3.jpg",
"images/img4.jpg",
"images/img5.jpg",
"images/img6.jpg",
"images/img7.jpg",
]
function main() {
snapButton = document.getElementById("snapper")
collageDiv = document.getElementById("collage")
pageSizeSelect = document.getElementById("page_size_selector")
collageUrlA = document.getElementById("collage-url")
snapButton.onclick = () => snap()
pageSizeSelect.onchange = () => pageSizeChange()
for(const tmpl of document.getElementsByClassName("template")) {
tmpl.onclick = () => applyTemplate(tmpl)
}
applyTemplate(document.getElementById("default_template"))
}
/**
* @param {HTMLDivElement} elem
* @param {string} imgUrl
*/
function makeCroppieElem(elem, imgUrl) {
const cpie = new Croppie(elem, {
viewport: {
width: elem.clientWidth,
height: elem.clientHeight,
type: 'square'
},
showZoomer: false,
});
cpie.bind({
url: imgUrl
});
return cpie
}
function initCollage() {
for(const cpie of crops) {
cpie.destroy()
}
crops = []
for(const elem of collageDiv.getElementsByClassName("img")) {
const cpie = makeCroppieElem(elem, elem.dataset.collageImageUrl)
const lastLen = crops.push(cpie)
elem.dataset.collageCropieIndex = lastLen - 1
}
}
async function makeCollage(req) {
const resp = await fetch("make-collage", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(req),
})
return await resp.text();
}
function snap() {
const col = collageDiv.offsetLeft;
const cot = collageDiv.offsetTop;
const req = {
background_image: "",
aspect: {
width: pageSizes[pageSize]["width"],
height: pageSizes[pageSize]["height"],
},
dimension: {
width: collageDiv.clientWidth,
height: collageDiv.clientHeight,
},
photos: [],
};
for(const elem of collageDiv.getElementsByClassName("img")) {
const cpie = crops[elem.dataset.collageCropieIndex]
const fsx = elem.offsetLeft - col
const fsy = elem.offsetTop - cot
const [sx, sy, ex, ey] = cpie.get().points;
const photo = {
image: elem.dataset.collageImageUrl.slice("images/".length),
crop: {
start: {
x: parseInt(sx),
y: parseInt(sy),
},
end: {
x: parseInt(ex),
y: parseInt(ey),
},
},
frame: {
start: {
x: fsx,
y: fsy,
},
end: {
x: fsx + elem.clientWidth,
y: fsy + elem.clientHeight,
},
},
};
req.photos.push(photo)
}
(async () => {
const collagFile = await makeCollage(req)
collageUrlA.href = `collages/${collagFile}`;
collageUrlA.text = `${collagFile} generated`;
})();
}
function pageSizeChange() {
collageDiv.style.setProperty('--collage-ap', pageSizes[pageSizeSelect.value]["ap"])
pageSize = pageSizeSelect.value
initCollage()
}
/**
* @param {HTMLDivElement} templateDiv
*/
function applyTemplate(templateDiv) {
document.getElementsByClassName("template-selected").item(0)?.classList.remove("template-selected")
/** @type {HTMLDivElement} */
const templateClone = templateDiv.cloneNode(true)
templateDiv.classList.add("template-selected")
for (const index in imageUrls) {
const url = imageUrls[index]
const imgClass = `img${index}`
const [imgDiv] = templateClone.getElementsByClassName(imgClass)
if(imgDiv === undefined) {
break;
}
imgDiv.dataset.collageImageUrl = url;
}
collageDiv.replaceChildren(...templateClone.children)
collageDiv.classList.remove(collageDiv.dataset.collageTemplate)
collageDiv.classList.add(templateDiv.dataset.collageTemplate)
collageDiv.dataset.collageTemplate = templateDiv.dataset.collageTemplate
initCollage()
}
main()

12
web/upload.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<!-- DEVONLY --> <script src="http://localhost:35729/livereload.js"></script>
</head>
<body>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input name="photos" type=file multiple accept="image/*" />
<input type=submit>
</form>
</body>
</html>