Free Trial
10 days, no credit_card

<< More Tutorials

How to resize an image by width in Go using rethumb?

To resize an image or photo in Go use the following code:

package main

import (
	"fmt";
	"net/http";
	"io/ioutil"
)

func main() {
	paramOperation := "width";
	paramValue := "100"; // New width in pixels.

	imageURL := "http://images.rethumb.com/image_coimbra_600x300.jpg";
	imageFilename := "resized-image.jpg";

	resp, err := http.Get(fmt.Sprintf("http://api.rethumb.com/v1/%s/%s/%s", paramOperation, paramValue, imageURL))
	check(err)
	defer resp.Body.Close()
	
	body, err := ioutil.ReadAll(resp.Body)
	check(err)

	err = ioutil.WriteFile(imageFilename, body, 0644)
	check(err)
}

func check(e error) {
	if e != nil {
		panic(e)
	}
}

Start using this example now

Use the following commands to get started:

$ git clone https://github.com/rethumb/rethumb-go-examples.git
$ cd rethumb-go-examples
$ go build resize-by-width.go
$ ./resize-by-width

More examples using Go