Method: App42::ImageProcessor::ImageProcessorService#convert_format

Defined in:
lib/imageProcessor/ImageProcessorService.rb

#convert_format(name, imagePath, formatToConvert) ⇒ Object

Converts the format of the image. Returns the original image url and converted image url. Images are stored on the cloud and can be accessed through the urls Conversion is done based on the formatToConvert provided

Parameters:

  • name
    • Name of the image to resize

  • imagePath
    • Path of the local file to resize

  • formatToConvert
    • to which file needs to be converted

Returns:

  • Image object containing urls for the original and converted images



947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
# File 'lib/imageProcessor/ImageProcessorService.rb', line 947

def convert_format(name, imagePath, formatToConvert)
  puts "convert_format Called "
  puts "Base url #{@base_url}"
  response = nil
  imageObj = nil
  imageObj = Image.new()
  util = Util.new
  util.throwExceptionIfNullOrBlank(name, "Name");
  util.throwExceptionIfNullOrBlank(imagePath, "Image Path");
  util.throwExceptionIfNullOrBlank(formatToConvert, "FormatToConvert");
  begin
    file = File.new(imagePath,"rb")
    ext = File.extname(file)
    if((ext.eql?(".jpg") == false) && (ext.eql?(".JPG") == false) && (ext.eql?(".jpeg") == false) && (ext.eql?(".JPEG") == false) && (ext.eql?(".gif") == false) && (ext.eql?(".GIF") == false) && (ext.eql?(".png") == false) && (ext.eql?(".PNG") == false))
      raise TypeError,"The Request parameters are invalid. Only file with extensions jpg, jpeg, gif and png are supported"
    end
    if((formatToConvert.eql?("jpg") == false) && (formatToConvert.eql?("JPG") == false) && (formatToConvert.eql?("jpeg") == false) && (formatToConvert.eql?("JPEG") == false) && (formatToConvert.eql?("gif") == false) && (formatToConvert.eql?("GIF") == false) && (formatToConvert.eql?("png") == false) && (formatToConvert.eql?("PNG") == false))
      raise TypeError, "The Request parameters are invalid. Supported conversion extensions are jpg, jpeg, gif and png only"
    end
    if File.file?(imagePath) == false
      raise App42Exception.new("File " + imagePath.to_s + " does not exist");
    end
    connection = App42::Connection::RESTConnection.new(@base_url)
    params = Hash.new
    query_params = Hash.new
    query_params = {
      'apiKey'=> @api_key,
      'version' => @version,
      'timeStamp' => util.get_timestamp_utc,
    }
    params = query_params.clone
    post_params = Hash.new
    post_params.store("name", name)
    post_params.store("formatToConvert", formatToConvert)
    params = params.merge(post_params)
    signature = util.sign(@secret_key, params)
    resource_url = "#{@version}/#{@resource}/convertformat"
    response = connection.imageMultipart(signature, resource_url, query_params, params, imagePath)
    image = ImageProcessorResponseBuilder.new
    imageObj = image.buildResponse(response)
  rescue  App42Exception =>e
    raise e
  rescue  Exception => e
    raise App42Exception.new(e)
  end
  return imageObj
end