コード例 #1
0
ファイル: main.go プロジェクト: nalin-humin/gifresize
func main() {
	flag.Usage = usage
	flag.Parse()

	r, err := read(flag.Arg(0))
	if err != nil {
		log.Fatalf("error reading input: %v", err)
	}

	tx := func(m image.Image) image.Image {
		return resize.Resize(width, height, m, resize.NearestNeighbor)
	}

	err = gifresize.Process(os.Stdout, r, tx)
	if err != nil {
		log.Fatalf("error processing image: %v", err)
	}
}
コード例 #2
0
ファイル: transform.go プロジェクト: LimiQS/imageproxy
// Transform the provided image.  img should contain the raw bytes of an
// encoded image in one of the supported formats (gif, jpeg, or png).  The
// bytes of a similarly encoded image is returned.
func Transform(img []byte, opt Options) ([]byte, error) {
	if opt == emptyOptions {
		// bail if no transformation was requested
		return img, nil
	}

	// decode image
	m, format, err := image.Decode(bytes.NewReader(img))
	if err != nil {
		return nil, err
	}

	// transform and encode image
	buf := new(bytes.Buffer)
	switch format {
	case "gif":
		fn := func(img image.Image) image.Image {
			return transformImage(img, opt)
		}
		err = gifresize.Process(buf, bytes.NewReader(img), fn)
		if err != nil {
			return nil, err
		}
	case "jpeg":
		quality := opt.Quality
		if quality == 0 {
			quality = defaultQuality
		}

		m = transformImage(m, opt)
		err = jpeg.Encode(buf, m, &jpeg.Options{Quality: quality})
		if err != nil {
			return nil, err
		}
	case "png":
		m = transformImage(m, opt)
		err = png.Encode(buf, m)
		if err != nil {
			return nil, err
		}
	}

	return buf.Bytes(), nil
}