oh man this is getting hard
This commit is contained in:
parent
08557ce751
commit
ba17ce94f0
|
@ -1,21 +1,32 @@
|
||||||
|
require 'pathname'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
module ResponsiveImage
|
module ResponsiveImage
|
||||||
class Image
|
class Image
|
||||||
def initialize(width, height, config)
|
def initialize(path, width, height, config)
|
||||||
|
@path = path.force_encoding(Encoding::UTF_8)
|
||||||
@width = width
|
@width = width
|
||||||
@height = height
|
@height = height
|
||||||
@config = config
|
@config = config
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The directory name, relative to base_path
|
||||||
|
def dirname
|
||||||
|
base_path = Pathname.new(File.join(@config[:site_source], @config['base_path']))
|
||||||
|
image_path = Pathname.new(File.join(@config[:site_source], @path))
|
||||||
|
|
||||||
|
image_path.relative_path_from(base_path).dirname.to_s.delete('.')
|
||||||
|
end
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
'path' => '',
|
'basename' => File.basename(@path),
|
||||||
'width' => @width,
|
'dirname' => dirname,
|
||||||
|
'extension' => File.extname(@path).delete('.'),
|
||||||
|
'filename' => File.basename(@path, '.*'),
|
||||||
'height' => @height,
|
'height' => @height,
|
||||||
'basename' => '',
|
'path' => @path,
|
||||||
'dirname' => '',
|
'width' => @width
|
||||||
'filename' => '',
|
|
||||||
'extension' => ''
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ module Jekyll
|
||||||
image = Magick::Image::read(path).first
|
image = Magick::Image::read(path).first
|
||||||
|
|
||||||
{
|
{
|
||||||
original: Image.new,
|
original: Image.new(image.filename, image.columns, image.rows, config),
|
||||||
resized: ImageResizer.resize(image, config),
|
resized: ImageResizer.resize(image, config),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def resize(image, config)
|
def resize(image, config)
|
||||||
resized = []
|
results = []
|
||||||
|
|
||||||
config['sizes'].each do |size|
|
config['sizes'].each do |size|
|
||||||
width = size['width']
|
width = size['width']
|
||||||
|
@ -15,28 +15,52 @@ module Jekyll
|
||||||
|
|
||||||
next unless needs_resizing?(image, width)
|
next unless needs_resizing?(image, width)
|
||||||
|
|
||||||
image_path = image.filename.force_encoding(Encoding::UTF_8)
|
image_path = image.filename
|
||||||
output_path = image_path
|
source_img = Image.new(image_path, width, height, config)
|
||||||
|
site_source_path = format_output_path(config['output_path_format'], source_img.to_h)
|
||||||
|
|
||||||
resized.push(Image.new(width, height, config))
|
resized_img = Image.new(site_source_path, width, height, config)
|
||||||
|
results.push(resized_img)
|
||||||
|
|
||||||
# Don't resize images more than once
|
# Don't resize images more than once
|
||||||
next if File.exist?(output_path)
|
next if File.exist?(site_source_path)
|
||||||
|
|
||||||
|
site_dest_path = File.join(config[:site_dest], site_source_path)
|
||||||
|
ensure_output_dir_exists!(site_source_path)
|
||||||
|
ensure_output_dir_exists!(site_dest_path)
|
||||||
|
|
||||||
|
Jekyll.logger.info "Generating #{site_source_path}"
|
||||||
|
|
||||||
resized = image.scale(ratio)
|
resized = image.scale(ratio)
|
||||||
resized.write(output_path) do |i|
|
resized.write(site_source_path) do |i|
|
||||||
i.quality = size['quality'] || config['default_quality']
|
i.quality = size['quality'] || config['default_quality']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Ensure the generated file is copied to the _site directory
|
||||||
|
Jekyll.logger.info "Copying image to #{site_dest_path}"
|
||||||
|
FileUtils.copy_file(site_source_path, site_dest_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
resized
|
results
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_output_path(format, image_hash)
|
||||||
|
params = symbolize_keys(image_hash)
|
||||||
|
|
||||||
|
Pathname.new(format % params).cleanpath.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def symbolize_keys(hash)
|
||||||
|
hash.each_with_object({}){ |(key, val), h| h[key.to_sym] = val }
|
||||||
end
|
end
|
||||||
|
|
||||||
def needs_resizing?(image, width)
|
def needs_resizing?(image, width)
|
||||||
image.columns > width
|
image.columns > width
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_output_dir_exists!(dir)
|
def ensure_output_dir_exists!(path)
|
||||||
|
dir = File.dirname(path)
|
||||||
|
|
||||||
unless Dir.exist?(dir)
|
unless Dir.exist?(dir)
|
||||||
Jekyll.logger.info "Creating output directory #{dir}"
|
Jekyll.logger.info "Creating output directory #{dir}"
|
||||||
FileUtils.mkdir_p(dir)
|
FileUtils.mkdir_p(dir)
|
||||||
|
|
Loading…
Reference in New Issue