Class: Thingfish::Processor::Image

Inherits:
Thingfish::Processor
  • Object
show all
Extended by:
Configurability, Loggability, Strelka::MethodUtilities
Defined in:
lib/thingfish/processor/image.rb

Overview

Image processor plugin for Thingfish

Defined Under Namespace

Classes: MagickOperations

Constant Summary collapse

VERSION =

Package version

'0.1.1'
REVISION =

Version control revision

%q$Revision: c3964930bd4f $
CONFIG_DEFAULTS =

The default dimensions of thumbnails

{
	thumbnail_dimensions: '100x100',
}
IGNORED_MIMETYPES =

An array of mediatypes to ignore, even though ImageMagick claims it groks them

%w[
	application/octet-stream
	text/html
	text/plain
	text/x-server-parsed-html
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImage

Set up a new Filter object



69
70
71
72
73
74
75
# File 'lib/thingfish/processor/image.rb', line 69

def initialize( * ) # :notnew:
	super

	@supported_formats = find_supported_formats()
	@handled_types     = find_handled_types( @supported_formats )
	@generated_types   = find_generated_types( @supported_formats )
end

Instance Attribute Details

#generated_typesObject (readonly)

The mediatypes the plugin can generated in a response, as ThingFish::AcceptParam objects



91
92
93
# File 'lib/thingfish/processor/image.rb', line 91

def generated_types
  @generated_types
end

#handled_typesObject (readonly)

The mediatypes the plugin accepts in a request, as ThingFish::AcceptParam objects



87
88
89
# File 'lib/thingfish/processor/image.rb', line 87

def handled_types
  @handled_types
end

#supported_formatsObject (readonly)

The Hash of formats and the operations they support.



83
84
85
# File 'lib/thingfish/processor/image.rb', line 83

def supported_formats
  @supported_formats
end

Class Method Details

.configure(config = nil) ⇒ Object

Configurability API – configure the processor with the :images section of the config.



56
57
58
59
60
# File 'lib/thingfish/processor/image.rb', line 56

def self::configure( config=nil )
	config = self.defaults.merge( config || {} )

	self.thumbnail_dimensions = config[:thumbnail_dimensions].split('x', 2).map( &:to_i )
end

Instance Method Details

#handled_type?(type) ⇒ Boolean Also known as: is_handled_type?

Returns true if the given media type is one the processor handles. Overridden so the types can be used by the instance.

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/thingfish/processor/image.rb', line 96

def handled_type?( type )
	self.log.debug "Looking for handled type for: %p" % [ type ]
	result = self.handled_types.find {|handled_type| type =~ handled_type }
	self.log.debug "  found: %p" % [ result ]
	return result
end

#inspectObject

Return a human-readable representation of the receiving object suitable for debugging.



145
146
147
148
149
150
151
152
153
# File 'lib/thingfish/processor/image.rb', line 145

def inspect
	return "#<%p:%#x %d supported image formats, %d readable, %d writable>" % [
		self.class,
		self.object_id * 2,
		self.supported_formats.size,
		self.handled_types.size,
		self.generated_types.size,
	]
end

#on_request(request) ⇒ Object

Synchronous processor API – extract metadata from uploaded images.



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
# File 'lib/thingfish/processor/image.rb', line 106

def on_request( request )
	self.log.debug "Image-processing %p" % [ request ]
	image = case request.body
			when StringIO
				self.log.debug "  making a single image from a StringIO"
				Magick::Image.from_blob( request.body.read )
			else
				path = request.body.path
				self.log.debug "  making a flattened image out of %p" % [ path ]
				path = path.to_s + '[0]' # ImageMagick, read a single frame
				list = Magick::ImageList.new( path )
				list.flatten_images
			end

	image = image.first if image.respond_to?( :first )
	self.log.debug "  image is: %p" % [ image ]
	 = self.( image )
	self.log.debug "  extracted image metadata: %p" % [  ]
	request.(  )

	self.log.debug "  going to generate a thumbnail..."
	self.generate_thumbnail( image, ['title'] ) do |thumbio, |
		self.log.debug "  generated: %p (%p)" % [ thumbio,  ]
		request.add_related_resource( thumbio,  )
	end

	self.log.debug "  destroying the image to free up memory"

rescue Magick::ImageMagickError => err
	self.log.error "Problem while processing file %p: %s" % [ err.class, err.message ]
	self.log.debug { err.backtrace.join( "\n  " ) }

ensure
	image.destroy! if image
end

#thumbnail_dimensionsObject

The (maximum) dimensions of generated thumbnails



50
# File 'lib/thingfish/processor/image.rb', line 50

singleton_attr_accessor :thumbnail_dimensions