module CloudinaryHelper

Constants

CLOUDINARY_JS_CONFIG_PARAMS
CL_BLANK
DEFAULT_POSTER_OPTIONS
DEFAULT_SOURCES
DEFAULT_SOURCE_TYPES
DEFAULT_VIDEO_OPTIONS

Public Class Methods

included(base) click to toggle source
# File lib/cloudinary/helper.rb, line 303
def self.included(base)
  ActionView::Helpers::FormBuilder.send(:include, Cloudinary::FormBuilder)
  base.class_eval do
    unless method_defined?(:image_tag)
      include ActionView::Helpers::AssetTagHelper
    end
    alias_method :image_tag_without_cloudinary, :image_tag unless public_method_defined? :image_tag_without_cloudinary
    alias_method :image_path_without_cloudinary, :image_path unless public_method_defined? :image_path_without_cloudinary
    if Cloudinary.config.enhance_image_tag
      alias_method :image_tag, :image_tag_with_cloudinary
      alias_method :image_path, :image_path_with_cloudinary
    end
  end
end

Public Instance Methods

cl_blank() click to toggle source
# File lib/cloudinary/helper.rb, line 110
def cl_blank
  CL_BLANK
end
cl_client_hints_meta_tag() click to toggle source
# File lib/cloudinary/helper.rb, line 232
def cl_client_hints_meta_tag
  tag "meta", "http-equiv" => "Accept-CH", :content => "DPR, Viewport-Width, Width"
end
cl_download_archive_url(options = {}) click to toggle source

@see {Cloudinary::Utils.download_archive_url}

# File lib/cloudinary/helper.rb, line 290
def cl_download_archive_url(options = {})
  Cloudinary::Utils.download_archive_url(options)
end
cl_download_zip_url(options = {}) click to toggle source

@see {Cloudinary::Utils.download_zip_url}

# File lib/cloudinary/helper.rb, line 295
def cl_download_zip_url(options = {})
  Cloudinary::Utils.download_zip_url(options)
end
cl_form_tag(callback_url, options={}, &block) click to toggle source

cl_form_tag was originally contributed by Milovan Zogovic

# File lib/cloudinary/helper.rb, line 196
def cl_form_tag(callback_url, options={}, &block)
  form_options = options.delete(:form) || {}
  form_options[:method] = :post
  form_options[:multipart] = true

  params = Cloudinary::Uploader.build_upload_params(options.merge(:callback=>callback_url))
  params[:signature] = Cloudinary::Utils.api_sign_request(params, Cloudinary.config.api_secret)
  params[:api_key] = Cloudinary.config.api_key

  api_url = Cloudinary::Utils.cloudinary_api_url("upload",
              {:resource_type => options.delete(:resource_type), :upload_prefix => options.delete(:upload_prefix)})

  form_tag(api_url, form_options) do
    content = []

    params.each do |name, value|
      content << hidden_field_tag(name, value, :id => nil) if value.present?
    end

    content << capture(&block)

    content.join("\n").html_safe
  end
end
cl_image_path(source, options = {}) click to toggle source

Works similarly to cl_image_tag, however just generates the URL of the image

# File lib/cloudinary/helper.rb, line 115
def cl_image_path(source, options = {})
  options = options.clone
  url = cloudinary_url_internal(source, options)
  image_path_without_cloudinary(url)
end
Also aliased as: cl_path
cl_image_tag(source, options = {}) click to toggle source

Stand-in for Rails image_tag helper that accepts various options for transformations.

source

the public ID, possibly with a file type extension. If there is no extension, the :format option is expected to indicate what the extension is. This value can contain the version, or not.

options

Options you would normally pass to image_tag as well as Cloudinary-specific options to control the transformation. Depending on what options are provided, the generated URL may or may not have Cloudinary-specific details in it. For example, if you only specify :width and :height, these values will not be sent to Cloudinary, however if you also specify :crop, they will be.

Examples

# Image tag sized by the browser, not Cloudinary
cl_image_tag "sample.png", :width=>100, :height=>100, :alt=>"hello" # W/H are not sent to Cloudinary

# Image tag sized by Cloudinary using the :fit crop strategy
cl_image_tag "sample.png", :width=>100, :height=>100, :alt=>"hello", :crop=>:fit # W/H are sent to Cloudinary

Get a url for the image with the public id "sample", in :png format.
cl_image_tag "sample", format: :png

