Skip to content

Instantly share code, notes, and snippets.

@benjaminwood
Created September 10, 2013 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminwood/6511933 to your computer and use it in GitHub Desktop.
Save benjaminwood/6511933 to your computer and use it in GitHub Desktop.
Simple Locomotive CMS image resizing/cashing using (free) proxy service http://images.weserv.nl. Drop this file in your engine's config/initializers directory, restart your rails server and use the filter like this: {{ image_path | scaled_image_url: 'widthxheight' }} Example: {{ post.image.url | scaled_image_url: '1096x616' }}
module Locomotive
module Liquid
module Filters
module Text
# Create a url that will result in the given image
# being scaled and compressed by the specified amount
#
# For more info see: http://images.weserv.nl/
#
# url: The original image url, with or without protocol
# options["w"]: Desired Width
# options["h"]: Desired Height
# options["q"]: Desired JPG image quality (0-100)
# options["t"]: Type of scaling [fit|absolute|square]
#
def scaled_image_url(url, size)
options = {}
options[:w] ||= size.split("x").first
options[:h] ||= size.split("x").last
options[:q] ||= 80
options[:url] = url.to_s.strip.sub(/^https?:\/\//i, "")
"http://images.weserv.nl/?" + options.to_query
end
end
::Liquid::Template.register_filter(Text)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment