コード例 #1
0
ファイル: handlers.go プロジェクト: jmptrader/tenpu
func resizeThumbnail(from *bytes.Buffer, spec *ThumbnailSpec) (to io.Reader, w int, h int, err error) {

	src, name, err := image.Decode(from)
	if err != nil {
		return
	}
	srcB := src.Bounds()

	var dst image.Image
	if spec.CropToSquare && srcB.Dx() != srcB.Dy() {
		var rect image.Rectangle
		if srcB.Dx() > srcB.Dy() {
			x1 := (srcB.Dx() - srcB.Dy()) / 2
			x2 := srcB.Dx() - x1
			rect = image.Rect(x1, 0, x2, srcB.Dy())

		} else {
			rect = image.Rect(0, 0, srcB.Dx(), srcB.Dx())
		}
		w = spec.Height
		if (spec.Height > spec.Width && spec.Width != 0) || spec.Height == 0 {
			w = spec.Width
		}
		h = w

		cropedImg := imaging.Crop(src, rect)
		rect = cropedImg.Bounds()
		dst = resize.Resize(cropedImg, rect, w, h)

	} else {
		w, h = spec.CalculateRect(srcB, spec.CropToSquare)

		if w >= srcB.Dx() || h >= srcB.Dy() {
			w, h = srcB.Dx(), srcB.Dy()
		}

		rect := image.Rect(0, 0, srcB.Dx(), srcB.Dy())
		dst = resize.Resize(src, rect, w, h)
	}

	var buf bytes.Buffer
	switch name {
	case "jpeg":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	case "png":
		png.Encode(&buf, dst)
	case "gif":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	case "bmp":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	}

	to = &buf
	return
}
コード例 #2
0
ファイル: handlers.go プロジェクト: athom/tenpu
func resizeThumbnail(from *bytes.Buffer, spec *ThumbnailSpec) (to io.Reader, w int, h int, err error) {

	src, name, err := image.Decode(from)
	if err != nil {
		return
	}
	srcB := src.Bounds()

	w, h = spec.CalculateRect(srcB)

	rect := image.Rect(0, 0, srcB.Dx(), srcB.Dy())

	dst := resize.Resize(src, rect, w, h)

	var buf bytes.Buffer
	switch name {
	case "jpeg":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	case "png":
		png.Encode(&buf, dst)
	case "gif":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	}

	to = &buf
	return
}