How can I use Cloudinary to output a url with transformation as a string?

牧云@^-^@ 提交于 2019-12-07 07:54:54

问题


I'll preface this by saying that may be approaching this incorrectly. What I'm trying to do is pass the url w/transformation into JS using a data- attribute.

Currently, I'm using the following to generate the image tag:

= cl_image_tag(image.asset.filename.to_s, transformation: "scroller", :"data-medium" => image.asset.filename.to_s)

Which produces this:

<img src="http://res.cloudinary.com/bucket/image/upload/t_scroller/v1373070863/s1ufy3nygii85ytoeent.jpg" data-medium="s1ufy3nygii85ytoeent.jpg">

What I'd like to be able to do is have it output this (Utilizing the t_medium named transition I've set up):

<img src="http://res.cloudinary.com/bucket/image/upload/t_scroller/v1373070863/s1ufy3nygii85ytoeent.jpg" data-medium="http://res.cloudinary.com/bucket/image/upload/t_medium/v1373070863/s1ufy3nygii85ytoeent.jpg">

Currently the cl_image_tag is doing the heavy lifting by generating an image tag with the correctly configured URL. This is great, however I can't seem to find any documentation on how to output a configured URL as a string without the image tag (to use as the data-medium attribute). I could manually configure the URL, but I was wondering if there was a better way?


回答1:


You can use the cloudinary_url helper to generate the URL without the image tag. For example:

cloudinary_url(image.asset.filename.to_s, transformation: "medium")

As zeantsoi said, if you are using CarrierWave, you can also pass the uploader itself as a parameter:

cloudinary_url(image.asset, transformation: "medium")



回答2:


On top of Tal Lev-Ami's answer:

If you need to call cloudinary_url outside of a view (for instance in a serializer model for an api), you have 2 options:

  • rely on "helper": helper.cloudinary_url
  • or use Cloudinary::Utils.cloudinary_url


来源:https://stackoverflow.com/questions/17515075/how-can-i-use-cloudinary-to-output-a-url-with-transformation-as-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!