Class: DynamicPDFApi::ImageResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/ruby_client/ImageResource.rb

Overview

Represents an image resource used to create an ImageInput object to create PDF from images.

Instance Attribute Summary

Attributes inherited from Resource

#_file_path, #_mime_type, #data, #resource_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

_get_file_data

Constructor Details

#initialize(file, resource_name = nil) ⇒ ImageResource

Initializes a new instance of the ImageResource class.

Parameters:

  • file (String)

    |[Stream] The image file path or the byte of the image file or the stream of the image file.

  • resource_name (String) (defaults to: nil)

    The name of the resource.



16
17
18
19
20
# File 'lib/ruby_client/ImageResource.rb', line 16

def initialize(file, resource_name = nil)
  @_type = ResourceType::IMAGE
  @_mime_type = nil
  super(file, resource_name)
end

Class Method Details

.is_gif_image(header) ⇒ Object



66
67
68
69
# File 'lib/ruby_client/ImageResource.rb', line 66

def self.is_gif_image(header)
  header[0] == 0x47 && header[1] == 0x49 && header[2] == 0x46 && header[3] == 0x38 && (header[4] == 0x37 ||
                                                                                       header[4] == 0x39) && header[5] == 0x61
end

.is_jpeg2000_image(header) ⇒ Object



49
50
51
52
53
54
# File 'lib/ruby_client/ImageResource.rb', line 49

def self.is_jpeg2000_image(header)
  (header[0] == 0x00 && header[1] == 0x00 && header[2] == 0x00 && header[3] == 0x0C && header[4] == 0x6A &&
   header[5] == 0x50 && (header[6] == 0x1A || header[6] == 0x20) && (header[7] == 0x1A || header[7] == 0x20) &&
   header[8] == 0x0D && header[9] == 0x0A && header[10] == 0x87 && header[11] == 0x0A) ||
    (header[0] == 0xFF && header[1] == 0x4F && header[2] == 0xFF && header[3] == 0x51 && header[6] == 0x00 && header[7] == 0x00)
end

.is_jpeg_image(header) ⇒ Object



71
72
73
# File 'lib/ruby_client/ImageResource.rb', line 71

def self.is_jpeg_image(header)
  header[0] == 0xFF && header[1] == 0xD8 && header[2] == 0xFF
end

.is_png_image(header) ⇒ Object



56
57
58
59
# File 'lib/ruby_client/ImageResource.rb', line 56

def self.is_png_image(header)
  header[0] == 0x89 && header[1] == 0x50 && header[2] == 0x4E && header[3] == 0x47 &&
    header[4] == 0x0D && header[5] == 0x0A && header[6] == 0x1A && header[7] == 0x0A
end

.is_tiff_image(header) ⇒ Object



61
62
63
64
# File 'lib/ruby_client/ImageResource.rb', line 61

def self.is_tiff_image(header)
  (header[0] == 0x49 && header[1] == 0x49 && header[2] == 0x2A && header[3] == 0x00) ||
    (header[0] == 0x4D && header[1] == 0x4D && header[2] == 0x00 && header[3] == 0x2A)
end

.is_valid_bitmap_image(header) ⇒ Object



75
76
77
# File 'lib/ruby_client/ImageResource.rb', line 75

def self.is_valid_bitmap_image(header)
  header[0] == 0x42 && header[1] == 0x4D
end

Instance Method Details

#_file_extensionObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_client/ImageResource.rb', line 22

def _file_extension
  @file_header = @data[0, 16]
  @file_header = @file_header.bytes

  if ImageResource.is_png_image(@file_header)
    @_mime_type = "image/png"
    ".png"
  elsif ImageResource.is_jpeg_image(@file_header)
    @_mime_type = "image/jpeg"
    ".jpeg"
  elsif ImageResource.is_gif_image(@file_header)
    @_mime_type = "image/gif"
    ".gif"
  elsif ImageResource.is_tiff_image(@file_header)
    @_mime_type = "image/tiff"
    ".tiff"
  elsif ImageResource.is_jpeg2000_image(@file_header)
    @_mime_type = "image/jpeg"
    ".jpeg"
  elsif ImageResource.is_valid_bitmap_image(@file_header)
    @_mime_type = "image/bmp"
    ".bmp"
  else
    raise "Not supported image type or invalid image."
  end
end

#to_json(_options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/ruby_client/ImageResource.rb', line 79

def to_json(_options = {})
  json_array = {}
  json_array["type"] = "image"
  json_array["align"] = 1
  json_array["vAlign"] = 1

  json_array["resourceName"] = @resource_name
  JSON.pretty_generate(json_array)
end