See documentation for more details and options: cloudinary.com/documentation/rails_image_manipulation

# File lib/cloudinary/helper.rb, line 33
def cl_image_tag(source, options = {})
  cloudinary_tag source, options do |source, options|
    if source
      image_tag_without_cloudinary(source, options)
    else
      tag 'img', options
    end
  end

end
cl_image_upload(object_name, method, options={}) click to toggle source
# File lib/cloudinary/helper.rb, line 239
def cl_image_upload(object_name, method, options={})
  cl_image_upload_tag("#{object_name}[#{method}]", options)
end
Also aliased as: cl_upload
cl_image_upload_tag(field, options={}) click to toggle source
# File lib/cloudinary/helper.rb, line 262
def cl_image_upload_tag(field, options={})
  html_options = options.delete(:html) || {}
  if options.delete(:multiple)
    html_options[:multiple] = true
    field = "#{ field }[]" unless field.to_s[-2..-1] == "[]"
  end

  tag_options = html_options.merge(:type=>"file", :name=>"file",
    :"data-url"=>cl_upload_url(options),
    :"data-form-data"=>cl_upload_tag_params(options),
    :"data-cloudinary-field"=>field,
    :"data-max-chunk-size"=>options[:chunk_size],
    :"class" => [html_options[:class], "cloudinary-fileupload"].flatten.compact
  ).reject{|k,v| v.blank?}
  tag("input", tag_options)
end
Also aliased as: cl_upload_tag
cl_path(source, options = {})
Alias for: cl_image_path
cl_picture_tag(source, options = {}, sources =[]) click to toggle source
# File lib/cloudinary/helper.rb, line 45
def cl_picture_tag(source, options = {}, sources =[])

  options = options.clone
  content_tag 'picture' do
    sources.map do |source_def|
      source_options = options.clone
      source_options = Cloudinary::Utils.chain_transformation(source_options, source_def[:transformation])
      source_options[:media] = source_def
      cl_source_tag(source, source_options)
    end.push(cl_image_tag(source, options))
        .join('')
        .html_safe
  end
