This repository has been archived by the owner on Dec 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathvips.rb
300 lines (261 loc) · 8.27 KB
/
vips.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# encoding: utf-8
module CarrierWave
module Vips
def self.configure
@config ||= begin
c = Struct.new(:sharpen_mask, :sharpen_scale, :allowed_formats).new
c.sharpen_mask = [ [ -1, -1, -1 ], [ -1, 24, -1 ], [ -1, -1, -1 ] ]
c.sharpen_scale = 16
c.allowed_formats = %w(jpeg jpg png)
c
end
@config
yield @config if block_given?
@config
end
def self.included(base)
base.send(:extend, ClassMethods)
end
module ClassMethods
def resize_to_limit(width, height)
process :resize_to_limit => [width, height]
end
def resize_to_fit(width, height)
process :resize_to_fit => [width, height]
end
def resize_to_fill(width, height)
process :resize_to_fill => [width, height]
end
def quality(percent)
process :quality => percent
end
def convert(extension)
process :convert => extension
end
def strip
process :strip
end
def auto_orient
process :auto_orient
end
end
##
# Read the camera EXIF data to determine orientation and adjust accordingly
#
def auto_orient
manipulate! do |image|
o = image.get('exif-Orientation').to_i rescue nil
o ||= image.get('exif-ifd0-Orientation').to_i rescue 1
case o
when 1
# Do nothing, everything is peachy
when 6
image.rot270
when 8
image.rot180
when 3
image.rot90
else
raise('Invalid value for Orientation: ' + o.to_s)
end
image.set_type GObject::GSTR_TYPE, 'exif-Orientation', ''
image.set_type GObject::GSTR_TYPE, 'exif-ifd0-Orientation', ''
end
end
##
# Change quality of the image (if supported by file format)
#
#
# === Parameters
# [percent (Integer)] quality from 0 to 100
#
def quality(percent)
write_opts[:Q] = percent
get_image
end
##
# Remove all exif and icc data when writing to a file. This method does
# not actually remove any metadata but rather marks it to be removed when
# writing the file.
#
def strip
write_opts[:strip] = true
get_image
end
##
# Convert the file to a different format
#
#
# === Parameters
# [f (String)] the format for the file format (jpeg, png)
# [opts (Hash)] options to be passed to converting function (ie, :interlace => true for png)
#
def convert(f, opts = {})
opts = opts.dup
f = f.to_s.downcase
allowed = cwv_config.allowed_formats
raise ArgumentError, "Format must be one of: #{allowed.join(',')}" unless allowed.include?(f)
self.format_override = f == 'jpeg' ? 'jpg' : f
opts[:Q] = opts.delete(:quality) if opts.has_key?(:quality)
write_opts.merge!(opts)
get_image
end
##
# 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
#
def resize_to_fit(new_width, new_height)
manipulate! do |image|
resize_image(image,new_width,new_height)
end
end
##
# 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
#
def resize_to_fill(new_width, new_height)
manipulate! do |image|
image = resize_image image, new_width, new_height, :max
if image.width > new_width
top = 0
left = (image.width - new_width) / 2
elsif image.height > new_height
left = 0
top = (image.height - new_height) / 2
else
left = 0
top = 0
end
# Floating point errors can sometimes chop off an extra pixel
# TODO: fix all the universe so that floating point errors never happen again
new_height = image.height if image.height < new_height
new_width = image.width if image.width < new_width
image.extract_area(left, top, new_width, new_height)
end
end
##
# 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
#
def resize_to_limit(new_width, new_height)
manipulate! do |image|
image = resize_image(image,new_width,new_height) if new_width < image.width || new_height < image.height
image
end
end
##
# Manipulate the image with Vips. Saving of the image is delayed until after
# all the process blocks have been called. Make sure you always return an
# Vips::Image object from the block
#
# === Gotcha
#
# This method assumes that the object responds to +current_path+ and +file+.
# Any class that this module is mixed into must have a +current_path+ and a +file+ method.
# CarrierWave::Uploader does, so you won't need to worry about this in
# most cases.
#
# === Yields
#
# [Vips::Image] for further manipulation
#
# === Raises
#
# [CarrierWave::ProcessingError] if manipulation failed.
#
def manipulate!
@_vimage ||= get_image
@_vimage = yield @_vimage
rescue => e
raise CarrierWave::ProcessingError.new("Failed to manipulate file, maybe it is not a supported image? Original Error: #{e}")
end
def process!(*)
ret = super
if @_vimage
ext_regex = /(\.[[:alnum:]]+)$/
ext = format_override ? "_tmp.#{format_override}" : '_tmp\1'
tmp_name = current_path.sub(ext_regex, ext)
opts = write_opts.dup
opts.delete(:Q) unless write_jpeg?(tmp_name)
@_vimage.write_to_file(tmp_name, **opts)
FileUtils.mv(tmp_name, current_path)
@_vimage = nil
end
ret
end
def filename
return unless original_filename
format_override ? original_filename.sub(/\.[[:alnum:]]+$/, ".#{format_override}") : original_filename
end
private
attr_accessor :format_override
def get_image
cache_stored_file! unless cached?
@_vimage ||= if jpeg? || png?
::Vips::Image.new_from_file(current_path, access: :sequential)
else
::Vips::Image.new_from_file(current_path)
end
end
def write_opts
@_write_opts ||= {}
end
def resize_image(image, width, height, min_or_max = :min)
ratio = get_ratio image, width, height, min_or_max
return image if ratio == 1
if ratio > 1
image = image.resize(ratio, kernel: :nearest)
else
image = image.resize(ratio, kernel: :cubic)
image = image.conv(cwv_sharpen_mask) if cwv_config.sharpen_mask
end
image
end
def get_ratio(image, width,height, min_or_max = :min)
width_ratio = width.to_f / image.width
height_ratio = height.to_f / image.height
[width_ratio, height_ratio].send(min_or_max)
end
def jpeg?(path = current_path)
%w(jpg jpeg).include? ext(path)
end
def png?(path = current_path)
ext(path) == 'png'
end
def write_jpeg?(path)
format_override == 'jpg' || jpeg?(path)
end
def ext(path)
matches = /\.([[:alnum:]]+)$/.match(path)
matches && matches[1].downcase
end
def cwv_config
CarrierWave::Vips.configure
end
def cwv_sharpen_mask(mask = cwv_config.sharpen_mask, scale = cwv_config.sharpen_scale)
::Vips::Image.new_from_array(mask, scale)
end
end # Vips
end # CarrierWave