imgpop
·2 mins
Now I will going into technical details inside imgpop.
Like I said in the previous post. I extends Brian’s imgpopup and do some makeup to things.
At the backend, I use mini_magick
to actually save the resized image, and use that as preview/thumbnail.
Here’s the snippet.
#...#
# Open the source image, and scale it accordingly.
image = MiniMagick::Image.open(image_path)
vars['full_width'] = image[:width]
vars['full_height'] = image[:height]
image.resize "#{@percent}%"
rpath = source+ resized_image
if not File.exists? rpath
# Actually save the image
image.format "jpg"
image.quality "92"
image.write rpath
# This is the tricky part
# we should register the new created file
# since Jekyll already indexed all files before
context.registers[:site].static_files << StaticFile.new(
context.registers[:site], source, File.dirname(@path.sub(%r{^/}, '')),
"resized_#{File.basename(@path)}")
print "image saved to #{rpath}\n"
end
#...#