Пример #1
0
func ProcessPhoto(photo *Photo) error {
	origImg, err := imaging.Open(GetPhotoPath(photo, "o"))
	if err != nil {
		return err
	}

	src := imaging.Clone(origImg)

	for _, sz := range PhotoSizes {
		var dst image.Image

		switch sz.Type {
		case "thumbnail":
			dst = imaging.Thumbnail(src, sz.Width, sz.Height, imaging.Lanczos)
		case "fit":
			dst = imaging.Fit(src, sz.Width, sz.Height, imaging.Lanczos)
		}

		err := imaging.Save(dst, GetPhotoPath(photo, sz.Suffix))
		if err != nil {
			return err
		}
	}

	return nil
}
Пример #2
0
func ProcessAvatar(user *User) error {
	origImg, err := imaging.Open(GetAvatarPath(user, "o"))
	if err != nil {
		return err
	}

	src := imaging.Clone(origImg)

	for _, sz := range AvatarSizes {
		var dst image.Image

		switch sz.Type {
		case "thumbnail":
			dst = imaging.Thumbnail(src, sz.Width, sz.Height, imaging.Lanczos)
		case "fit":
			dst = imaging.Fit(src, sz.Width, sz.Height, imaging.Lanczos)
		}

		err := imaging.Save(dst, GetAvatarPath(user, sz.Suffix))
		if err != nil {
			return err
		}
	}

	return nil
}
Пример #3
0
// This function used internally to convert any image type to NRGBA if needed.
func toNRGBA(img image.Image) *image.NRGBA {
	srcBounds := img.Bounds()
	if srcBounds.Min.X == 0 && srcBounds.Min.Y == 0 {
		if src0, ok := img.(*image.NRGBA); ok {
			return src0
		}
	}
	return imaging.Clone(img)
}
Пример #4
0
func MakeImages(rect *image.Rectangle, file io.Reader) ([]*bytes.Buffer, error) {
	runtime.GOMAXPROCS(runtime.NumCPU())

	ar := aspectRatio(rect)

	if ar < RATIO {
		return nil, fmt.Errorf("makeImages: aspect ratio is too small")
	} else if ar > RATIO {
		cropRatio(rect)
	}

	img, _, err := image.Decode(file)
	if err != nil {
		return nil, err
	}
	img = imaging.Clone(img)

	top := rect.Min.Y
	square := squareHeight(rect)
	space := spaceHeight(rect)

	buffers := make([]*bytes.Buffer, 5, 5)

	for i := 0; i < 5; i++ {
		new_rect := image.Rect(rect.Min.X, top, rect.Max.X, top+square)
		new_img := imaging.Crop(img, new_rect)

		buffers[i] = bytes.NewBuffer(make([]byte, 0))
		err = png.Encode(buffers[i], new_img)
		if err != nil {
			panic(err)
		}

		top = top + square + space
	}

	return buffers, nil
}