Sha256: d6822a17abdb1a9707e0ef227fcfa9d3c94ee650b4d996ad2adad0f247ef6330
Contents?: true
Size: 1.92 KB
Versions: 3
Compression:
Stored size: 1.92 KB
Contents
require "image_size" module Vectory class ImageResize def call(img, path, maxheight, maxwidth) s, realsize = get_image_size(img, path) img.name == "svg" && !img["viewBox"] && s[0] && s[1] and img["viewBox"] = "0 0 #{s[0]} #{s[1]}" s, skip = image_dont_resize(s, realsize) skip and return s s = image_size_fillin(s, realsize) image_shrink(s, maxheight, maxwidth) end def get_image_size(img, path) realsize = ImageSize.path(path).size s = image_size_interpret(img, realsize || [nil, nil]) image_size_zeroes_complete(s, realsize) end private def image_dont_resize(dim, realsize) dim.nil? and return [[nil, nil], true] realsize.nil? and return [dim, true] dim[0] == nil && dim[1] == nil and return [dim, true] [dim, false] end def image_size_fillin(dim, realsize) dim[1] = fill_size(dim[1], dim[0], realsize[1], realsize[0]) dim[0] = fill_size(dim[0], dim[1], realsize[0], realsize[1]) dim end def fill_size(current, another, real_current, real_another) return current unless current.zero? && !another.zero? another * real_current / real_another end def image_shrink(dim, maxheight, maxwidth) dim[1] > maxheight and dim = [(dim[0] * maxheight / dim[1]).ceil, maxheight] dim[0] > maxwidth and dim = [maxwidth, (dim[1] * maxwidth / dim[0]).ceil] dim end def image_size_interpret(img, realsize) w = image_size_percent(img["width"], realsize[0]) h = image_size_percent(img["height"], realsize[1]) [w, h] end def image_size_percent(value, real) /%$/.match?(value) && !real.nil? and value = real * (value.sub(/%$/, "").to_f / 100) value.to_i end def image_size_zeroes_complete(dim, realsize) if dim[0].zero? && dim[1].zero? dim = realsize end [dim, realsize] end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
vectory-0.7.2 | lib/vectory/image_resize.rb |
vectory-0.7.1 | lib/vectory/image_resize.rb |
vectory-0.7.0 | lib/vectory/image_resize.rb |