Module: N::GfxUtils

Defined in:
lib/n/utils/gfx.rb

Overview

GfxUtils

Design:

A simple wrapper around ImageMagick.

Implement as a module to avoid class polution. You can still Ruby’s advanced features to include the module in your class. Passing the object to act upon allows to check for nil, which isn’t possible if you use self.

Class Method Summary collapse

Class Method Details

.create_thumbnail(src, dest, width, height) ⇒ Object

Input:

width, height = dimensions for the scaled image.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/n/utils/gfx.rb', line 30

def self.create_thumbnail(src, dest, width, height)
	# gmosx: [0] selects the 1st frame in case of animated gifs.
	# gmosx: SOS!!! the +profile "*" is need to strip metadata that
	#      fuckup the internet explorer!
	if (dest =~ /jpg$/i) or (File.stat(src).size > 60000)
		# keep only the first frame
		system %|convert +profile "*" +compress -scale '#{width}x#{height}>' -antialias #{src}[0] #{dest}|
	else
		system %|convert +profile "*" +compress -scale '#{width}x#{height}>' -antialias #{src} #{dest}|
	end
end

.grab_exif(path) ⇒ Object

Use ImageMagick to grab the EXIF data contained in a jpeg file and store it to a hash: EXIF field => EXIF value.

A sample EXIF table:

Make=Canon Model=Canon PowerShot S110 Orientation=1 X Resolution=180/1 YResolution=180/1 ResolutionUnit=2 DateTime=2002:05:16 21:02:36 YCbCrPositioning=1 ExifOffset=196 ExposureTime=1/80 FNumber=28/10 ExifVersion=0210 DateTimeOriginal=2002:05:16 21:02:36 DateTimeDigitized=2002:05:16 21:02:36 ComponentsConfiguration=… CompressedBitsPerPixel=3/1 ShutterSpeedValue=202/32 ApertureValue=95/32 ExposureBiasValue=0/3 MaxApertureValue=194698/65536 SubjectDistance=772/1000 MeteringMode=5 Flash=1 FocalLength=173/32 MakerNote=. UserComment= FlashPixVersion=0100 ColorSpace=1 ExifImageWidth=1600 ExifImageLength=1200 InteroperabilityOffset=1328 unknown=R98 unknown=0100 unknown=1600 unknown=1200 FocalPlaneXResolution=1600000/206 FocalPlaneYResolution=1200000/155 FocalPlaneResolutionUnit=2 SensingMethod=2 FileSource=.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/n/utils/gfx.rb', line 89

def self.grab_exif(path)
	info = %x|identify -format "%[EXIF:*]" #{path}|.split("\n")

	exif = {}
	info.each { |l|
		tag, value = l.split("=")
		exif[tag] = value
	} 
	
	unless exif.empty?
		return exif
	else
		return nil
	end
end