コード例 #1
0
ファイル: http.go プロジェクト: LeXa4894/test
// upload is the HTTP handler for uploading images; it handles "/".
func upload(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		// No upload; show the upload form.
		templates.ExecuteTemplate(w, "upload.html", nil)
		return
	}

	f, _, err := r.FormFile("image")
	check(err)
	defer f.Close()

	// Grab the image data
	var buf bytes.Buffer
	io.Copy(&buf, f)
	i, _, err := image.Decode(&buf)
	check(err)

	// Resize if too large, for more efficient moustachioing.
	// We aim for less than 1200 pixels in any dimension; if the
	// picture is larger than that, we squeeze it down to 600.
	const max = 1200
	if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
		// If it's gigantic, it's more efficient to downsample first
		// and then resize; resizing will smooth out the roughness.
		if b.Dx() > 2*max || b.Dy() > 2*max {
			w, h := max, max
			if b.Dx() > b.Dy() {
				h = b.Dy() * h / b.Dx()
			} else {
				w = b.Dx() * w / b.Dy()
			}
			i = resize.Resample(i, i.Bounds(), w, h)
			b = i.Bounds()
		}
		w, h := max/2, max/2
		if b.Dx() > b.Dy() {
			h = b.Dy() * h / b.Dx()
		} else {
			w = b.Dx() * w / b.Dy()
		}
		i = resize.Resize(i, i.Bounds(), w, h)
	}

	// Encode as a new JPEG image.
	buf.Reset()
	err = jpeg.Encode(&buf, i, nil)
	check(err)

	// Create an App Engine context for the client's request.
	c := appengine.NewContext(r)

	// Save the image under a unique key, a hash of the image.
	key := datastore.NewKey(c, "Image", keyOf(buf.Bytes()), 0, nil)
	_, err = datastore.Put(c, key, &Image{buf.Bytes()})
	check(err)

	// Redirect to /edit using the key.
	http.Redirect(w, r, "/edit?id="+key.StringID(), http.StatusFound)
}
コード例 #2
0
ファイル: image.go プロジェクト: strickyak/goterp
func cmdImageResample(fr *Frame, argv []T) T {
	imgT, widthT, heightT := Arg3(argv)

	img := imgT.Raw().(image.Image)
	width := int(widthT.Int())
	height := int(heightT.Int())

	z := resize.Resample(img, img.Bounds(), width, height)
	return MkValue(R.ValueOf(z))
}
コード例 #3
0
ファイル: i.go プロジェクト: prinsmike/go-resize
func handler(w http.ResponseWriter, r *http.Request) {
	get := func(n string) int {
		i, _ := strconv.Atoi(r.FormValue(n))
		return i
	}
	myurl := "https://s3.amazonaws.com/lenses_s3/uploads/IMG_1762.JPG"
	res, err := http.Get(myurl)
	if err != nil {
		log.Fatal(err)
	}
	img, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatal(err)

	}
	res.Body.Close()
	rdr := bytes.NewReader(img)
	i, _, err := image.Decode(rdr)
	check(err)
	// Resize if too large, for more efficient moustachioing.
	// We aim for less than 1200 pixels in any dimension; if the
	// picture is larger than that, we squeeze it down to 600.
	max := 0
	if x := get("x"); x != 0 {
		max = x
	} else {
		max = 1200
	}
	if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
		// If it's gigantic, it's more efficient to downsample first
		// and then resize; resizing will smooth out the roughness.
		if b.Dx() > 2*max || b.Dy() > 2*max {
			w, h := max, max
			if b.Dx() > b.Dy() {
				h = b.Dy() * h / b.Dx()
			} else {
				w = b.Dx() * w / b.Dy()
			}
			i = resize.Resample(i, i.Bounds(), w, h)
			b = i.Bounds()
		}
		w, h := max/2, max/2
		if b.Dx() > b.Dy() {
			h = b.Dy() * h / b.Dx()
		} else {
			w = b.Dx() * w / b.Dy()
		}
		i = resize.Resize(i, i.Bounds(), w, h)
	}
	// Deliver image
	w.Header().Set("Content-type", "image/jpeg")
	jpeg.Encode(w, i, nil)
}
コード例 #4
0
ファイル: dpcompare.go プロジェクト: hernan43/dpcmp
// 99.9% of this was stolen from
// http://goo.gl/lgkTC
func extractImageFromPost(name string, r *http.Request) []byte {
	f, _, err := r.FormFile(name)
	check(err)
	defer f.Close()

	// Grab the image data
	buf := new(bytes.Buffer)
	io.Copy(buf, f)
	i, _, err := image.Decode(buf)
	check(err)

	// We aim for less than 400 pixels in any dimension; if the
	// picture is larger than that, we squeeze it down
	const max = 800
	if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
		// If it's gigantic, it's more efficient to downsample first
		// and then resize; resizing will smooth out the roughness.
		if b.Dx() > 2*max || b.Dy() > 2*max {
			w, h := max, max
			if b.Dx() > b.Dy() {
				h = b.Dy() * h / b.Dx()
			} else {
				w = b.Dx() * w / b.Dy()
			}
			i = resize.Resample(i, i.Bounds(), w, h)
			b = i.Bounds()
		}
		w, h := max/2, max/2
		//w, h := max, max
		if b.Dx() > b.Dy() {
			h = b.Dy() * h / b.Dx()
		} else {
			w = b.Dx() * w / b.Dy()
		}
		i = resize.Resize(i, i.Bounds(), w, h)
	}

	// Encode as a new JPEG image.
	buf.Reset()
	err = jpeg.Encode(buf, i, &jpeg.Options{Quality: 95})
	check(err)

	// return JPEG
	return buf.Bytes()
}
コード例 #5
0
ファイル: pic_wh_tuing.go プロジェクト: bigknife/fileserver
//调整图片分辨率,生成新的分辨率的图片文件
// PUT /pic/width-height , body : {name=/a.jpg&width=40&height=30}
// 生成文件: /a.jpg.40.30.jpg 如果该文件已经存在,则直接返回
func TuningPicWH(w http.ResponseWriter, r *http.Request, serverConfig map[string]string) {
	name, sWidth, sHeight := r.FormValue("name"), r.FormValue("width"), r.FormValue("height")
	mylog.Info("put /pic/width-height, {name = %s}", name)

	if name == "" {
		panic(errors.New("Put /pic/width-height interface need 'name' param"))
	}
	if sWidth == "" {
		panic(errors.New("Put /pic/width-height interface need 'width' param"))
	}
	if sHeight == "" {
		panic(errors.New("Put /pic/width-height interface need 'height' param"))
	}

	width, err := strconv.ParseInt(sWidth, 10, 0)
	if err != nil {
		panic(err)
	}
	height, err := strconv.ParseInt(sHeight, 10, 0)
	if err != nil {
		panic(err)
	}

	storePath, _ := serverConfig["storePath"]

	absPath := storePath + "/" + strings.Trim(name, "/")

	sufix := getSufix(name)

	tunedAbsPath := absPath + "." + sWidth + "." + sHeight + "." + sufix
	_, err = os.Open(tunedAbsPath)
	if err != nil {
		//文件不存在,进行调整,生成新的文件
		inFile, err := os.Open(absPath)
		if err != nil {
			panic(err)
		}
		defer inFile.Close()
		img, _, err := image.Decode(inFile)
		if err != nil {
			panic(err)
		}

		newImg := resize.Resample(img, image.Rect(0, 0, img.Bounds().Max.X, img.Bounds().Max.Y), int(width), int(height))
		outFile, err := os.Create(tunedAbsPath)

		if err != nil {
			panic(err)
		}
		defer outFile.Close()

		if sufix == "jpg" || sufix == "jpeg" || sufix == "JPG" || sufix == "JPEG" {
			err = jpeg.Encode(outFile, newImg, &jpeg.Options{100})
			if err != nil {
				panic(err)
			}
		} else if sufix == "png" || sufix == "PNG" {
			err = png.Encode(outFile, newImg)
			if err != nil {
				panic(err)
			}
		}

	}

}
コード例 #6
0
ファイル: imagectl.go プロジェクト: clicker360/ebfmex-pub
// upload is the HTTP handler for uploading images; it handles "/".
func upload(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	var imgprops appimage.ServingURLOptions
	imgprops.Secure = true
	imgprops.Size = 180
	imgprops.Crop = false
	if s, ok := sess.IsSess(w, r, c); ok {
		u, _ := model.GetCta(c, s.User)
		emp, err := u.GetEmpresa(c, r.FormValue("IdEmp"))
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		imgo := model.GetLogo(c, r.FormValue("IdEmp"))
		if imgo == nil {
			imgo = new(model.Image)
			imgo.IdEmp = emp.IdEmp
		}
		fd := imgToForm(*imgo)
		tc := make(map[string]interface{})
		tc["Sess"] = s
		tc["Empresa"] = emp
		tc["FormData"] = fd
		if r.Method != "POST" {
			// No upload; show the upload form.
			micrositio(w, r)
			return
		}

		idemp := r.FormValue("IdEmp")
		sp1 := r.FormValue("Sp1")
		sp2 := r.FormValue("Sp2")
		f, _, err := r.FormFile("image")
		model.Check(err)
		defer f.Close()

		// Grab the image data
		var buf bytes.Buffer
		io.Copy(&buf, f)
		i, _, err := image.Decode(&buf)
		if err != nil {
			if r.FormValue("tipo") == "async" {
				//w.Header().Set("Content-Type", "application/json")
				fmt.Fprintf(w, "<p>'%s'</p>", "No se actualizó el logotipo, formato no aceptado")
			} else {
				tc["Error"] = struct{ Badformat string }{"badformat"}
				micrositioTpl.Execute(w, tc)
			}
			return
		}
		const max = 600
		// We aim for less than max pixels in any dimension.
		if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
			// If it's gigantic, it's more efficient to downsample first
			// and then resize; resizing will smooth out the roughness.
			if b.Dx() > 2*max || b.Dy() > 2*max {
				w, h := max*2, max*2
				if b.Dx() > b.Dy() {
					h = b.Dy() * h / b.Dx()
				} else {
					w = b.Dx() * w / b.Dy()
				}
				i = resize.Resample(i, i.Bounds(), w, h)
				b = i.Bounds()
			}
			w, h := max, max
			if b.Dx() > b.Dy() {
				h = b.Dy() * h / b.Dx()
			} else {
				w = b.Dx() * w / b.Dy()
			}
			i = resize.Resize(i, i.Bounds(), w, h)
		}

		// Encode as a new JPEG image.
		buf.Reset()
		err = jpeg.Encode(&buf, i, nil)
		if err != nil {
			if r.FormValue("tipo") == "async" {
				fmt.Fprintf(w, "<p>'%s'</p>", "No se actualizó el logotipo, formato no aceptado")
			} else {
				tc["Error"] = struct{ Badencode string }{"badencode"}
				micrositioTpl.Execute(w, tc)
			}
			return
		}
		var blobkey appengine.BlobKey
		blob, err := blobstore.Create(c, "image/jpeg")
		if err != nil {
			c.Errorf("blobstore Create: %v", idemp)
		}
		_, err = blob.Write(buf.Bytes())
		if err != nil {
			c.Errorf("blobstore Write: %v", idemp)
		}
		err = blob.Close()
		if err != nil {
			c.Errorf("blobstore Close: %v", idemp)
		}
		blobkey, err = blob.Key()
		if err != nil {
			c.Errorf("blobstore Key Gen: %v", idemp)
		}
		if url, err := appimage.ServingURL(c, blobkey, &imgprops); err != nil {
			c.Errorf("Cannot construct EmpLogo ServingURL : %v", idemp)
		} else {
			// Save the image under a unique key, a hash of the image.
			img := &model.Image{
				Data: buf.Bytes(), IdEmp: idemp, IdImg: model.RandId(20),
				Kind: "EmpLogo", Name: imgo.Name, Desc: imgo.Desc,
				Sizepx: 0, Sizepy: 0, Url: imgo.Url, Type: "",
				Sp1: sp1, Sp2: sp2, Sp3: string(blobkey), Sp4: url.String(),
				Np1: 0, Np2: 0, Np3: 0, Np4: 0,
			}

			_, err = model.PutLogo(c, img)
			if err != nil {
				if r.FormValue("tipo") == "async" {
					fmt.Fprintf(w, "<p>'%s'</p>", "No se actualizó el logotipo. Sistema en matenimiento, intente en unos minutos")
				} else {
					tc["Error"] = struct{ Cantsave string }{"cantsave"}
					micrositioTpl.Execute(w, tc)
				}
				return
			}
		}

		/*
			se crea icono
		*/
		val := slogores(c, idemp, 70, 0)
		if val != 0 {
			tc["Error"] = struct{ Cantsave string }{"cantsave"}
			micrositioTpl.Execute(w, tc)
			return
		}

		if r.FormValue("tipo") == "async" {
			fmt.Fprintf(w, "<p></p>")
		} else {
			micrositio(w, r)
		}
		return
	} else {
		http.Redirect(w, r, "/r/registro", http.StatusFound)
	}
}
コード例 #7
0
ファイル: simg.go プロジェクト: clicker360/ebfmex-pub
// Resize only if picture from EmpLogo is more than 80 pix width
// If resize, save image to entity king ShortLogo and stream
// If no resize is necesary, save to entity ShortLogo and stream
func rslogo(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	if r.Method == "GET" {
		sf, _ := strconv.Atoi(r.FormValue("s"))
		force, _ := strconv.Atoi(r.FormValue("force"))
		// Check for shortlogo
		var simg model.Image
		simg.Kind = "ShortLogo"
		if r.FormValue("IdEmp") != "" {
			simg.IdEmp = r.FormValue("IdEmp")
		} else {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		if force != 1 {
			shortlogo, _ := model.GetShortLogo(c, r.FormValue("IdEmp"))

			// Stream short logo if already exists
			if shortlogo != nil {
				w.Header().Set("Content-type", "image/jpeg")
				w.Write(shortlogo.Data)

				/*
					m, _, err := image.Decode(bytes.NewBuffer(shortlogo.Data))
					model.Check(err)
					w.Header().Set("Content-type", "image/jpeg")
					jpeg.Encode(w, m, nil)
				*/

				return
			}
		}

		// Process biglogo if shortlogo doesn't exists
		// Save and Stream new shortlogo
		biglogo := model.GetLogo(c, r.FormValue("IdEmp"))
		if biglogo == nil {
			// No such imageb
			w.WriteHeader(http.StatusNotFound)
			return
		}

		i, _, err := image.Decode(bytes.NewBuffer(biglogo.Data))
		if err != nil {
			// ERR_BADFORMAT
			w.WriteHeader(http.StatusNotFound)
			return
		}

		const max = 70
		if sf == 0 {
			// We aim for less than max pixels in any dimension.
			if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
				// If it's gigantic, it's more efficient to downsample first
				// and then resize; resizing will smooth out the roughness.
				if b.Dx() > 2*max || b.Dy() > 2*max {
					w, h := max*2, max*2
					if b.Dx() > b.Dy() {
						h = b.Dy() * h / b.Dx()
					} else {
						w = b.Dx() * w / b.Dy()
					}
					i = resize.Resample(i, i.Bounds(), w, h)
					b = i.Bounds()
				}
				w, h := max, max
				if b.Dx() > b.Dy() {
					h = b.Dy() * h / b.Dx()
				} else {
					w = b.Dx() * w / b.Dy()
				}
				i = resize.Resize(i, i.Bounds(), w, h)
			} else {
				w, h := max, max
				if b.Dx() > b.Dy() {
					h = b.Dy() * h / b.Dx()
				} else {
					w = b.Dx() * w / b.Dy()
				}
				i = resize.Resize(i, i.Bounds(), w, h)
			}
		} else {
			// We aim for a resize by ratio.
			b := i.Bounds()
			h := b.Dy()
			w := b.Dx()
			if sf > 0 && sf <= 2 {
				h = h * sf
				w = w * sf
			} else if sf < 0 && sf > -1 {
				sf = (sf * 2) + sf
				h = h * (1 / sf)
				w = w * (1 / sf)
			}
			i = resize.Resize(i, i.Bounds(), w, h)
		}

		// Encode as a new JPEG image.
		var buf bytes.Buffer
		err = jpeg.Encode(&buf, i, nil)
		if err != nil {
			// ERR_FAIL_ENCODE
			w.WriteHeader(http.StatusNotFound)
			return
		}

		// Save the image under a unique key, a hash of the image.
		simg.IdImg = model.RandId(20)
		simg.Data = buf.Bytes()
		simg.Name = biglogo.Name
		simg.Desc = biglogo.Desc
		simg.Sizepx = max
		simg.Sizepy = 0
		simg.Url = biglogo.Url
		simg.Type = "jpeg"
		simg.Sp1 = biglogo.Sp1
		simg.Sp2 = biglogo.Sp2
		simg.Sp3 = biglogo.Sp3
		simg.Sp4 = biglogo.Sp4
		simg.Np1 = biglogo.Np1
		simg.Np2 = biglogo.Np2
		simg.Np3 = biglogo.Np3
		simg.Np4 = biglogo.Np4

		_, err = model.PutLogo(c, &simg)
		if err != nil {
			// ERR_DATASTORE
			// Don't return, stream image either way
		}
		w.Header().Set("Content-type", "image/jpeg")
		w.Write(simg.Data)
		/*
			m, _, err := image.Decode(bytes.NewBuffer(simg.Data))
			model.Check(err)
			w.Header().Set("Content-type", "image/jpeg")
			jpeg.Encode(w, m, nil)
		*/
	}
}
コード例 #8
0
ファイル: simg.go プロジェクト: clicker360/ebfmex-pub
func slogores(c appengine.Context, id string, max int, sf int) int {
	var simg model.Image
	// Process biglogo if shortlogo doesn't exists
	// Save and Stream new shortlogo
	biglogo := model.GetLogo(c, id)
	if biglogo == nil {
		// No such imageb
		return -1
	}

	i, _, err := image.Decode(bytes.NewBuffer(biglogo.Data))
	if err != nil {
		// ERR_BADFORMAT
		return -1
	}

	if sf == 0 {
		// We aim for less than 80 pixels in any dimension.
		if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
			// If it's gigantic, it's more efficient to downsample first
			// and then resize; resizing will smooth out the roughness.
			if b.Dx() > 2*max || b.Dy() > 2*max {
				w, h := max*2, max*2
				if b.Dx() > b.Dy() {
					h = b.Dy() * h / b.Dx()
				} else {
					w = b.Dx() * w / b.Dy()
				}
				i = resize.Resample(i, i.Bounds(), w, h)
				b = i.Bounds()
			}
			w, h := max, max
			if b.Dx() > b.Dy() {
				h = b.Dy() * h / b.Dx()
			} else {
				w = b.Dx() * w / b.Dy()
			}
			i = resize.Resize(i, i.Bounds(), w, h)
		} else {
			w, h := max, max
			if b.Dx() > b.Dy() {
				h = b.Dy() * h / b.Dx()
			} else {
				w = b.Dx() * w / b.Dy()
			}
			i = resize.Resize(i, i.Bounds(), w, h)
		}
	} else {
		// We aim for a resize by ratio.
		b := i.Bounds()
		h := b.Dy()
		w := b.Dx()
		if sf > 0 && sf <= 2 {
			h = h * sf
			w = w * sf
		} else if sf < 0 && sf > -1 {
			sf = (sf * 2) + sf
			h = h * (1 / sf)
			w = w * (1 / sf)
		}
		i = resize.Resize(i, i.Bounds(), w, h)
	}

	// Encode as a new JPEG image.
	var buf bytes.Buffer
	err = jpeg.Encode(&buf, i, nil)
	if err != nil {
		// ERR_FAIL_ENCODE
		return -1
	}

	// Save the image under a unique key, a hash of the image.
	shortlogo, _ := model.GetShortLogo(c, id)
	simg.Kind = "ShortLogo"
	if shortlogo != nil {
		simg.IdImg = shortlogo.IdImg
	} else {
		simg.IdImg = model.RandId(20)
	}
	simg.IdEmp = id
	simg.Data = buf.Bytes()
	simg.Name = biglogo.Name
	simg.Desc = biglogo.Desc
	simg.Sizepx = max
	simg.Sizepy = 0
	simg.Url = biglogo.Url
	simg.Type = "jpeg"
	simg.Sp1 = biglogo.Sp1
	simg.Sp2 = biglogo.Sp2
	simg.Sp3 = biglogo.Sp3
	simg.Sp4 = biglogo.Sp4
	simg.Np1 = biglogo.Np1
	simg.Np2 = biglogo.Np2
	simg.Np3 = biglogo.Np3
	simg.Np4 = biglogo.Np4

	_, err = model.PutLogo(c, &simg)
	if err != nil {
		return -1
	}
	return 0
}
コード例 #9
0
ファイル: http.go プロジェクト: 9nut/jpg2txt
// handler for '/'; if the request is a POST try to convert the image
func upload(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	if r.Method != "POST" {
		// No upload; show the upload form.
		if err := uploadTemplate.Execute(w, ""); err != nil {
			c.Errorf("Can't execute uploadTempl: ", err)
		}
		return
	}

	// Grab the image data
	var buf bytes.Buffer
	f, _, err := r.FormFile("image")
	if err != nil {
		u := r.FormValue("url")
		// c.Infof("about to fetch %v\n", u)
		if len(u) == 0 {
			if err = uploadTemplate.Execute(w, "");  err != nil {
				c.Errorf("Can't execute uploadTempalte:", err)
			}
			return
		}
		client := urlfetch.Client(c)
		resp, err := client.Get(u)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		defer resp.Body.Close()
		buf.Reset()
		io.Copy(&buf, resp.Body)
	} else {
		defer f.Close()
		io.Copy(&buf, f)
	}

	// c.Infof("length of buffer is: %d\n", buf.Len())
	i, _, err := image.Decode(&buf)
	check(err)

	const max = 300
	if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
		// If it's gigantic, it's more efficient to downsample first
		// and then resize; resizing will smooth out the roughness.
		if b.Dx() > 2*max || b.Dy() > 2*max {
			w, h := max, max
			if b.Dx() > b.Dy() {
				h = b.Dy() * h / b.Dx()
			} else {
				w = b.Dx() * w / b.Dy()
			}
			i = resize.Resample(i, i.Bounds(), w, h)
			b = i.Bounds()
		}
		w, h := max/2, max/2
		if b.Dx() > b.Dy() {
			h = b.Dy() * h / b.Dx()
		} else {
			w = b.Dx() * w / b.Dy()
		}
		i = resize.Resize(i, i.Bounds(), w, h)
	}

	// Encode as a new ascii image
	buf.Reset()
	err = Encode(&buf, i)
	check(err)
	OutBuf := string(buf.Bytes())
	// c.Infof("Done converting; buf:\n%v\n", OutBuf)

	w.Header().Set("Content-type", "text/html")
	uploadTemplate.Execute(w, OutBuf)
}