Browse Source

convert new images to PNG

master
arnaucode 7 years ago
parent
commit
5a47f73020
2 changed files with 13 additions and 3 deletions
  1. +6
    -3
      handlers.go
  2. +7
    -0
      imageOperations.go

+ 6
- 3
handlers.go

@ -5,6 +5,7 @@ import (
"image/jpeg"
"io/ioutil"
"net/http"
"strings"
"github.com/gorilla/mux"
)
@ -36,6 +37,8 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
}
imageName := strings.Split(handler.Filename, ".")[0]
fileName := imageName + ".png"
data, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
@ -46,14 +49,14 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "error al processar la imatge")
}
img = Resize(img)
data, err = imageToData(img, handler.Filename)
data, err = imageToPNG(img)
if err != nil {
fmt.Fprintln(w, "error al processar la imatge")
}
err = ioutil.WriteFile(handler.Filename, data, 0777)
err = ioutil.WriteFile(fileName, data, 0777)
if err != nil {
fmt.Println(err)
}
fmt.Fprintln(w, "url:", handler.Filename)
fmt.Fprintln(w, "url:", fileName)
}

+ 7
- 0
imageOperations.go

@ -51,6 +51,13 @@ func imageToData(img image.Image, imageName string) ([]byte, error) {
return buf.Bytes(), err
}
func imageToPNG(img image.Image) ([]byte, error) {
buf := new(bytes.Buffer)
var err error
err = png.Encode(buf, img)
return buf.Bytes(), err
}
func Resize(img image.Image) image.Image {
r := resize.Resize(160, 0, img, resize.Lanczos3)
return r

Loading…
Cancel
Save