end
cl_private_download_url(public_id, format, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 285
def cl_private_download_url(public_id, format, options = {})
  Cloudinary::Utils.private_download_url(public_id, format, options)
end
cl_signed_download_url(public_id, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 299
def cl_signed_download_url(public_id, options = {})
  Cloudinary::Utils.cloudinary_url(public_id, options)
end
cl_source_tag(source, options) click to toggle source
# File lib/cloudinary/helper.rb, line 60
def cl_source_tag(source, options)
  srcset_param = options.fetch(:srcset, {}).merge(Cloudinary.config.srcset || {})
  attributes = options.fetch(:attributes, {}).clone
  responsive_attributes = generate_image_responsive_attributes(source, attributes, srcset_param, options)
  attributes = attributes.merge(responsive_attributes)
  unless attributes.has_key? :srcset
    attributes[:srcset] = Cloudinary::Utils.cloudinary_url(source, options)
  end
  media_attr = generate_media_attribute(options[:media])
  attributes[:media] = media_attr unless media_attr.empty?
  tag "source", attributes, true
end
cl_sprite_tag(source, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 191
def cl_sprite_tag(source, options = {})
  stylesheet_link_tag(cl_sprite_url(source, options))
end
cl_sprite_url(source, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 176
def cl_sprite_url(source, options = {})
  options = options.clone

  version_store = options.delete(:version_store)
  if options[:version].blank? && (version_store == :file) && defined?(Rails) && defined?(Rails.root)
    file_name = "#{Rails.root}/tmp/cloudinary/cloudinary_sprite_#{source.sub(/\..*/, '')}.version"
    if File.exists?(file_name)
      options[:version] = File.read(file_name).chomp
    end
  end

  options[:format] = "css" unless source.ends_with?(".css")
  cloudinary_url_internal(source, options.merge(:type=>:sprite))
end
cl_unsigned_image_upload(object_name, method, upload_preset, options={}) click to toggle source
# File lib/cloudinary/helper.rb, line 243
def cl_unsigned_image_upload(object_name, method, upload_preset, options={})
  cl_unsigned_image_upload_tag("#{object_name}[#{method}]", upload_preset, options)
end
Also aliased as: cl_unsigned_upload
cl_unsigned_image_upload_tag(field, upload_preset, options={}) click to toggle source
# File lib/cloudinary/helper.rb, line 280
def cl_unsigned_image_upload_tag(field, upload_preset, options={})
  cl_image_upload_tag(field, options.merge(:unsigned => true, :upload_preset => upload_preset))
end
Also aliased as: cl_unsigned_upload_tag
cl_unsigned_upload(object_name, method, upload_preset, options={})
cl_unsigned_upload_tag(field, upload_preset, options={})
cl_upload(object_name, method, options={})
Alias for: cl_image_upload
cl_upload_tag(field, options={})
Alias for: cl_image_upload_tag
cl_upload_tag_params(options={}) click to toggle source
# File lib/cloudinary/helper.rb, line 252
def cl_upload_tag_params(options={})
  cloudinary_params = Cloudinary::Uploader.build_upload_params(options)
  cloudinary_params[:callback] = build_callback_url(options)
  if options[:unsigned]
    return cloudinary_params.reject{|k, v| Cloudinary::Utils.safe_blank?(v)}.to_json
  else
    return Cloudinary::Utils.sign_request(cloudinary_params, options).to_json
  end
end
cl_upload_url(options={}) click to toggle source
# File lib/cloudinary/helper.rb, line 248
def cl_upload_url(options={})
  Cloudinary::Utils.cloudinary_api_url("upload", {:resource_type=>:auto}.merge(options))
end
cl_video_path(source, options={}) click to toggle source

Returns a url for the given source with options

# File lib/cloudinary/video_helper.rb, line 93
def cl_video_path(source, options={})
  cl_image_path(source, DEFAULT_VIDEO_OPTIONS.merge(options))
end
cl_video_tag(source, options = {}, &block) click to toggle source

Creates an HTML video tag for the provided source

Options

  • :source_types - Specify which source type the tag should include. defaults to webm, mp4 and ogv.

  • :source_transformation - specific transformations to use for a specific source type.

  • :sources - list of sources (overrides :source_types when present)

  • :poster - override default thumbnail:

    • url: provide an ad hoc url

    • options: with specific poster transformations and/or Cloudinary :public_id

Examples

cl_video_tag("mymovie.mp4")
cl_video_tag("mymovie.mp4", :source_types => :webm)
cl_video_tag("mymovie.ogv", :poster => "myspecialplaceholder.jpg")
cl_video_tag("mymovie.webm", :source_types => [:webm, :mp4], :poster => {:effect => 'sepia'}) do
  content_tag( :span, "Cannot present video!")
end
cl_video_tag("mymovie", :sources => [
  {
    :type => "mp4",
    :codecs => "hev1",
    :transformations => { :video_codec => "h265" }
  },
  {
    :type => "webm",
    :transformations => { :video_codec => "auto" }
  }
])
# File lib/cloudinary/video_helper.rb, line 55
def cl_video_tag(source, options = {}, &block)
  source = strip_known_ext(source) unless Cloudinary::Utils.config_option_fetch(options, :use_fetch_format)
  video_attributes = [:autoplay,:controls,:loop,:muted,:poster, :preload]
  options = Cloudinary::Utils.deep_symbolize_keys(DEFAULT_VIDEO_OPTIONS.merge(options))

  options[:source_types] ||= DEFAULT_SOURCE_TYPES
  video_attributes.keep_if{ |key, _| options.has_key?(key)} # required prior to Rails 4.x
  video_options = options.extract!(*video_attributes)
  if video_options.has_key? :poster
    poster = video_options.delete(:poster)
    case poster
    when String
      video_options[:poster] = poster
    when Hash
      if poster.has_key? :public_id
        poster[:resource_type] = "image"
        poster_name            = poster[:public_id]
        video_options[:poster] = cl_image_path(poster_name, poster)
      else
        video_options[:poster] = cl_video_thumbnail_path(source, poster)
      end
    else
      # no poster
    end
  else
    video_options[:poster] = cl_video_thumbnail_path(source, options)
  end

  fallback = (capture(&block) if block_given?) || options.delete(:fallback_content)

  if options[:sources]
    video_tag_from_sources(source, options, video_options, fallback)
  else
    video_tag_from_source_types(source, options, video_options, fallback)
  end
end
cl_video_thumbnail_path(source, options={}) click to toggle source

Returns a url for the thumbnail for the given video source and options

# File lib/cloudinary/video_helper.rb, line 103
def cl_video_thumbnail_path(source, options={})
  cl_image_path(source, DEFAULT_POSTER_OPTIONS.merge(options))
end
cl_video_thumbnail_tag(source, options={}) click to toggle source

Returns an HTML img tag with the thumbnail for the given video source and options

# File lib/cloudinary/video_helper.rb, line 98
def cl_video_thumbnail_tag(source, options={})
  cl_image_tag(source, DEFAULT_POSTER_OPTIONS.merge(options))
end
cloudinary_js_config() click to toggle source
# File lib/cloudinary/helper.rb, line 222
def cloudinary_js_config
  params = {}
  CLOUDINARY_JS_CONFIG_PARAMS.each do
    |param|
    value = Cloudinary.config.send(param)
    params[param] = value if !value.nil?
  end
  content_tag("script", "$.cloudinary.config(#{params.to_json});".html_safe, :type=>"text/javascript")
end
cloudinary_tag(source, options = {}) { |src,tag_options| ... } click to toggle source
# File lib/cloudinary/helper.rb, line 74
def cloudinary_tag(source, options = {})
  tag_options = options.clone
  tag_options[:width] = tag_options.delete(:html_width) if tag_options.include?(:html_width)
  tag_options[:height] = tag_options.delete(:html_height) if tag_options.include?(:html_height)
  tag_options[:size] = tag_options.delete(:html_size) if tag_options.include?(:html_size)
  tag_options[:border] = tag_options.delete(:html_border) if tag_options.include?(:html_border)
  srcset_param = Cloudinary::Utils.config_option_consume(tag_options, :srcset, {})
  src = cloudinary_url_internal(source, tag_options)
  attributes = tag_options.delete(:attributes) || {}

  responsive_placeholder = Cloudinary::Utils.config_option_consume(tag_options, :responsive_placeholder)
  client_hints = Cloudinary::Utils.config_option_consume(tag_options, :client_hints)

  hidpi = tag_options.delete(:hidpi)
  responsive = tag_options.delete(:responsive)
  if !client_hints && (hidpi || responsive)
    tag_options["data-src"] = src
    src = nil
    extra_class = responsive ? "cld-responsive" : "cld-hidpi"
    tag_options[:class] = [tag_options[:class], extra_class].compact.join(" ")
    responsive_placeholder = CL_BLANK if responsive_placeholder == "blank"
    tag_options[:src] = responsive_placeholder
  end
  responsive_attrs = generate_image_responsive_attributes(source, attributes, srcset_param, options)
  unless  responsive_attrs.empty?
    tag_options.delete(:width)
    tag_options.delete(:height)
    tag_options.merge! responsive_attrs
  end
  if block_given?
    yield(src,tag_options)
  else
    tag('div', tag_options)
  end
end
cloudinary_url(source, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 235
def cloudinary_url(source, options = {})
  cloudinary_url_internal(source, options.clone)
end
cloudinary_url_internal(source, options = {}) click to toggle source
# File lib/active_storage/service/cloudinary_service.rb, line 19
def cloudinary_url_internal(source, options = {})
  source = ActiveStorage::Blob.service.public_id(source) if defined? ActiveStorage::Blob.service.public_id
  cloudinary_url_internal_original(source, options)
end
cloudinary_url_internal_original(source, options = {})
facebook_profile_image_path(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 140
def facebook_profile_image_path(profile, options = {})
  cl_image_path(profile, {:type=>:facebook}.merge(options))
end
facebook_profile_image_tag(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 136
def facebook_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:facebook}.merge(options))
end
fetch_image_tag(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 132
def fetch_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:fetch}.merge(options))
end
gplus_profile_image_path(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 172
def gplus_profile_image_path(profile, options = {})
  cl_image_path(profile, {:type=>:gplus}.merge(options))
end
gplus_profile_image_tag(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 168
def gplus_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:gplus}.merge(options))
end
gravatar_profile_image_path(email, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 148
def gravatar_profile_image_path(email, options = {})
  cl_image_path(Digest::MD5.hexdigest(email.strip.downcase), {:type=>:gravatar, :format=>:jpg}.merge(options))
end
gravatar_profile_image_tag(email, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 144
def gravatar_profile_image_tag(email, options = {})
  cl_image_tag(Digest::MD5.hexdigest(email.strip.downcase), {:type=>:gravatar, :format=>:jpg}.merge(options))
end
image_path_with_cloudinary(*args) click to toggle source
# File lib/cloudinary/helper.rb, line 127
def image_path_with_cloudinary(*args)
  source, options = args
  cl_image_path(source, {:type=>:asset}.merge(options || {}))
end
image_tag_with_cloudinary(*args) click to toggle source
# File lib/cloudinary/helper.rb, line 122
def image_tag_with_cloudinary(*args)
  source, options = args
  cl_image_tag(source, {:type=>:asset}.merge(options || {}))
end
twitter_name_profile_image_path(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 164
def twitter_name_profile_image_path(profile, options = {})
  cl_image_path(profile, {:type=>:twitter_name}.merge(options))
end
twitter_name_profile_image_tag(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 160
def twitter_name_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:twitter_name}.merge(options))
end
twitter_profile_image_path(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 156
def twitter_profile_image_path(profile, options = {})
  cl_image_path(profile, {:type=>:twitter}.merge(options))
end
twitter_profile_image_tag(profile, options = {}) click to toggle source
# File lib/cloudinary/helper.rb, line 152
def twitter_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:twitter}.merge(options))
end

Protected Instance Methods

strip_known_ext(name) click to toggle source
# File lib/cloudinary/video_helper.rb, line 109
def strip_known_ext(name)
  name.sub(/\.(#{DEFAULT_SOURCE_TYPES.join("|")})$/, '')
end

Private Instance Methods

build_callback_url(options) click to toggle source
# File lib/cloudinary/helper.rb, line 334
def build_callback_url(options)
  callback_path = options.delete(:callback_cors) || Cloudinary.config.callback_cors || "/cloudinary_cors.html"
  if callback_path.match(/^https?:\/\//)
    callback_path
  else
    callback_url = request.scheme + "://"
    callback_url << request.host
    if request.scheme == "https" && request.port != 443 ||
      request.scheme == "http" && request.port != 80
      callback_url << ":#{request.port}"
    end
    callback_url << callback_path
  end
end
generate_tag_from_sources(params) click to toggle source
# File lib/cloudinary/video_helper.rb, line 151
def generate_tag_from_sources(params)
  source_name, sources, options, video_options, fallback = params.values_at(:source_name, :sources, :options, :video_options, :fallback)

  cloudinary_tag(source_name, options) do |_source, tag_options|
    content_tag('video', tag_options.merge(video_options)) do
      source_tags = sources.map do |source|
        type = source[:type]
        options[:format] = type
        transformation = source[:transformations] || {}
        cloudinary_tag(source_name, options.merge(transformation)) do |url, _tag_options|
          mime_type = "video/#{(type == 'ogv' ? 'ogg' : type)}"
          if source[:codecs]
            codecs = source[:codecs].is_a?(Array) ? source[:codecs].join(", ") : source[:codecs]
            mime_type = "#{mime_type}; codecs=#{codecs}"
          end
          tag("source", :src => url, :type => mime_type)
        end
      end
      source_tags.push(fallback.html_safe) unless fallback.blank?
      safe_join(source_tags)
    end
  end
end
video_tag_from_source_types(source_name, options, video_options, fallback) click to toggle source
# File lib/cloudinary/video_helper.rb, line 115
def video_tag_from_source_types(source_name, options, video_options, fallback)
  source_transformation = options.delete(:source_transformation) || {}
  source_types = Array(options.delete(:source_types))

  if source_types.size > 1
    sources = source_types.map do |type|
      {
        :type => type,
        :transformations => source_transformation[type.to_sym] || {}
      }
    end

    generate_tag_from_sources(:source_name => source_name,
                              :sources => sources,
                              :options => options,
                              :video_options => video_options,
                              :fallback => fallback)
  else
    transformation      = source_transformation[source_types.first.to_sym] || {}
    video_options[:src] = cl_video_path("#{source_name}.#{source_types.first.to_sym}", transformation.merge(options))
    cloudinary_tag(source_name, options) do |_source, tag_options|
      content_tag('video', fallback, tag_options.merge(video_options))
    end
  end
end
video_tag_from_sources(source_name, options, video_options, fallback) click to toggle source
# File lib/cloudinary/video_helper.rb, line 141
def video_tag_from_sources(source_name, options, video_options, fallback)
  sources = options.delete(:sources)

  generate_tag_from_sources(:source_name => source_name,
                            :sources => sources,
                            :options => options,
                            :video_options => video_options,
                            :fallback => fallback)
end