Class: Plugin::Thumbnailer::InputImage

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

Instance Method Summary collapse

Methods included from MetaData

#mime_type

Constructor Details

#initialize(image, processing_methods, options = {}) ⇒ InputImage

Returns a new instance of InputImage.



90
91
92
93
# File 'lib/httpthumbnailer/plugin/thumbnailer.rb', line 90

def initialize(image, processing_methods, options = {})
	@image = image
	@processing_methods = processing_methods
end

Instance Method Details

#equal?(image) ⇒ Boolean

needs to be seen as @image when returned in replace block

Returns:

  • (Boolean)


151
152
153
# File 'lib/httpthumbnailer/plugin/thumbnailer.rb', line 151

def equal?(image)
	super image or @image.equal? image
end

#heightObject



146
147
148
# File 'lib/httpthumbnailer/plugin/thumbnailer.rb', line 146

def height
	@image.base_rows
end

#process_image(method, width, height, options) ⇒ Object



123
124
125
126
127
128
# File 'lib/httpthumbnailer/plugin/thumbnailer.rb', line 123

def process_image(method, width, height, options)
	@image.replace do |image|
		impl = @processing_methods[method] or raise UnsupportedMethodError, method
		impl.call(image, width, height, options)
	end
end

#thumbnail(spec) ⇒ Object



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

def thumbnail(spec)
	spec = spec.dup
	# default backgraud 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

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

	begin
		process_image(spec.method, width, height, spec.options).replace do |image|
			if image.alpha?
				log.info 'thumbnail has alpha, rendering on background'
				image.render_on_background(spec.options['background-color'])
			end
		end.use do |image|
			Service.stats.incr_total_thumbnails_created
			image_format = spec.format == :input ? @image.format : spec.format

			yield Thumbnail.new(image, image_format, spec.options)
		end
	rescue Magick::ImageMagickError => error
		raise ImageTooLargeError, error.message if error.message =~ /cache resources exhausted/
		raise
	end
end

#useObject

behave as @image in processing



131
132
133
134
135
# File 'lib/httpthumbnailer/plugin/thumbnailer.rb', line 131

def use
	@image.use do |image|
		yield self
	end
end

#widthObject

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



142
143
144
# File 'lib/httpthumbnailer/plugin/thumbnailer.rb', line 142

def width
	@image.base_columns
end