Пример #1
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})
}
Пример #2
0
func (f *defaultFileStorage) store(src readable, filename, contentType string) error {
	if err := os.MkdirAll(f.uploadsDir, 0777); err != nil && !os.IsExist(err) {
		return errgo.Mask(err)
	}

	if err := os.MkdirAll(f.thumbnailsDir, 0777); err != nil && !os.IsExist(err) {
		return errgo.Mask(err)
	}

	// make thumbnail
	var (
		img image.Image
		err error
	)

	switch contentType {
	case "image/png":
		img, err = png.Decode(src)
		break
	case "image/jpeg":
		img, err = jpeg.Decode(src)
		break
	case "image/jpg":
		img, err = jpeg.Decode(src)
		break
	case "image/gif":
		img, err = gif.Decode(src)
		break
	default:
		return errors.New("invalid content type:" + contentType)
	}

	if err != nil {
		return errgo.Mask(err)
	}

	thumb := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
	graphics.Thumbnail(thumb, img)

	dst, err := os.Create(path.Join(f.thumbnailsDir, filename))
	if err != nil {
		return errgo.Mask(err)
	}

	g := gift.New(gift.Contrast(-30))
	g.Draw(thumb, thumb)

	if err != nil {
		return errgo.Mask(err)
	}

	defer dst.Close()

	switch contentType {
	case "image/png":
		err = png.Encode(dst, thumb)
		break
	case "image/jpeg":
		err = jpeg.Encode(dst, thumb, nil)
		break
	case "image/jpg":
		err = jpeg.Encode(dst, thumb, nil)
		break
	case "image/gif":
		err = gif.Encode(dst, thumb, nil)
	}

	if err != nil {
		return errgo.Mask(err)
	}

	src.Seek(0, 0)

	dst, err = os.Create(path.Join(f.uploadsDir, filename))

	if err != nil {
		return errgo.Mask(err)
	}

	defer dst.Close()

	_, err = io.Copy(dst, src)
	if err != nil {
		return errgo.Mask(err)
	}

	return nil

}