Skip to content

Commit 76dfaca

Browse files
committed
...
0 parents  commit 76dfaca

File tree

8 files changed

+421
-0
lines changed

8 files changed

+421
-0
lines changed

bindata.go

Lines changed: 244 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module dhvcc/resize
2+
3+
go 1.16
4+
5+
require (
6+
github.com/andybalholm/brotli v1.0.3 // indirect
7+
github.com/aws/aws-lambda-go v1.26.0 // indirect
8+
github.com/disintegration/imaging v1.6.2 // indirect
9+
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
10+
github.com/klauspost/compress v1.13.5 // indirect
11+
github.com/valyala/fasthttp v1.29.0 // indirect
12+
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
13+
)

go.sum

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
3+
github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM=
4+
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
5+
github.com/aws/aws-lambda-go v1.26.0 h1:6ujqBpYF7tdZcBvPIccs98SpeGfrt/UOVEiexfNIdHA=
6+
github.com/aws/aws-lambda-go v1.26.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
7+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
8+
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
12+
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
13+
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
14+
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
15+
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
16+
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
17+
github.com/klauspost/compress v1.13.5 h1:9O69jUPDcsT9fEm74W92rZL9FQY7rCdaXVneq+yyzl4=
18+
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
19+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
21+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
22+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
23+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
24+
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
25+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
26+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
27+
github.com/valyala/fasthttp v1.29.0 h1:F5GKpytwFk5OhCuRh6H+d4vZAcEeNAwPTdwQnm6IERY=
28+
github.com/valyala/fasthttp v1.29.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
29+
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
30+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
31+
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
32+
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs=
33+
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
34+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
35+
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
36+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
37+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
38+
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
39+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
40+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
41+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
42+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
43+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
44+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
45+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
46+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
47+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

lambda.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"crypto/tls"
7+
"encoding/base64"
8+
"fmt"
9+
"github.com/aws/aws-lambda-go/events"
10+
"github.com/aws/aws-lambda-go/lambda"
11+
"github.com/disintegration/imaging"
12+
"image"
13+
"image/color"
14+
"net/http"
15+
"time"
16+
)
17+
18+
const width = 1200
19+
const height = 630
20+
const minHeight = 250
21+
22+
func convertImage(url string) image.Image {
23+
fmt.Printf("====> Should convert url: %s.\n", url)
24+
asset, err := Asset("logo.png")
25+
if err != nil {
26+
return imaging.New(1, 1, color.Black)
27+
}
28+
background, _ := imaging.Decode(bytes.NewReader(asset))
29+
30+
fmt.Printf("Started request.\n")
31+
tr := &http.Transport{}
32+
tr.TLSClientConfig = &tls.Config{
33+
NextProtos: []string{"h1"},
34+
}
35+
36+
client := &http.Client{
37+
Timeout: 5 * time.Second,
38+
Transport: tr,
39+
}
40+
imageResp, err := client.Get(url)
41+
if err != nil {
42+
panic(err)
43+
}
44+
fmt.Println(imageResp)
45+
//imageResp, _ := http.Get(url)
46+
//defer imageResp.Body.Close()
47+
48+
fmt.Printf("Finished request.\n")
49+
image, _ := imaging.Decode(imageResp.Body)
50+
image_h := image.Bounds().Max.Y
51+
52+
if image_h < minHeight {
53+
image = imaging.Resize(image, 0, minHeight, imaging.Lanczos)
54+
image_h = image.Bounds().Max.Y
55+
}
56+
57+
output := imaging.Resize(background, 0, image_h, imaging.Lanczos)
58+
output = imaging.PasteCenter(output, image)
59+
fmt.Printf("====> Returning request.\n")
60+
return image
61+
}
62+
63+
func encodeImage(image image.Image) string {
64+
var buf bytes.Buffer
65+
fmt.Printf("====> Start encoding.\n")
66+
b64encoder := base64.NewEncoder(base64.StdEncoding, &buf)
67+
imaging.Encode(b64encoder, image, imaging.JPEG)
68+
b64encoder.Close()
69+
encoded := buf.String()
70+
fmt.Printf("====> Ended encoding.\n")
71+
return encoded
72+
}
73+
74+
func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
75+
fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
76+
body := encodeImage(convertImage(request.QueryStringParameters["image"]))
77+
headers := map[string]string{"content-type": "image/png"}
78+
79+
return events.APIGatewayProxyResponse{Body: body, StatusCode: 200, Headers: headers}, nil
80+
}
81+
82+
func main() {
83+
lambda.Start(HandleRequest)
84+
}

main

8.75 MB
Binary file not shown.

main.zip

4.58 MB
Binary file not shown.

other/logo.png

38.7 KB
Loading

other/resise_file.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"github.com/disintegration/imaging"
6+
"bytes"
7+
)
8+
9+
const width = 1200
10+
const height = 630
11+
const minHeight = 250
12+
13+
const mangaUrl = "https://static.readmanga.live/uploads/pics/01/41/131_p.jpg"
14+
15+
func main() {
16+
asset, _ := Asset("logo.png")
17+
background, _ := imaging.Decode(bytes.NewReader(asset))
18+
19+
imageResp, _ := http.Get(mangaUrl)
20+
defer imageResp.Body.Close()
21+
22+
image, _ := imaging.Decode(imageResp.Body)
23+
image_h := image.Bounds().Max.Y
24+
25+
if image_h < minHeight {
26+
image = imaging.Resize(image, 0, minHeight, imaging.Lanczos)
27+
image_h = image.Bounds().Max.Y
28+
}
29+
30+
output := imaging.Resize(background, 0, image_h, imaging.Lanczos)
31+
output = imaging.PasteCenter(output, image)
32+
imaging.Save(output, "test.png")
33+
}

0 commit comments

Comments
 (0)