Created
September 10, 2013 16:27
-
-
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' }}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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