Module: CloudinaryHelper

Includes:
ActionView::Helpers::CaptureHelper
Defined in:
lib/cloudinary/helper.rb,
lib/cloudinary/video_helper.rb

Constant Summary collapse

CL_BLANK =
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
CLOUDINARY_JS_CONFIG_PARAMS =
[:api_key, :cloud_name, :private_cdn, :secure_distribution, :cdn_subdomain]
DEFAULT_POSTER_OPTIONS =
{ :format => 'jpg', :resource_type => 'video' }
DEFAULT_SOURCE_TYPES =
%w(webm mp4 ogv)
DEFAULT_VIDEO_OPTIONS =
{ :resource_type => 'video' }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/cloudinary/helper.rb', line 268

def self.included(base)
  ActionView::Helpers::FormBuilder.send(:include, Cloudinary::FormBuilder)
  base.class_eval do
    if !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

Instance Method Details

#cl_blankObject



68
69
70
# File 'lib/cloudinary/helper.rb', line 68

def cl_blank
  CL_BLANK
end

#cl_client_hints_meta_tagObject



190
191
192
# File 'lib/cloudinary/helper.rb', line 190

def cl_client_hints_meta_tag
  tag "meta", "http-equiv" => "Accept-CH", :content => "DPR, Viewport-Width, Width"
end

#cl_download_archive_url(options = {}) ⇒ Object

See Also:

  • CloudinaryHelper.{Cloudinary{Cloudinary::Utils{Cloudinary::Utils.download_archive_url}


255
256
257
# File 'lib/cloudinary/helper.rb', line 255

def cl_download_archive_url(options = {})
  Cloudinary::Utils.download_archive_url(options)
end

#cl_download_zip_url(options = {}) ⇒ Object

See Also:

  • CloudinaryHelper.{Cloudinary{Cloudinary::Utils{Cloudinary::Utils.download_zip_url}


260
261
262
# File 'lib/cloudinary/helper.rb', line 260

def cl_download_zip_url(options = {})
  Cloudinary::Utils.download_zip_url(options)
end

#cl_form_tag(callback_url, options = {}, &block) ⇒ Object

cl_form_tag was originally contributed by Milovan Zogovic



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cloudinary/helper.rb', line 154

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 = {}) ⇒ Object Also known as: cl_path

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



73
74
75
76
77
# File 'lib/cloudinary/helper.rb', line 73

def cl_image_path(source, options = {})
  options = options.clone
  url = cloudinary_url_internal(source, options)
  image_path_without_cloudinary(url)
end

#cl_image_tag(source, options = {}) ⇒ Object

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



29
30
31
32
33
34
35
36
37
38
# File 'lib/cloudinary/helper.rb', line 29

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 = {}) ⇒ Object Also known as: cl_upload



197
198
199
# File 'lib/cloudinary/helper.rb', line 197

def cl_image_upload(object_name, method, options={})
  cl_image_upload_tag("#{object_name}[#{method}]", options)
end

#cl_image_upload_tag(field, options = {}) ⇒ Object Also known as: cl_upload_tag



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cloudinary/helper.rb', line 220

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

#cl_private_download_url(public_id, format, options = {}) ⇒ Object



243
244
245
# File 'lib/cloudinary/helper.rb', line 243

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 = {}) ⇒ Object



264
265
266
# File 'lib/cloudinary/helper.rb', line 264

def cl_signed_download_url(public_id, options = {})
  Cloudinary::Utils.signed_download_url(public_id, options)
end

#cl_sprite_tag(source, options = {}) ⇒ Object



149
150
151
# File 'lib/cloudinary/helper.rb', line 149

def cl_sprite_tag(source, options = {})
  stylesheet_link_tag(cl_sprite_url(source, options))
end

#cl_sprite_url(source, options = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cloudinary/helper.rb', line 134

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 = {}) ⇒ Object Also known as: cl_unsigned_upload



201
202
203
# File 'lib/cloudinary/helper.rb', line 201

def cl_unsigned_image_upload(object_name, method, upload_preset, options={})
  cl_unsigned_image_upload_tag("#{object_name}[#{method}]", upload_preset, options)
end

#cl_unsigned_image_upload_tag(field, upload_preset, options = {}) ⇒ Object Also known as: cl_unsigned_upload_tag



238
239
240
# File 'lib/cloudinary/helper.rb', line 238

def cl_unsigned_image_upload_tag(field, upload_preset, options={})
  cl_image_upload_tag(field, options.merge(:unsigned => true, :upload_preset => upload_preset))
end

#cl_upload_tag_params(options = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/cloudinary/helper.rb', line 210

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 = {}) ⇒ Object



206
207
208
# File 'lib/cloudinary/helper.rb', line 206

def cl_upload_url(options={})
  Cloudinary::Utils.cloudinary_api_url("upload", {:resource_type=>:auto}.merge(options))
end

#cl_video_path(source, options = {}) ⇒ Object

Returns a url for the given source with options



79
80
81
# File 'lib/cloudinary/video_helper.rb', line 79

def cl_video_path(source, options={})
  cl_image_path(source, DEFAULT_VIDEO_OPTIONS.merge(options))
end

#cl_video_tag(source, options = {}, &block) ⇒ Object

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.

  • :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
  ( :span, "Cannot present video!")
end


23
24
25
26
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudinary/video_helper.rb', line 23

