Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Text to Image File JPEG, PNG or GIF in Ruby
# How to convert Text to GIF image format
# Getting instance of the Convert API
@apiInstance = GroupDocsConversionCloud::ConvertApi.from_keys(@app_sid, @app_key)
# Initialize convert settings
@settings = GroupDocsConversionCloud::ConvertSettings.new()
@settings.storage_name = @storage_name
@settings.file_path = "text-to-image/sample-file.txt"
@settings.format = "gif"
# Text load options
@loadOptions = GroupDocsConversionCloud::TxtLoadOptions()
@loadOptions.encoding = "shift_jis"
@settings.load_options = @loadOptions
@convertOptions = GroupDocsConversionCloud::GifConvertOptions.new()
@convertOptions.grayscale = true
@convertOptions.from_page = 1
@convertOptions.pages_count = 1
@convertOptions.quality = 100
@convertOptions.rotate_angle = 90
@convertOptions.use_pdf = false
@settings.convert_options = @convertOptions
@settings.output_path = "text-to-image"
@request = GroupDocsConversionCloud::ConvertDocumentRequest.new(@settings)
@result = @apiInstance.convert_document(@request)
puts("Successfully converted Text to gif image format.")

You can convert Text to image file formats programmatically on the cloud. In this article, you will learn how to convert text file to image format using REST API in Ruby.

The following topics are covered in this article:

  1. Text to Images Conversion REST API – Installation
  2. Convert Text to JPG/JPEG file format using REST API
  3. Convert Text to PNG format using REST API in Ruby
  4. How to Convert Text to GIF file format in Ruby API
# How to convert Text to JPG/JPEG file format
# Getting instance of the ConvertAPI
@apiInstance = GroupDocsConversionCloud::ConvertApi.from_keys(@app_sid, @app_key)
# Initialize and set convert settings
@settings = GroupDocsConversionCloud::ConvertSettings.new()
@settings.storage_name = @storage_name
@settings.file_path = "text-to-image/sample-file.txt"
@settings.format = "jpeg"
# Text load options
@loadOptions = GroupDocsConversionCloud::TxtLoadOptions()
@loadOptions.encoding = "shift_jis"
@settings.load_options = @loadOptions
@convertOptions = GroupDocsConversionCloud::JpegConvertOptions.new()
@convertOptions.grayscale = true
@convertOptions.from_page = 1
@convertOptions.pages_count = 1
@convertOptions.quality = 100
@convertOptions.rotate_angle = 90
@convertOptions.use_pdf = false
@settings.convert_options = @convertOptions
@settings.output_path = "text-to-image"
@request = GroupDocsConversionCloud::ConvertDocumentRequest.new(@settings)
@result = @apiInstance.convert_document(@request)
puts("Text successfully converted to jpeg format. " + (@result).to_s)
# How to convert Text to PNG format
# Getting instance of the Convert API
@apiInstance = GroupDocsConversionCloud::ConvertApi.from_keys(@app_sid, @app_key)
# Initialize convert settings
@settings = GroupDocsConversionCloud::ConvertSettings.new()
@settings.storage_name = @storage_name
@settings.file_path = "text-to-image/sample-file.txt"
@settings.format = "png"
# Text load options
@loadOptions = GroupDocsConversionCloud::TxtLoadOptions()
@loadOptions.encoding = "shift_jis"
@settings.load_options = @loadOptions
@convertOptions = GroupDocsConversionCloud::PngConvertOptions.new()
@convertOptions.grayscale = true
@convertOptions.from_page = 1
@convertOptions.pages_count = 1
@convertOptions.quality = 100
@convertOptions.rotate_angle = 90
@convertOptions.use_pdf = false
@settings.convert_options = @convertOptions
@settings.output_path = "text-to-image"
@request = GroupDocsConversionCloud::ConvertDocumentRequest.new(@settings)
@result = @apiInstance.convert_document(@request)
puts("Text .txt file converted to png file image. ")
# Load Text file to Image Conversion REST API in your rails application:
require 'groupdocs_conversion_cloud'
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
@app_sid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
@storage_name = "DefaultStorage"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment