Module: CarrierWave::ImageSorcery

Extended by:
ActiveSupport::Concern
Defined in:
lib/carrierwave-imagesorcery/version.rb,
lib/carrierwave-imagesorcery/image_sorcery.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#convert(format) ⇒ Object

Changes the image encoding format to the given format

See www.imagemagick.org/script/command-line-options.php#format

Parameters

format (#to_s)

an abreviation of the format

Examples

image.convert(:png)


43
44
45
46
47
48
49
# File 'lib/carrierwave-imagesorcery/image_sorcery.rb', line 43

def convert(format)
  manipulate! do |img|
    img.manipulate!(:format => format.to_s.downcase)
    img = yield(img) if block_given?
    img
  end
end

#dimensionsObject



160
161
162
163
164
# File 'lib/carrierwave-imagesorcery/image_sorcery.rb', line 160

def dimensions
   manipulate! do |img|
    img.dimensions
  end
end

#manipulate!Object



166
167
168
169
170
171
172
# File 'lib/carrierwave-imagesorcery/image_sorcery.rb', line 166

def manipulate!
  cache_stored_file! if !cached?
  image = Sorcery.new current_path
  image = yield(image)
rescue RuntimeError, StandardError => e
  raise CarrierWave::ProcessingError , I18n.translate(:"errors.messages.imagesorcery_processing_error", :e => e)
end

#resize_and_pad(width, height, background = :transparent, gravity = 'Center') ⇒ Object

Resize the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with the given color, which defaults to transparent (for gif and png, white for jpeg).

See www.imagemagick.org/script/command-line-options.php#gravity for gravity options.

Parameters

width (Integer)

the width to scale the image to

height (Integer)

the height to scale the image to

background (String, :transparent)

the color of the background as a hexcode, like “#ff45de”

gravity (String)

how to position the image



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/carrierwave-imagesorcery/image_sorcery.rb', line 147

def resize_and_pad(width, height, background=:transparent, gravity='Center')
  manipulate! do |img|
    opt={}
    opt[:thumbnail] = "#{width}x#{height}>"
    background == :transparent ? opt[:background] = "rgba(255, 255, 255, 0.0)" : opt[:background] = background
    opt[:gravity] = gravity
    opt[:extent] = "#{width}x#{height}"
    img.manipulate!(opt)
    img = yield(img) if block_given?
    img
  end
end

#resize_to_fill(width, height, gravity = 'Center') ⇒ Object

Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension.

Parameters

width (Integer)

the width to scale the image to

height (Integer)

the height to scale the image to

gravity (String)

the current gravity suggestion (default: ‘Center’; options: ‘NorthWest’, ‘North’, ‘NorthEast’, ‘West’, ‘Center’, ‘East’, ‘SouthWest’, ‘South’, ‘SouthEast’)



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/carrierwave-imagesorcery/image_sorcery.rb', line 111

def resize_to_fill(width, height, gravity = 'Center')
  manipulate! do |img|
    cols, rows = img.dimensions[:x].to_i, img.dimensions[:y].to_i
    opt={}
    if width != cols || height != rows
      scale = [width/cols.to_f, height/rows.to_f].max
      cols = (scale * (cols + 0.5)).round
      rows = (scale * (rows + 0.5)).round
      opt[:resize] = "#{cols}x#{rows}"
    end
    opt[:gravity] = gravity
    opt[:background] = "rgba(255,255,255,0.0)"
    opt[:extent] = "#{width}x#{height}" if cols != width || rows != height
    img.manipulate!(opt)
    img = yield(img) if block_given?
    img
  end
end

#resize_to_fit(width, height) ⇒ Object

Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.

Parameters

width (Integer)

the width to scale the image to

height (Integer)

the height to scale the image to



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/carrierwave-imagesorcery/image_sorcery.rb', line 86

def resize_to_fit(width, height)
  manipulate! do |img|
    img.manipulate!(:resize => "#{width}x#{height}")
    img = yield(img) if block_given?
    if img
      img
    else
      raise CarrierWave::ProcessingError , I18n.translate(:"errors.messages.imagesorcery_processing_error")
    end
    # img
  end
end

#resize_to_limit(width, height) ⇒ Object

Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.

Parameters

width (Integer)

the width to scale the image to

height (Integer)

the height to scale the image to



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/carrierwave-imagesorcery/image_sorcery.rb', line 62

def resize_to_limit(width, height)
  manipulate! do |img|
    img.manipulate!(:resize => "#{width}x#{height}>")
    # img = yield(img) if block_given?
    if img
      img
    else
      raise CarrierWave::ProcessingError , I18n.translate(:"errors.messages.imagesorcery_processing_error")
    end
    # img
  end
end