Image to Image / Golang Examples
This page contains various examples of using the Image to Image API in Golang. The examples are complete and fully functional. Read more about how to convert Image to Image in Golang.
Basic examples
- PNG file to JPEG file
- PNG file to in-memory JPEG
- PNG file to JPEG stream
- PNG url to JPEG file
- PNG url to in-memory JPEG
- PNG url to JPEG stream
- In-memory PNG to JPEG file
- In-memory PNG to in-memory JPEG
- In-memory PNG to JPEG stream
- Get info about the current conversion
Basic examples
PNG file to JPEG file
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Run the conversion and save the result to a file. err := client.ConvertFileToFile("/path/to/logo.png", "logo.jpg") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
PNG file to in-memory JPEG
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Run the conversion and store the result in the `image` variable. image, err := client.ConvertFile("/path/to/logo.png") // Check for the conversion error. handleError(err) // at this point the "image" variable contains JPG raw data and // can be sent in an HTTP response, saved to a file, etc. } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
PNG file to JPEG stream
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Create an output stream for the conversion result outputStream, err := os.Create("logo.jpg") // Check for a file creation error. handleError(err) // Close the output stream. defer outputStream.Close() // run the conversion and write the result to the output stream. err = client.ConvertFileToStream("/path/to/logo.png", outputStream) // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
PNG url to JPEG file
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Run the conversion and save the result to a file. err := client.ConvertUrlToFile("https://pdfcrowd.com/static/images/logo.png", "logo.jpg") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
PNG url to in-memory JPEG
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Run the conversion and store the result in the `image` variable. image, err := client.ConvertUrl("https://pdfcrowd.com/static/images/logo.png") // Check for the conversion error. handleError(err) // at this point the "image" variable contains JPG raw data and // can be sent in an HTTP response, saved to a file, etc. } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
PNG url to JPEG stream
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Create an output stream for the conversion result outputStream, err := os.Create("logo.jpg") // Check for a file creation error. handleError(err) // Close the output stream. defer outputStream.Close() // run the conversion and write the result to the output stream. err = client.ConvertUrlToStream("https://pdfcrowd.com/static/images/logo.png", outputStream) // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
In-memory PNG to JPEG file
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" "io/ioutil" ) func readFile(fileName string) []byte { content, err := ioutil.ReadFile(fileName) handleError(err) return content } func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Run the conversion and save the result to a file. err := client.ConvertRawDataToFile(readFile("/path/to/logo.png"), "logo.jpg") // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
In-memory PNG to in-memory JPEG
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" "io/ioutil" ) func readFile(fileName string) []byte { content, err := ioutil.ReadFile(fileName) handleError(err) return content } func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Run the conversion and store the result in the `image` variable. image, err := client.ConvertRawData(readFile("/path/to/logo.png")) // Check for the conversion error. handleError(err) // at this point the "image" variable contains JPG raw data and // can be sent in an HTTP response, saved to a file, etc. } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
In-memory PNG to JPEG stream
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" "io/ioutil" ) func readFile(fileName string) []byte { content, err := ioutil.ReadFile(fileName) handleError(err) return content } func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") // Create an output stream for the conversion result outputStream, err := os.Create("logo.jpg") // Check for a file creation error. handleError(err) // Close the output stream. defer outputStream.Close() // run the conversion and write the result to the output stream. err = client.ConvertRawDataToStream(readFile("/path/to/logo.png"), outputStream) // Check for the conversion error. handleError(err) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }
Get info about the current conversion
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // Create an API client instance. client := pdfcrowd.NewImageToImageClient("demo", "demo") // Configure the conversion. client.SetOutputFormat("jpg") client.SetDebugLog(true) // Run the conversion and save the result to a file. err := client.ConvertFileToFile("/path/to/logo.png", "logo.jpg") // Check for the conversion error. handleError(err) // print URL pointing to the debug log for this request. fmt.Println("Debug log url:", client.GetDebugLogUrl()) // print number of conversion credits remaining in your account. fmt.Println("Remaining credit count:", client.GetRemainingCreditCount()) // print number of credits consumed for this conversion. fmt.Println("Consumed credit count:", client.GetConsumedCreditCount()) // print unique identifier assigned to this conversion job. fmt.Println("Job id:", client.GetJobId()) // print size of the output data in bytes. fmt.Println("Output size:", client.GetOutputSize()) } func handleError(err error) { if err != nil { why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("PDFCrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } panic(err.Error()) } }