Browse Source

implemented resizing of new images before saving

master
arnaucode 7 years ago
parent
commit
bd717bccc2
3 changed files with 79 additions and 23 deletions
  1. +10
    -0
      README.md
  2. +12
    -23
      handlers.go
  3. +57
    -0
      imageOperations.go

+ 10
- 0
README.md

@ -1,2 +1,12 @@
# goImgCDN # goImgCDN
CDN for images, written in Go lang CDN for images, written in Go lang
upload example (with curl):
```
curl -F file=@./image1.png http://127.0.0.1:3050/image
```
to get image:
```
http://127.0.0.1:3050/images/image1.png
```

+ 12
- 23
handlers.go

@ -2,13 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"image"
"image/jpeg" "image/jpeg"
"image/png"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -20,34 +16,17 @@ func Index(w http.ResponseWriter, r *http.Request) {
func ImageShow(w http.ResponseWriter, r *http.Request) { func ImageShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
imageName := vars["imageName"] 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 { if err != nil {
//log.Fatal(err)
//fmt.Fprintln(w, "la imatge no existeix al server")
fmt.Fprintln(w, err) 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 { if err != nil {
//log.Fatal(err)
fmt.Fprintln(w, "la imatge no existeix al server") fmt.Fprintln(w, "la imatge no existeix al server")
} else { } else {
file.Close()
jpeg.Encode(w, img, nil) // Write to the ResponseWriter jpeg.Encode(w, img, nil) // Write to the ResponseWriter
} }
} }
@ -61,6 +40,16 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
fmt.Println(err) 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) err = ioutil.WriteFile(handler.Filename, data, 0777)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

+ 57
- 0
imageOperations.go

@ -0,0 +1,57 @@
package main
import (
"bytes"
"image"
"image/jpeg"
"image/png"
"strings"
"github.com/nfnt/resize"
)
func dataToImage(data []byte, imageName string) (image.Image, error) {
reader := bytes.NewReader(data)
var imageExtension = strings.Split(imageName, ".")[1]
var img image.Image
var err error
switch imageExtension {
case "png":
img, err = png.Decode(reader)
case "jpg":
img, err = jpeg.Decode(reader)
case "jpeg":
img, err = jpeg.Decode(reader)
default:
img = nil
}
if err != nil {
return img, err
}
return img, err
}
func imageToData(img image.Image, imageName string) ([]byte, error) {
buf := new(bytes.Buffer)
var imageExtension = strings.Split(imageName, ".")[1]
var err error
switch imageExtension {
case "png":
err = png.Encode(buf, img)
case "jpg":
err = jpeg.Encode(buf, img, nil)
case "jpeg":
err = jpeg.Encode(buf, img, nil)
default:
img = nil
}
if err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), err
}
func Resize(img image.Image) image.Image {
r := resize.Resize(160, 0, img, resize.Lanczos3)
return r
}

Loading…
Cancel
Save