|
|
@ -2,13 +2,9 @@ package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"image" |
|
|
|
"image/jpeg" |
|
|
|
"image/png" |
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"github.com/gorilla/mux" |
|
|
|
) |
|
|
@ -20,34 +16,17 @@ func Index(w http.ResponseWriter, r *http.Request) { |
|
|
|
func ImageShow(w http.ResponseWriter, r *http.Request) { |
|
|
|
vars := mux.Vars(r) |
|
|
|
imageName := vars["imageName"] |
|
|
|
//fmt.Fprintln(w, "Image show:", imageName)
|
|
|
|
var imageExtension = strings.Split(imageName, ".")[1] |
|
|
|
|
|
|
|
file, err := os.Open(imageName) |
|
|
|
file, err := ioutil.ReadFile(imageName) |
|
|
|
if err != nil { |
|
|
|
//log.Fatal(err)
|
|
|
|
//fmt.Fprintln(w, "la imatge no existeix al server")
|
|
|
|
fmt.Fprintln(w, err) |
|
|
|
} |
|
|
|
|
|
|
|
var img image.Image |
|
|
|
switch imageExtension { |
|
|
|
case "png": |
|
|
|
img, err = png.Decode(file) |
|
|
|
case "jpg": |
|
|
|
img, err = jpeg.Decode(file) |
|
|
|
case "jpeg": |
|
|
|
img, err = jpeg.Decode(file) |
|
|
|
default: |
|
|
|
img = nil |
|
|
|
} |
|
|
|
img, err := dataToImage(file, imageName) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
//log.Fatal(err)
|
|
|
|
fmt.Fprintln(w, "la imatge no existeix al server") |
|
|
|
} else { |
|
|
|
file.Close() |
|
|
|
|
|
|
|
jpeg.Encode(w, img, nil) // Write to the ResponseWriter
|
|
|
|
} |
|
|
|
} |
|
|
@ -61,6 +40,16 @@ func NewImage(w http.ResponseWriter, r *http.Request) { |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
} |
|
|
|
|
|
|
|
img, err := dataToImage(data, handler.Filename) |
|
|
|
if err != nil { |
|
|
|
fmt.Fprintln(w, "error al processar la imatge") |
|
|
|
} |
|
|
|
img = Resize(img) |
|
|
|
data, err = imageToData(img, handler.Filename) |
|
|
|
if err != nil { |
|
|
|
fmt.Fprintln(w, "error al processar la imatge") |
|
|
|
} |
|
|
|
err = ioutil.WriteFile(handler.Filename, data, 0777) |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|