oh man this is getting hard
This commit is contained in:
parent
08557ce751
commit
ba17ce94f0
|
@ -1,21 +1,32 @@
|
|||
require 'pathname'
|
||||
|
||||
module Jekyll
|
||||
module ResponsiveImage
|
||||
class Image
|
||||
def initialize(width, height, config)
|
||||
def initialize(path, width, height, config)
|
||||
@path = path.force_encoding(Encoding::UTF_8)
|
||||
@width = width
|
||||
@height = height
|
||||
@config = config
|
||||
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
|
||||
{
|
||||
'path' => '',
|
||||
'width' => @width,
|
||||
'height' => @height,
|
||||
'basename' => '',
|
||||
'dirname' => '',
|
||||
'filename' => '',
|
||||
'extension' => ''
|
||||
'basename' => File.basename(@path),
|
||||
'dirname' => dirname,
|
||||
'extension' => File.extname(@path).delete('.'),
|
||||
'filename' => File.basename(@path, '.*'),
|
||||
'height' => @height,
|
||||
'path' => @path,
|
||||
'width' => @width
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ module Jekyll
|
|||
image = Magick::Image::read(path).first
|
||||
|
||||
{
|
||||
original: Image.new,
|
||||
original: Image.new(image.filename, image.columns, image.rows, config),
|
||||
resized: ImageResizer.resize(image, config),
|
||||
}
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@ module Jekyll
|
|||
end
|
||||
|
||||
def resize(image, config)
|
||||
resized = []
|
||||
results = []
|
||||
|
||||
config['sizes'].each do |size|
|
||||
width = size['width']
|
||||
|
@ -15,28 +15,52 @@ module Jekyll
|
|||
|
||||
next unless needs_resizing?(image, width)
|
||||
|
||||
image_path = image.filename.force_encoding(Encoding::UTF_8)
|
||||
output_path = image_path
|
||||
image_path = image.filename
|
||||
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
|
||||
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.write(output_path) do |i|
|
||||
resized.write(site_source_path) do |i|
|
||||
i.quality = size['quality'] || config['default_quality']
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
def needs_resizing?(image, width)
|
||||
image.columns > width
|
||||
end
|
||||
|
||||
def ensure_output_dir_exists!(dir)
|
||||
def ensure_output_dir_exists!(path)
|
||||
dir = File.dirname(path)
|
||||
|
||||
unless Dir.exist?(dir)
|
||||
Jekyll.logger.info "Creating output directory #{dir}"
|
||||
FileUtils.mkdir_p(dir)
|
||||
|
|
Loading…
Reference in New Issue