Пример #1
0
func cutImage(img *image.NRGBA) []*image.NRGBA {
	r := image.Rect(0, 0, 6, 11)
	cuts := make([]*image.NRGBA, 10)
	for i := 0; i < 10; i++ {
		cuts[i] = img.SubImage(r).(*image.NRGBA)
		r = r.Add(image.Pt(7, 0))
	}

	return cuts
}
Пример #2
0
func Resize(src io.Reader, c *CacheContext) (io.Reader, error) {
	raw, err := ioutil.ReadAll(src)
	if err != nil {
		return nil, err
	}

	width := c.Width
	data := bytes.NewReader(raw)
	img, format, err := image.Decode(data)
	if err != nil {
		return nil, err
	}
	var resizedImage image.NRGBA
	if c.Crop {

		minDimension := int(math.Min(float64(img.Bounds().Size().X), float64(img.Bounds().Size().Y)))

		if minDimension < c.Width || c.Width == 0 {
			width = minDimension
		}

		resizedImage = *imaging.Fill(img, width, width, imaging.Center, imaging.Lanczos)
	} else {
		resizedImage = *imaging.Resize(img, width, 0, imaging.Lanczos)
	}

	buf := new(bytes.Buffer)
	var imgFormat imaging.Format
	switch format {
	case "png":
		imgFormat = imaging.PNG
	case "jpeg":
		imgFormat = imaging.JPEG
	case "tiff":
		imgFormat = imaging.TIFF
	case "bmp":
		imgFormat = imaging.BMP
	default:
		return nil, errors.New("unsupported image format")
	}

	err = imaging.Encode(buf, resizedImage.SubImage(resizedImage.Rect), imgFormat)
	if err != nil {
		return nil, err
	}

	return buf, err

}