Class: Utopia::Gallery::ResizeImage

Inherits:
Process
  • Object
show all
Defined in:
lib/utopia/gallery/process.rb

Direct Known Subclasses

ResizeVector

Instance Attribute Summary collapse

Attributes inherited from Process

#name

Instance Method Summary collapse

Methods inherited from Process

fresh?, link, mtime

Constructor Details

#initialize(name, size = [800, 800], method = :resize_to_fit, **options) ⇒ ResizeImage

Returns a new instance of ResizeImage.



65
66
67
68
69
70
71
# File 'lib/utopia/gallery/process.rb', line 65

def initialize(name, size = [800, 800], method = :resize_to_fit, **options)
	super(name)
	
	@size = size
	@method = method
	@options = options
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



74
75
76
# File 'lib/utopia/gallery/process.rb', line 74

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



75
76
77
# File 'lib/utopia/gallery/process.rb', line 75

def options
  @options
end

#sizeObject (readonly)

Returns the value of attribute size.



73
74
75
# File 'lib/utopia/gallery/process.rb', line 73

def size
  @size
end

Instance Method Details

#call(cache) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/utopia/gallery/process.rb', line 99

def call(cache)
	input_path = cache.input_path
	output_path = cache.output_path_for(self)
	
	return if Process.fresh?(input_path, output_path)
	
	resizer = resizer_for(cache.input_path)
	
	FileUtils.mkdir_p(File.dirname(output_path))
	
	generate_output(resizer, output_path)
ensure
	resizer&.close
end

#generate_output(resizer, output_path) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/utopia/gallery/process.rb', line 87

def generate_output(resizer, output_path)
	if output_image = resizer.send(@method, @size)
		output_image.write_to_file output_path, **@options
	else
		Process.link(resizer.input_path, output_path)
	end
end

#relative_path(media) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/utopia/gallery/process.rb', line 77

def relative_path(media)
	path = super
	
	if path.match?(/\.(pdf|svg)$/)
		path += ".png"
	end
	
	return path
end

#resizer_for(input_path) ⇒ Object



95
96
97
# File 'lib/utopia/gallery/process.rb', line 95

def resizer_for(input_path)
	Vips::Thumbnail::Resizer.new(input_path)
end