def cl_video_tag(source, options = {}, &block)
  source = strip_known_ext(source)
  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

  source_transformation = options.delete(:source_transformation) || {}
  source_types = Array(options.delete(:source_types))
  fallback     = (capture(&block) if block_given?) || options.delete(:fallback_content)

  if source_types.size > 1
    cloudinary_tag(source, options) do |_source, tag_options|
      ('video', tag_options.merge(video_options)) do
        source_tags = source_types.map do |type|
          transformation = source_transformation[type.to_sym] || {}
          cloudinary_tag("#{source}.#{type}", options.merge(transformation)) do |url, _tag_options|
            mime_type = "video/#{(type == 'ogv' ? 'ogg' : type)}"
            tag("source", :src => url, :type => mime_type)
          end
        end
        source_tags.push(fallback.html_safe) unless fallback.blank?
        safe_join(source_tags)
      end
    end
  else
    transformation      = source_transformation[source_types.first.to_sym] || {}
    video_options[:src] = cl_video_path("#{source}.#{source_types.first.to_sym}", transformation.merge(options))
    cloudinary_tag(source, options) do |_source, tag_options|
      ('video', fallback, tag_options.merge(video_options))
    end
  end
end

#cl_video_thumbnail_path(source, options = {}) ⇒ Object

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



89
90
91
# File 'lib/cloudinary/video_helper.rb', line 89

def cl_video_thumbnail_path(source, options={})
  cl_image_path(source, DEFAULT_POSTER_OPTIONS.merge(options))
end

#cl_video_thumbnail_tag(source, options = {}) ⇒ Object

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



84
85
86
# File 'lib/cloudinary/video_helper.rb', line 84

def cl_video_thumbnail_tag(source, options={})
  cl_image_tag(source, DEFAULT_POSTER_OPTIONS.merge(options))
end

#cl_zip_download_url(tag, options = {}) ⇒ Object

Deprecated.

Helper method that uses the deprecated ZIP download API. Replaced by cl_download_zip_url that uses the more advanced and robust archive generation and download API



250
251
252
# File 'lib/cloudinary/helper.rb', line 250

def cl_zip_download_url(tag, options = {})
  Cloudinary::Utils.zip_download_url(tag, options)
end

#cloudinary_js_configObject



180
181
182
183
184
185
186
187
188
# File 'lib/cloudinary/helper.rb', line 180

def cloudinary_js_config
  params = {}
  CLOUDINARY_JS_CONFIG_PARAMS.each do
    |param|
    value = Cloudinary.config.send(param)
    params[param] = value if !value.nil?
  end
  ("script", "$.cloudinary.config(#{params.to_json});".html_safe, :type=>"text/javascript")
end

#cloudinary_tag(source, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloudinary/helper.rb', line 40

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)
  source = cloudinary_url_internal(source, tag_options)

  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"] = source
    source = 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
  if block_given?
    yield(source,tag_options)
  else
    tag('div', tag_options)
  end
end

#cloudinary_url(source, options = {}) ⇒ Object



193
194
195
# File 'lib/cloudinary/helper.rb', line 193

def cloudinary_url(source, options = {})
  cloudinary_url_internal(source, options.clone)
end

#facebook_profile_image_path(profile, options = {}) ⇒ Object



98
99
100
# File 'lib/cloudinary/helper.rb', line 98

def facebook_profile_image_path(profile, options = {})
  cl_image_path(profile, {:type=>:facebook}.merge(options))
end

#facebook_profile_image_tag(profile, options = {}) ⇒ Object



94
95
96
# File 'lib/cloudinary/helper.rb', line 94

def facebook_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:facebook}.merge(options))
end

#fetch_image_tag(profile, options = {}) ⇒ Object



90
91
92
# File 'lib/cloudinary/helper.rb', line 90

def fetch_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:fetch}.merge(options))
end

#gplus_profile_image_path(profile, options = {}) ⇒ Object



130
131
132
# File 'lib/cloudinary/helper.rb', line 130

def gplus_profile_image_path(profile, options = {})
  cl_image_path(profile, {:type=>:gplus}.merge(options))
end

#gplus_profile_image_tag(profile, options = {}) ⇒ Object



126
127
128
# File 'lib/cloudinary/helper.rb', line 126

def gplus_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:gplus}.merge(options))
end

#gravatar_profile_image_path(email, options = {}) ⇒ Object



106
107
108
# File 'lib/cloudinary/helper.rb', line 106

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 = {}) ⇒ Object



102
103
104
# File 'lib/cloudinary/helper.rb', line 102

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) ⇒ Object



85
86
87
88
# File 'lib/cloudinary/helper.rb', line 85

def image_path_with_cloudinary(*args)
  source, options = args
  cl_image_path(source, {:type=>:asset}.merge(options || {}))
end

#image_tag_with_cloudinary(*args) ⇒ Object



80
81
82
83
# File 'lib/cloudinary/helper.rb', line 80

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 = {}) ⇒ Object



122
123
124
# File 'lib/cloudinary/helper.rb', line 122

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 = {}) ⇒ Object



118
119
120
# File 'lib/cloudinary/helper.rb', line 118

def twitter_name_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:twitter_name}.merge(options))
end

#twitter_profile_image_path(profile, options = {}) ⇒ Object



114
115
116
# File 'lib/cloudinary/helper.rb', line 114

def twitter_profile_image_path(profile, options = {})
  cl_image_path(profile, {:type=>:twitter}.merge(options))
end

#twitter_profile_image_tag(profile, options = {}) ⇒ Object



110
111
112
# File 'lib/cloudinary/helper.rb', line 110

def twitter_profile_image_tag(profile, options = {})
  cl_image_tag(profile, {:type=>:twitter}.merge(options))
end