Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 810
Eric Norcross Avatar asked Dec 11 '22 13:12

Eric Norcross


2 Answers

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")
like image 64
Tal Lev-Ami Avatar answered Jan 22 '23 08:01

Tal Lev-Ami


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
like image 44
Arnaud Avatar answered Jan 22 '23 08:01

Arnaud