5
5
"encoding/base64"
6
6
"fmt"
7
7
"image"
8
- "image/color"
9
8
"net/http"
10
9
11
10
"github.com/disintegration/imaging"
@@ -17,12 +16,6 @@ const minHeight = 250
17
16
18
17
func ConvertImage (url string ) image.Image {
19
18
fmt .Printf ("====> Should convert url: %s.\n " , url )
20
- asset , err := Asset ("assets/logo.png" )
21
- if err != nil {
22
- fmt .Println ("Can't load asset, falling back to black square" )
23
- return imaging .New (width , height , color .Black )
24
- }
25
- background , _ := imaging .Decode (bytes .NewReader (asset ))
26
19
27
20
fmt .Printf ("Started request.\n " )
28
21
imageResp , err := http .Get (url )
@@ -33,14 +26,21 @@ func ConvertImage(url string) image.Image {
33
26
34
27
fmt .Printf ("Finished request.\n " )
35
28
image , _ := imaging .Decode (imageResp .Body )
36
- image_h := image .Bounds ().Max .Y
29
+ imageH , imageW := image . Bounds (). Max . Y , image .Bounds ().Max .Y
37
30
38
- if image_h < minHeight {
31
+ if imageH < minHeight {
39
32
image = imaging .Resize (image , 0 , minHeight , imaging .Lanczos )
40
- image_h = image .Bounds ().Max .Y
33
+ imageH = image .Bounds ().Max .Y
34
+ } else if imageH > height || imageW > width {
35
+ image = imaging .Fit (image , width , height , imaging .Lanczos )
36
+ imageH = image .Bounds ().Max .Y
41
37
}
42
38
43
- output := imaging .Resize (background , 0 , image_h , imaging .Lanczos )
39
+ bgWidth := int (float64 (width ) / (float64 (height ) / float64 (imageH )))
40
+ background := imaging .Resize (image , bgWidth , imageH , imaging .Lanczos )
41
+ background = imaging .Blur (background , 20 )
42
+
43
+ output := imaging .Resize (background , 0 , imageH , imaging .Lanczos )
44
44
output = imaging .PasteCenter (output , image )
45
45
fmt .Printf ("====> Returning image %s.\n " , output .Bounds ().Max )
46
46
return output
0 commit comments