Class: Plugin::Thumbnailer::Service::InputImage

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, PerfStats
Includes:
ClassLogging, PerfStats, MimeType
Defined in:
lib/httpthumbnailer/plugin/thumbnailer/service/images.rb

Constant Summary collapse

UpscaledError =
Class.new RuntimeError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MimeType

#mime_type

Constructor Details

#initialize(image, thumbnailing_methods, edits) ⇒ InputImage

Returns a new instance of InputImage.



28
29
30
31
32
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 28

def initialize(image, thumbnailing_methods, edits)
	@image = image
	@thumbnailing_methods = thumbnailing_methods
	@edits = edits
end

Class Method Details

.from_blob(blob, thumbnailing_methods, edits, options = {}, &block) ⇒ Object



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
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 34

def self.from_blob(blob, thumbnailing_methods, edits, options = {}, &block)
	mw = options[:max_width]
	mh = options[:max_height]

	begin
		image = measure "loading original image" do
			image = measure "loading image form blob" do
				begin
					images =
						if mw and mh
							measure "loading image form blob with size hint", "#{mw}x#{mh}" do
								log.info "using max size hint of: #{mw}x#{mh}"
								Magick::Image.from_blob(blob) do |info|
									# actual hint is 2x the max thumbnail dimensions so we don't loose too much quality
									define('jpeg', 'size', "#{mw*2}x#{mh*2}")
									define('jbig', 'size', "#{mw*2}x#{mh*2}")
								end
							end
						else
							measure "loading image form blob without size hint" do
								Magick::Image.from_blob(blob)
							end
						end
					begin
						image = images.shift
						begin
							if image.columns > image.base_columns or image.rows > image.base_rows
								log.warn "input image got upscaled from: #{image.base_columns}x#{image.base_rows} to #{image.columns}x#{image.rows}"
								if not options[:no_upscale_fix]
									raise UpscaledError if options[:reload]
									measure "downsampling input image to base size", "#{image.base_columns}x#{image.base_rows}" do
										log.warn "downsampling input image to base size: #{image.base_columns}x#{image.base_rows}"
										image = image.get do |image|
											image.sample(image.base_columns, image.base_rows)
										end
									end
								end
							end
							image
						rescue
							image.destroy!
							raise
						end
					ensure
						images.each do |other|
							other.destroy!
						end
					end
				rescue UpscaledError
					log.warn "reloading input image without max size hint!"
					Service.stats.incr_total_images_reloaded
					mw = mh = nil
					retry
				end
			end
			image.get do |image|
				blob = nil

				log.info "loaded image: #{image.inspect.strip}"
				Service.stats.incr_total_images_loaded

				# clean up the image
				image.strip!
				image.properties do |key, value|
					log.debug "deleting user propertie '#{key}'"
					image[key] = nil
				end
				image
			end.get do |image|
				if mw and mh and not options[:no_downsample]
					f = image.find_downsample_factor(mw, mh)
					if f > 1
						measure "downsampling", image.inspect.strip do
							image = image.downsample(f)
							log.info "downsampled image by factor of #{f}: #{image.inspect.strip}"
							Service.stats.incr_total_images_downsampled
							image
						end
					end
				end
			end
		end
		image.get do |image|
			yield self.new(image, thumbnailing_methods, edits)
			true # make sure it is destroyed
		end
	rescue Magick::ImageMagickError => error
		raise ImageTooLargeError, error if error.message =~ /cache resources exhausted/
		raise UnsupportedMediaTypeError, error
	end
end

Instance Method Details

#_thumbnail(image, spec) ⇒ Object



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
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 140

def _thumbnail(image, spec)
	spec = spec.dup
	# default background is white
	spec.options['background-color'] = spec.options.fetch('background-color', 'white').sub(/^0x/, '#')

	width = spec.width == :input ? @image.columns : spec.width
	height = spec.height == :input ? @image.rows : spec.height
	image_format = spec.format == :input ? @image.format : spec.format

	raise ZeroSizedImageError.new(width, height) if width == 0 or height == 0

	begin
		measure "generating thumbnail to spec", spec do
			image.get do |image|
				if image.alpha?
					measure "rendering image on background", image.inspect.strip  do
						log.info 'image has alpha, rendering on background'
						image.render_on_background(spec.options['background-color'])
					end
				else
					image
				end
			end.get do |image|
				spec.edits.each do |edit|
					log.debug "applying edit '#{edit}'"
					image = image.get do |image|
						measure "edit", edit do
							edit_image(image, edit.name, *edit.args, edit.options, spec)
						end
					end
				end
				image
			end.get do |image|
				log.debug "thumbnailing with method '#{spec.method} #{width}x#{height} #{spec.options}'"
				measure "thumbnailing with method", "#{spec.method} #{width}x#{height} #{spec.options}" do
					thumbnail_image(image, spec.method, width, height, spec.options)
				end
			end.get do |image|
				if image.alpha?
					measure "rendering thumbnail on background", image.inspect.strip do
						log.info 'thumbnail has alpha, rendering on background'
						image.render_on_background(spec.options['background-color'])
					end
				else
					image
				end
			end.get do |image|
				Service.stats.incr_total_thumbnails_created
				yield Thumbnail.new(image, image_format, spec.options)
			end
		end
	rescue Magick::ImageMagickError => error
		raise ImageTooLargeError, error.message if error.message =~ /cache resources exhausted/
		raise
	end
end

#edit_image(image, name, *args, options, spec) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 197

def edit_image(image, name, *args, options, spec)
	impl = @edits[name] or raise UnsupportedEditError, name

	# make sure we pass as many args as expected (filling with nil)
	args_no = impl.arity - 3 # for image, optioins and spec
	args = args.dup
	args.fill(nil, (args.length)...args_no)
	if args.length > args_no
		log.warn "extra arguments to edit '#{name}': #{args[args_no..-1].join(', ')}"
		args = args[0...args_no]
	end

	ret = impl.call(image, *args, options, spec)

	fail "edit '#{name}' returned '#{ret.class.name}' - expecting nil or Magick::Image" unless ret.nil? or ret.kind_of? Magick::Image
	ret or image
rescue PluginContext::PluginArgumentError => error
	raise EditArgumentError.new(name, error.message)
end

#heightObject



235
236
237
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 235

def height
	@image.base_rows
end

#thumbnail(spec, &block) ⇒ Object



133
134
135
136
137
138
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 133

def thumbnail(spec, &block)
	# we don't want to destory the input image after we have generated the thumbnail so we can generate another one
	@image.borrow do |image|
		_thumbnail(image, spec, &block)
	end
end

#thumbnail!(spec, &block) ⇒ Object



126
127
128
129
130
131
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 126

def thumbnail!(spec, &block)
	# it is OK if the image get's destroyed in the process
	@image.get do |image|
		_thumbnail(image, spec, &block)
	end
end

#thumbnail_image(image, method, width, height, options) ⇒ Object



217
218
219
220
221
222
223
224
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 217

def thumbnail_image(image, method, width, height, options)
	impl = @thumbnailing_methods[method] or raise UnsupportedMethodError, method
	ret = impl.call(image, width, height, options)
	fail "thumbnailing method '#{name}' returned '#{ret.class.name}' - expecting nil or Magick::Image" unless ret.nil? or ret.kind_of? Magick::Image
	ret or image
rescue PluginContext::PluginArgumentError => error
	raise ThumbnailArgumentError.new(method, error.message)
end

#widthObject

We use base values since it might have been loaded with size hint and prescaled



231
232
233
# File 'lib/httpthumbnailer/plugin/thumbnailer/service/images.rb', line 231

def width
	@image.base_columns
end