Module: Smartthumbs::Thumbable

Defined in:
lib/smartthumbs/thumbable.rb

Instance Method Summary collapse

Instance Method Details

#create_directoryObject

creates the directory for a certain @format if it doesn’t exist



54
55
56
57
# File 'lib/smartthumbs/thumbable.rb', line 54

def create_directory
  dest = File.dirname(thumb_path_for(@format))
  FileUtils.mkdir_p(dest) unless File.exists?(dest)
end

#create_thumb_for(format) ⇒ Object

Creates the thumb for a certain @format



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/smartthumbs/thumbable.rb', line 60

def create_thumb_for(format)
  @format = format
  return if st_format(@format).blank?
  
  create_directory
  
  method = st_format(@format)[1] || :cut
  @x, @y = st_format(@format).first.split("x").map(&:to_i)

  if self.respond_to?(method)
    self.send(method) 
  end
  
  rounding_error
  rmagick_img.write(thumb_path_for(@format)) { self.quality = 80 }
  nil
end

#cutObject

resizes and cuts the image, so it that it fits exactly



139
140
141
# File 'lib/smartthumbs/thumbable.rb', line 139

def cut
  rmagick_img.crop_resized!(@x, @y, gravity)
end

#fillObject

the same as fit, except the fact that the image get’s filled up with a border



129
130
131
132
133
134
135
136
# File 'lib/smartthumbs/thumbable.rb', line 129

def fill
  fit
  rounding_error
  border_x = (@x - rmagick_img.columns)/2
  border_y = (@y - rmagick_img.rows)/2

  rmagick_img.border!(border_x,border_y,"white")
end

#fitObject

resizes the image in a manner that both edges fit the needs. usually one of the edges is smaller than the needs afterwards



119
120
121
122
123
124
125
# File 'lib/smartthumbs/thumbable.rb', line 119

def fit
  if self.needs_to_be_resized?
    rmagick_img.resize_to_fit!(@x, @y)
  else
    rmagick_img.resize_to_fit(@x, @y)
  end
end

#gravityObject

returns the gravity for the current resizing process and provides some shrotcuts



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/smartthumbs/thumbable.rb', line 80

def gravity
  @gravity ||= {
    :new => Magick::NorthWestGravity,
    :n =>   Magick::NorthGravity,
    :ne =>  Magick::NorthEastGravity,
    :w =>   Magick::WestGravity,
    :c =>   Magick::CenterGravity,
    :e =>   Magick::EastGravity,
    :sw =>  Magick::SouthWestGravity,
    :s =>   Magick::SouthGravity,
    :se =>  Magick::SouthEastGravity
  }[st_format(@format).last]
  
  @gravity ||= Magick::CenterGravity
end

#needs_to_be_resized?Boolean

checks whether the image needs to be resized to fit the current @format or not

Returns:

  • (Boolean)


154
155
156
# File 'lib/smartthumbs/thumbable.rb', line 154

def needs_to_be_resized?
  rmagick_img.rows > @y || rmagick_img.columns > @x
end

#rmagick_imgObject

return the rmagick instance



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/smartthumbs/thumbable.rb', line 6

def rmagick_img
  return @rmagick_img unless @rmagick_img.blank?
  if self.class.st_config[:blob].present?
    @rmagick_img ||= Magick::ImageList.new.from_blob(
      self.send(self.class.st_config[:blob])
    ).first
  elsif self.class.st_config[:file].present?
    @rmagick_img ||= Magick::ImageList.new.from_blob(
      File.read(self.send(self.class.st_config[:file]))
    ).first
  else
    raise "No thumb source defined. You have to define neither :blob or :file"
  end
  
  if self.class.st_config[:before_resize].present? && self.respond_to?(self.class.st_config[:before_resize].to_sym)
    self.send(self.class.st_config[:before_resize].to_sym)
  end
  @rmagick_img
  
end

#rounding_errorObject

if there’s just a small difference between the needs and the result, we’ll make it fit exaclty



145
146
147
148
149
150
151
# File 'lib/smartthumbs/thumbable.rb', line 145

def rounding_error
  dif = (@y-rmagick_img.rows) + (@x-rmagick_img.columns)

  if dif > 0 && dif < 10 then
    rmagick_img.resize!(@x, @y)
  end
end

#st_extensionObject

returns the file extension for the current image



44
45
46
47
48
49
50
51
# File 'lib/smartthumbs/thumbable.rb', line 44

def st_extension
  return "jpg" unless self.class.st_config[:extension].present?
  if self.class.st_config[:extension].is_a?(String)
    self.class.st_config[:extension]
  else
    self.send(self.class.st_config[:extension])
  end
end

#st_format(f) ⇒ Object

returns the specific format-array for the key f e.g. [“100x200”, :cut] The config value for formats can be a hash or sth that responds to call e.g. a lambda.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/smartthumbs/thumbable.rb', line 31

def st_format(f)
  if self.class.st_config[:formats].respond_to?(:call)
    self.class.st_config[:formats].call(f.to_sym)
  else
    if self.class.st_config[:formats][f].is_a?(Symbol)
      self.send(self.class.st_config[:formats][f])
    else
      self.class.st_config[:formats][f]
    end
  end
end

#thumb_exists_for?(format) ⇒ Boolean

Does a thumb already exist?

Returns:

  • (Boolean)


97
98
99
# File 'lib/smartthumbs/thumbable.rb', line 97

def thumb_exists_for?(format)
  File.exists?(self.thumb_path_for(format))
end

#thumb_path_for(format) ⇒ Object

returns the cache-path for a certain image



102
103
104
# File 'lib/smartthumbs/thumbable.rb', line 102

def thumb_path_for(format)
  "#{Rails.root}/public#{thumb_url_for(format)}"
end

#thumb_url_for(format) ⇒ Object

return the http url to the resized image this one has to be route from which the image is availabe - otherwise the caching benefit is gone



109
110
111
112
113
114
115
# File 'lib/smartthumbs/thumbable.rb', line 109

def thumb_url_for(format)
  if Smartthumbs::Config.get_option(:assume_klass).to_s == self.class.to_s
    "/th/#{format.to_s}/#{self.id}.#{st_extension}"
  else
    "/th/#{self.class.to_s.underscore.parameterize}/#{format.to_s}/#{self.id}.#{st_extension}"
  end
end