Exemple #1
0
func (fi *FileInfo) createThumb(buffer *bytes.Buffer, c context.Context) {
	if imageTypes.MatchString(fi.Type) {
		src, _, err := image.Decode(bytes.NewReader(buffer.Bytes()))
		check(err)
		filter := gift.New(gift.ResizeToFit(
			THUMB_MAX_WIDTH,
			THUMB_MAX_HEIGHT,
			gift.LanczosResampling,
		))
		dst := image.NewNRGBA(filter.Bounds(src.Bounds()))
		filter.Draw(dst, src)
		buffer.Reset()
		bWriter := bufio.NewWriter(buffer)
		switch fi.Type {
		case "image/jpeg", "image/pjpeg":
			err = jpeg.Encode(bWriter, dst, nil)
		case "image/gif":
			err = gif.Encode(bWriter, dst, nil)
		default:
			err = png.Encode(bWriter, dst)
		}
		check(err)
		bWriter.Flush()
		thumbnailKey := fi.Key + thumbSuffix + filepath.Ext(fi.Name)
		item := &memcache.Item{
			Key:   thumbnailKey,
			Value: buffer.Bytes(),
		}
		err = memcache.Set(c, item)
		check(err)
		fi.ThumbnailKey = thumbnailKey
	}
}
Exemple #2
0
func getimage(image_path string, w, h int, resized bool) (image.Image, error) {
	f, err := os.Open(image_path)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		return nil, err
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		return nil, err
	}
	if resized {
		g := gift.New(gift.ResizeToFit(w, h, gift.BoxResampling))
		res := image.NewRGBA(g.Bounds(img.Bounds()))
		g.Draw(res, img)
		return res, nil
	}
	return img, nil
}
Exemple #3
0
func (prc *ResizeProcessor) getFilter(width, height int, params imageserver.Params) (gift.Filter, error) {
	rsp, err := prc.getResampling(params)
	if err != nil {
		return nil, err
	}
	if !params.Has("mode") || width == 0 || height == 0 {
		return gift.Resize(width, height, rsp), nil
	}
	mode, err := params.GetString("mode")
	if err != nil {
		return nil, err
	}
	switch mode {
	case "fit":
		return gift.ResizeToFit(width, height, rsp), nil
	case "fill":
		return gift.ResizeToFill(width, height, rsp, gift.CenterAnchor), nil
	}
	return nil, &imageserver.ParamError{Param: "mode", Message: "invalid value"}
}