Class: Pdfcrowd::ImageToImageClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfcrowd.rb

Overview

Conversion from one image format to another image format.

Instance Method Summary collapse

Constructor Details

#initialize(user_name, api_key) ⇒ ImageToImageClient

Constructor for the Pdfcrowd API client.

  • user_name - Your username at Pdfcrowd.

  • api_key - Your API key.



2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
# File 'lib/pdfcrowd.rb', line 2324

def initialize(user_name, api_key)
    @helper = ConnectionHelper.new(user_name, api_key)
    @fields = {
        'input_format'=>'image',
        'output_format'=>'png'
    }
    @file_id = 1
    @files = {}
    @raw_data = {}
end

Instance Method Details

#convertFile(file) ⇒ Object

Convert a local file.

  • file - The path to a local file to convert. The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.

  • Returns - Byte array containing the conversion output.



2385
2386
2387
2388
2389
2390
2391
2392
# File 'lib/pdfcrowd.rb', line 2385

def convertFile(file)
    if (!(File.file?(file) && !File.zero?(file)))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file, "file", "image-to-image", "The file must exist and not be empty.", "convert_file"), 470);
    end
    
    @files['file'] = file
    @helper.post(@fields, @files, @raw_data)
end

#convertFileToFile(file, file_path) ⇒ Object

Convert a local file and write the result to a local file.

  • file - The path to a local file to convert. The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.

  • file_path - The output file path. The string must not be empty.



2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
# File 'lib/pdfcrowd.rb', line 2411

def convertFileToFile(file, file_path)
    if (!(!file_path.nil? && !file_path.empty?))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "image-to-image", "The string must not be empty.", "convert_file_to_file"), 470);
    end
    
    output_file = open(file_path, "wb")
    begin
        convertFileToStream(file, output_file)
        output_file.close()
    rescue Error => why
        output_file.close()
        FileUtils.rm(file_path)
        raise
    end
end

#convertFileToStream(file, out_stream) ⇒ Object

Convert a local file and write the result to an output stream.

  • file - The path to a local file to convert. The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip). The file must exist and not be empty.

  • out_stream - The output stream that will contain the conversion output.



2398
2399
2400
2401
2402
2403
2404
2405
# File 'lib/pdfcrowd.rb', line 2398

def convertFileToStream(file, out_stream)
    if (!(File.file?(file) && !File.zero?(file)))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file, "file", "image-to-image", "The file must exist and not be empty.", "convert_file_to_stream"), 470);
    end
    
    @files['file'] = file
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#convertRawData(data) ⇒ Object

Convert raw data.

  • data - The raw content to be converted.

  • Returns - Byte array with the output.



2431
2432
2433
2434
# File 'lib/pdfcrowd.rb', line 2431

def convertRawData(data)
    @raw_data['file'] = data
    @helper.post(@fields, @files, @raw_data)
end

#convertRawDataToFile(data, file_path) ⇒ Object

Convert raw data to a file.

  • data - The raw content to be converted.

  • file_path - The output file path. The string must not be empty.



2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
# File 'lib/pdfcrowd.rb', line 2449

def convertRawDataToFile(data, file_path)
    if (!(!file_path.nil? && !file_path.empty?))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "image-to-image", "The string must not be empty.", "convert_raw_data_to_file"), 470);
    end
    
    output_file = open(file_path, "wb")
    begin
        convertRawDataToStream(data, output_file)
        output_file.close()
    rescue Error => why
        output_file.close()
        FileUtils.rm(file_path)
        raise
    end
end

#convertRawDataToStream(data, out_stream) ⇒ Object

Convert raw data and write the result to an output stream.

  • data - The raw content to be converted.

  • out_stream - The output stream that will contain the conversion output.



2440
2441
2442
2443
# File 'lib/pdfcrowd.rb', line 2440

def convertRawDataToStream(data, out_stream)
    @raw_data['file'] = data
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#convertUrl(url) ⇒ Object

Convert an image.

  • url - The address of the image to convert. The supported protocols are http:// and https://.

  • Returns - Byte array containing the conversion output.



2339
2340
2341
2342
2343
2344
2345
2346
# File 'lib/pdfcrowd.rb', line 2339

def convertUrl(url)
    unless /(?i)^https?:\/\/.*$/.match(url)
        raise Error.new(Pdfcrowd.create_invalid_value_message(url, "url", "image-to-image", "The supported protocols are http:// and https://.", "convert_url"), 470);
    end
    
    @fields['url'] = url
    @helper.post(@fields, @files, @raw_data)
end

#convertUrlToFile(url, file_path) ⇒ Object

Convert an image and write the result to a local file.

  • url - The address of the image to convert. The supported protocols are http:// and https://.

  • file_path - The output file path. The string must not be empty.



2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
# File 'lib/pdfcrowd.rb', line 2365

def convertUrlToFile(url, file_path)
    if (!(!file_path.nil? && !file_path.empty?))
        raise Error.new(Pdfcrowd.create_invalid_value_message(file_path, "file_path", "image-to-image", "The string must not be empty.", "convert_url_to_file"), 470);
    end
    
    output_file = open(file_path, "wb")
    begin
        convertUrlToStream(url, output_file)
        output_file.close()
    rescue Error => why
        output_file.close()
        FileUtils.rm(file_path)
        raise
    end
end

#convertUrlToStream(url, out_stream) ⇒ Object

Convert an image and write the result to an output stream.

  • url - The address of the image to convert. The supported protocols are http:// and https://.

  • out_stream - The output stream that will contain the conversion output.



