Module: CloudflareImageResizing::Helper
- Defined in:
- app/helpers/cloudflare_image_resizing/helper.rb
Constant Summary collapse
- RESIZABLE_CONTENT_TYPES =
%w[image/jpeg image/gif image/png image/webp].freeze
Instance Method Summary collapse
-
#resizable?(image) ⇒ Boolean
Is the image resizable with Cloudflare Image Resizing? developers.cloudflare.com/images/image-resizing/format-limitations/ If we try and it isn’t, we get a 415 Someday (supposedly) setting onerror=redirect will make this unnecessary.
-
#resized_image(image, options) ⇒ Object
Helper to use Cloudflare Image Resizing developers.cloudflare.com/images/image-resizing/url-format/ Example usage: <%= image_tag resized_image(@user.avatar, width: 100, fit: “crop”), alt: “Cropped avatar” %>.
-
#resized_image_tag(image, options = {}) ⇒ Object
Helper to use resized_image and also build an image_tag Automatically adds a srcset with the same image in 2x and 3x (unless a srcset is provided.) See: developers.cloudflare.com/images/image-resizing/responsive-images/#srcset-for-high-dpi-displays Example usage: <%= resized_image_tag @user.avatar, resize: 100, fit: “crop”, alt: “Cropped avatar” %>.
Instance Method Details
#resizable?(image) ⇒ Boolean
Is the image resizable with Cloudflare Image Resizing? developers.cloudflare.com/images/image-resizing/format-limitations/ If we try and it isn’t, we get a 415 Someday (supposedly) setting onerror=redirect will make this unnecessary.
58 59 60 61 62 63 64 65 66 67 |
# File 'app/helpers/cloudflare_image_resizing/helper.rb', line 58 def resizable?(image) if image.is_a?(ActiveStorage::Attachment) || image.is_a?(ActiveStorage::Attached) image.content_type.in?(RESIZABLE_CONTENT_TYPES) elsif image.is_a?(String) extension = File.extname(image)[1..] Mime::Type.lookup_by_extension(extension).to_s.in?(RESIZABLE_CONTENT_TYPES) else false end end |
#resized_image(image, options) ⇒ Object
Helper to use Cloudflare Image Resizing developers.cloudflare.com/images/image-resizing/url-format/ Example usage: <%= image_tag resized_image(@user.avatar, width: 100, fit: “crop”), alt: “Cropped avatar” %>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/helpers/cloudflare_image_resizing/helper.rb', line 27 def resized_image(image, ) preload = .delete(:preload) path = if image.is_a?(ActiveStorage::Attachment) || image.is_a?(ActiveStorage::Attached) || image.is_a?(ActionText::Attachment) rails_public_blob_url(image) else image_path(image) end if resizable?(image) if ::Rails.application.config.cloudflare_image_resizing.enabled path = "/" + path unless path.starts_with?("/") # Direct R2 URLs don't have a leading / path = "/cdn-cgi/image/" + .to_param.tr("&", ",") + path end if preload # Allow us to add a <link rel="preload"> to the head # to speed up loading resized images. content_for(:cloudflare_image_resizing_preload) do preload_link_tag(path, as: :image) end end end path end |
#resized_image_tag(image, options = {}) ⇒ Object
Helper to use resized_image and also build an image_tag Automatically adds a srcset with the same image in 2x and 3x (unless a srcset is provided.) See: developers.cloudflare.com/images/image-resizing/responsive-images/#srcset-for-high-dpi-displays Example usage: <%= resized_image_tag @user.avatar, resize: 100, fit: “crop”, alt: “Cropped avatar” %>
12 13 14 15 16 17 18 19 20 21 |
# File 'app/helpers/cloudflare_image_resizing/helper.rb', line 12 def resized_image_tag(image, = {}) = .delete(:resize) || {} # note: srcset needs to encode the commas in the URLs (%2C) to avoid confusing it with commas between variants. image_tag( resized_image(image, ), .reverse_merge(srcset: "#{resized_image(image, .merge(dpr: 1)).gsub(",", "%2C")}, #{resized_image(image, .merge(dpr: 2)).gsub(",", "%2C")} 2x, #{resized_image(image, .merge(dpr: 3)).gsub(",", "%2C")} 3x".gsub(/\s+/, " ")) ) end |