Esempio n. 1
0
func CropImage(img image.Image, rect image.Rectangle) (croped image.Image) {
	g := gift.New(
		gift.Crop(rect),
	)
	croped_image := image.NewRGBA(g.Bounds(rect))
	g.Draw(croped_image, img)
	return croped_image
}
Esempio n. 2
0
func (this *AvatarController) Cut() {
	r := this.Request
	data := e.Data{Status: 1, Msg: "上传成功"}
	r.ParseForm()
	x, err := strconv.Atoi(r.FormValue("x"))
	y, err := strconv.Atoi(r.FormValue("y"))
	x2, err := strconv.Atoi(r.FormValue("x2"))
	y2, err := strconv.Atoi(r.FormValue("y2"))

	fileName := r.FormValue("fileName")
	log.Println(x, y, x2, y2, fileName)
	g := gift.New(
		gift.Crop(image.Rect(x, y, x2, y2)),
		gift.Resize(220, 220, gift.LanczosResampling),
	)
	log.Println("图片名称" + fileName)
	//打开待处理的图片
	file, err := os.Open(fileName)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	m1, err := png.Decode(file)
	if err != nil {
		panic(err)
	}
	bounds := m1.Bounds()

	dst := image.NewRGBA(g.Bounds(bounds))
	g.Draw(dst, m1)
	cookie := u.Cookie{HTTP: this.HTTP}

	filename := cookie.GetCookie().UserId.Value + ".png"

	imgfile, err := os.Create(Avatar_ReDir + filename)
	os.Remove(Upload_Dir + filename)
	defer imgfile.Close()
	err = png.Encode(imgfile, dst)
	u.OutputJson(this.ResponseWriter, &data)
}
Esempio n. 3
0
func (processor *Processor) Execute(src *Image, dst io.Writer, query *Query) {
	processor.gift.Empty()
	quality := 100

	if query.Count() > 0 {
		// resize
		if query.Has("w") || query.Has("h") {
			width := query.GetInt("w")
			height := query.GetInt("h")

			if width > 0 || height > 0 {
				processor.gift.Add(gift.Resize(width, height, gift.LanczosResampling))
			}
		}

		// crop
		if query.Has("c") {
			c := query.GetIntArray("c")
			if len(c) == 4 {
				processor.gift.Add(gift.Crop(image.Rect(c[0], c[1], c[2], c[3])))
			}
		}

		// grayscale
		if query.Has("grayscale") {
			processor.gift.Add(gift.Grayscale())
		}

		// sepia
		if query.Has("sepia") {
			sepia := query.GetInt("sepia")
			if sepia <= 100 {
				processor.gift.Add(gift.Sepia(float32(sepia)))
			}
		}

		// contrast
		if query.Has("contrast") {
			contrast := query.GetInt("contrast")
			processor.gift.Add(gift.Contrast(float32(contrast)))
		}

		// brightness
		if query.Has("brightness") {
			brightness := query.GetInt("brightness")
			processor.gift.Add(gift.Brightness(float32(brightness)))
		}

		// saturation
		if query.Has("saturation") {
			saturation := query.GetInt("saturation")
			processor.gift.Add(gift.Saturation(float32(saturation)))
		}

		// colorize
		if query.Has("colorize") {
			colorize := query.GetIntArray("colorize")
			if len(colorize) == 3 {
				processor.gift.Add(gift.Colorize(float32(colorize[0]), float32(colorize[1]), float32(colorize[2])))
			}
		}

		// colorbalance
		if query.Has("colorbalance") {
			colorbalance := query.GetIntArray("colorbalance")
			if len(colorbalance) == 3 {
				processor.gift.Add(gift.ColorBalance(float32(colorbalance[0]), float32(colorbalance[1]), float32(colorbalance[2])))
			}
		}

		// quality
		if query.Has("q") {
			q := query.GetInt("q")
			if q > 0 && q < 100 {
				quality = q
			}
		}

		// Draw
		if len(processor.gift.Filters) > 0 {
			rgba := image.NewRGBA(processor.gift.Bounds(src.Object.Bounds()))
			processor.gift.Draw(rgba, src.Object)

			jpeg.Encode(dst, rgba, &jpeg.Options{Quality: quality})

			return
		}
	}

	// default
	jpeg.Encode(dst, src.Object, &jpeg.Options{Quality: quality})
}
func crop(img_name string, crop_map map[string][]string, current_dir string) {
	idx := strings.Index(img_name, ".jpg")
	if idx == -1 {
		panic("no *.jpg in " + img_name)
	}

	c := crop_map[img_name[:idx]]
	fmt.Println(img_name + " : " + strings.Join(c, ", "))

	f_path := current_dir + "/" + img_name

	f, _ := os.Open(f_path)
	defer f.Close()

	img, err := jpeg.Decode(f)
	if err != nil {
		return
	}

	/**********
	 *  Crop
	 **********/

	x0, _ := strconv.Atoi(c[0])
	y0, _ := strconv.Atoi(c[1])
	x1, _ := strconv.Atoi(c[2])
	y1, _ := strconv.Atoi(c[3])

	g := gift.New(
		gift.Crop(image.Rect(x0, y0, x1, y1)),
	)

	bounds := g.Bounds(img.Bounds())
	crop := image.NewRGBA(bounds)
	g.Draw(crop, img)

	crop_f_path := current_dir + "/crop_" + img_name
	out, _ := os.Create(crop_f_path)
	defer out.Close()

	jpeg.Encode(out, crop, nil)

	/**********
	 *  Flip
	 **********/

	g = gift.New(
		gift.FlipHorizontal(),
	)

	dst := image.NewRGBA(g.Bounds(crop.Bounds()))
	g.Draw(dst, crop)

	flip_f_path := current_dir + "/flip_" + img_name
	out, _ = os.Create(flip_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)

	/***********
	 *  Rotate
	 ***********/

	g = gift.New(
		gift.Rotate90(),
	)

	dst = image.NewRGBA(g.Bounds(crop.Bounds()))
	g.Draw(dst, crop)

	rotate_f_path := current_dir + "/rotate90_" + img_name
	out, _ = os.Create(rotate_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)

	g = gift.New(
		gift.Rotate180(),
	)

	dst = image.NewRGBA(g.Bounds(crop.Bounds()))
	g.Draw(dst, crop)

	rotate_f_path = current_dir + "/rotate180_" + img_name
	out, _ = os.Create(rotate_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)

	g = gift.New(
		gift.Rotate270(),
	)

	dst = image.NewRGBA(g.Bounds(crop.Bounds()))
	g.Draw(dst, crop)

	rotate_f_path = current_dir + "/rotate270_" + img_name
	out, _ = os.Create(rotate_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)
}