2352
2353
2354
2355
2356
2357
2358
2359
# File 'lib/pdfcrowd.rb', line 2352

def convertUrlToStream(url, out_stream)
    unless /(?i)^https?:\/\/.*$/.match(url)
        raise Error.new(Pdfcrowd.create_invalid_value_message(url, "url", "image-to-image", "The supported protocols are http:// and https://.", "convert_url_to_stream"), 470);
    end
    
    @fields['url'] = url
    @helper.post(@fields, @files, @raw_data, out_stream)
end

#getConsumedCreditCountObject

Get the number of credits consumed by the last conversion.

  • Returns - The number of credits.



2521
2522
2523
# File 'lib/pdfcrowd.rb', line 2521

def getConsumedCreditCount()
    return @helper.getConsumedCreditCount()
end

#getDebugLogUrlObject

Get the URL of the debug log for the last conversion.

  • Returns - The link to the debug log.



2507
2508
2509
# File 'lib/pdfcrowd.rb', line 2507

def getDebugLogUrl()
    return @helper.getDebugLogUrl()
end

#getJobIdObject

Get the job id.

  • Returns - The unique job identifier.



2527
2528
2529
# File 'lib/pdfcrowd.rb', line 2527

def getJobId()
    return @helper.getJobId()
end

#getOutputSizeObject

Get the size of the output in bytes.

  • Returns - The count of bytes.



2533
2534
2535
# File 'lib/pdfcrowd.rb', line 2533

def getOutputSize()
    return @helper.getOutputSize()
end

#getRemainingCreditCountObject

Get the number of conversion credits available in your account. The returned value can differ from the actual count if you run parallel conversions. The special value 999999 is returned if the information is not available.

  • Returns - The number of credits.



2515
2516
2517
# File 'lib/pdfcrowd.rb', line 2515

def getRemainingCreditCount()
    return @helper.getRemainingCreditCount()
end

#setDebugLog(debug_log) ⇒ Object

Turn on the debug logging. Details about the conversion are stored in the debug log. The URL of the log can be obtained from the getDebugLogUrl method.

  • debug_log - Set to true to enable the debug logging.

  • Returns - The converter object.



2500
2501
2502
2503
# File 'lib/pdfcrowd.rb', line 2500

def setDebugLog(debug_log)
    @fields['debug_log'] = debug_log
    self
end

#setOutputFormat(output_format) ⇒ Object

The format of the output file.

  • output_format - Allowed values are png, jpg, gif, tiff, bmp, ico, ppm, pgm, pbm, pnm, psb, pct, ras, tga, sgi, sun, webp.

  • Returns - The converter object.



2469
2470
2471
2472
2473
2474
2475
2476
# File 'lib/pdfcrowd.rb', line 2469

def setOutputFormat(output_format)
    unless /(?i)^(png|jpg|gif|tiff|bmp|ico|ppm|pgm|pbm|pnm|psb|pct|ras|tga|sgi|sun|webp)$/.match(output_format)
        raise Error.new(Pdfcrowd.create_invalid_value_message(output_format, "output_format", "image-to-image", "Allowed values are png, jpg, gif, tiff, bmp, ico, ppm, pgm, pbm, pnm, psb, pct, ras, tga, sgi, sun, webp.", "set_output_format"), 470);
    end
    
    @fields['output_format'] = output_format
    self
end

#setProxy(host, port, user_name, password) ⇒ Object

Specifies an HTTP proxy that the API client library will use to connect to the internet.

  • host - The proxy hostname.

  • port - The proxy port.

  • user_name - The username.

  • password - The password.

  • Returns - The converter object.



2562
2563
2564
2565
# File 'lib/pdfcrowd.rb', line 2562

def setProxy(host, port, user_name, password)
    @helper.setProxy(host, port, user_name, password)
    self
end

#setResize(resize) ⇒ Object

Resize the image.

  • resize - The resize percentage or new image dimensions.

  • Returns - The converter object.



2482
2483
2484
2485
# File 'lib/pdfcrowd.rb', line 2482

def setResize(resize)
    @fields['resize'] = resize
    self
end

#setRetryCount(retry_count) ⇒ Object

Specifies the number of retries when the 502 HTTP status code is received. The 502 status code indicates a temporary network issue. This feature can be disabled by setting to 0.

  • retry_count - Number of retries wanted.

  • Returns - The converter object.



2571
2572
2573
2574
# File 'lib/pdfcrowd.rb', line 2571

def setRetryCount(retry_count)
    @helper.setRetryCount(retry_count)
    self
end

#setRotate(rotate) ⇒ Object

Rotate the image.

  • rotate - The rotation specified in degrees.

  • Returns - The converter object.



2491
2492
2493
2494
# File 'lib/pdfcrowd.rb', line 2491

def setRotate(rotate)
    @fields['rotate'] = rotate
    self
end

#setUseHttp(use_http) ⇒ Object

Specifies if the client communicates over HTTP or HTTPS with Pdfcrowd API.

  • use_http - Set to true to use HTTP.

  • Returns - The converter object.



2541
2542
2543
2544
# File 'lib/pdfcrowd.rb', line 2541

def setUseHttp(use_http)
    @helper.setUseHttp(use_http)
    self
end

#setUserAgent(user_agent) ⇒ Object

Set a custom user agent HTTP header. It can be usefull if you are behind some proxy or firewall.

  • user_agent - The user agent string.

  • Returns - The converter object.



2550
2551
2552
2553
# File 'lib/pdfcrowd.rb', line 2550

def setUserAgent(user_agent)
    @helper.setUserAgent(user_agent)
    self
end