Пример #1
0
func (p *Photo) resize(img image.Image, fn string, w, h int) (image.Image, error) {
	bounds := img.Bounds()
	if bounds.Dx() >= bounds.Dy() {
		h = bounds.Dy() * h / bounds.Dx()
	} else {
		w = bounds.Dx() * w / bounds.Dy()
	}
	img = resize.Resize(img, img.Bounds(), w, h)

	m, err := os.Create(path.Join(DATA_DIR, UPLOADS, fn))
	if err != nil {
		Log("Could not create image file")
		return nil, err
	}
	defer m.Close()

	// write new image to file
	err = jpeg.Encode(m, img, nil)
	if err != nil {
		Log("Could not encode image file")
		return nil, err
	}

	return img, err
}
Пример #2
0
func main() {
	// open "test.jpg"
	flag.Parse()
	file, err := os.Open(flag.Arg(0))
	if err != nil {
		log.Fatal(err)
	}

	// decode jpeg into image.Image
	img, err := jpeg.Decode(file)
	if err != nil {
		log.Fatal(err)
	}
	file.Close()
	bounds := img.Bounds()
	w, h := THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT
	if bounds.Dx() > bounds.Dy() {
		h = bounds.Dy() * h / bounds.Dx()
	} else {
		w = bounds.Dx() * w / bounds.Dy()
	}
	img = resize.Resize(img, img.Bounds(), w, h)

	out, err := os.Create("test_resized.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	// write new image to file
	jpeg.Encode(out, img, nil)
}