Cloudinary image transformation parameters not working in Rails app - ruby-on-rails

Here's the code:
= link_to (cl_image_tag(post.image_url, width:640, quality:30, class: "img-responsive")), post_path(post)
As mentioned here, this should give me an image with quality set to 30, but I'm not seeing the change in quality of the images on the site. I've tried different values for quality ranging from 10 to 100 but I'm not seeing even a slight difference. I also tried other parameters, for example, format: "jpg", which is supposed to force convert all non-jpg files to jpg, but it isn't working either. The width param works fine, by the way.

The cl_image_tag method accepts the image's public ID and doesn't support a URL parameter. The image tag you are getting is a fallback, which ignores all Cloudinary's parameters (except width/height which are used for the html tag). Make sure you save the public IDs in your DB. I recommend using Carrierwave, which handles the DB maintenance for you.

Related

Control over format when using RequestImageFileAsync in Blazor WebAssembly

Blazor Web assembly has a convenience method that converts an IBrowserFile containing an image into a resized version - which is handy for resizing large images prior to uploading them.
This method takes a format as a string which determines the format of the output file.
Is there anywhere a list of valid formats that this property will accept? Can you specify the compression or bit depth values on the resulting file?
Currently, If I take an existing .jpg file and convert it using a format string of "jpg" the resulting file, although smaller in pixel dimensions is actually about double the size on disk. A 4000x3000 image at about 2.8MB can be "reduced" to a 2000x1500 image that's 7.7MB in size. Which is obviously not helping when the purpose is to reduce upload size. I could easily upload the 2.8MB file and resize it more efficiently on the server.
var imageFile = await file.RequestImageFileAsync("jpg", 2000, 2000);
This suggests I'm using the method incorrectly - but Microsoft's documentation on this method gives no clues as to what valid "Format" strings might, only stating that it is a string type. I've tried ".jpg", "JPEG", "jpg" - all of which seem to produce the same valid jpg file. What should I be passing here to actually reduce the file size?
See https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
It's actually not "image/jpg" but "image/jpeg". If you specify non-existent format, the fallback (at least for me) seems to be "image/png". That's why you always got a valid image but with the same filesize.
I think this method uses html types:
html types
Try "image/jpg".
Be careful, though, this is a request to the browser, and the browser can send back whatever it wants. I believe this will work fine on all browsers, but you'd better check some of the common culprits (Hi, Opera!) to confirm.

How to make Carrierwave add random string to the file path?

I have this kind of hot fix:
= image_tag "#{current_candidate.profile.photo_url}?stamp=#{SecureRandom.hex}"
The reason for this is that I made a "crop" feature, and when users recrop their photo - old version gets displayed from browser cache.
Anyway.
This photo_url is called in many places, and rather than making a helper & call it everywhere I thought it would be nice to configure uploader to do that.
Is that possible?
Note: filename can't be random. For other reasons.
I would imagine it has something to do with overriding photo_url method.

How to download image from url and display in view

I am trying to download an image and displaying it in a view in rails.
The reason why I want to download it is because the url contains some api-keys which I am not very fond of giving away.
The solution I have tried thus far is the following:
#Model.rb file
def getUrlMethod
someUrlToAPNGfile = "whatever.png"
file = Tempfile.new(['imageprependname', '.png'], :encoding => "ascii-8bit")
file.write(open(data).read)
return "#{Rails.application.config.action_mailer.default_url_options[:host]}#{file.path}"
end
#This seems to be downloading the image just fine. However the url that is returned does not point to a legal place
Under development I get this URL for the picture: localhost:3000/var/folders/18/94qgts592sq_yq45fnthpzxh0000gn/T/imageprependname20130827-97433-10esqxh.png
That image link does not point anywhere useful.
My theories to what might be wrong is:
The tempfile is deleted before the user can request it
The url points to the wrong place
The url is not a legal route in the routes file
A am currently not aware of any way to fix either of these. Any help?
By the way: I do not need to store the picture after I have displayed it, as it will be changing constantly from the source.
I can think of two options:
First, embed the image directly in the HTML documents, see
http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/
http://webcodertools.com/imagetobase64converter
Second, in the HTML documents, write the image tag as usual:
<img src="/remote_images/show/whatever.png" alt="whatever" />
Then you create a RemoteImages controller to process the requests for images. In the action show, the images will be downloaded and returned with send_data.
You don't have to manage temporary files with both of these options.
You can save the file anywhere in the public folder of the rails application. The right path would be something like this #{Rails.root}/public/myimages/<image_name>.png and then you can refer to it with a URL like this http://localhost:3000/myimages/<image_name>.png. Hope this will help.

Using Paperclip with FFmpeg and multiple output formats: how to store in the database and show in the view a specific output format?

Because I want to use HTML5 video I need to convert videos uploaded by the user into multiple codecs. I am following this tutorial: http://jimneath.org/2008/06/03/converting-videos-with-rails-converting-the-video/ but it only outputs FLV, a format I don't even want at all.
I know I can add more FFmpeg commands and convert multiple times, but the problem lies in the database and the view. How can I define multiple source_file_names in the DB, and how can I specify in #video.source.url which format I want? Should I subclass the Video model, add more table columns for each type or something else?
Paperclip allows you to specify multiple styles. Generally, this is used to specify multiple sizes for uploaded images; Paperclip processes the image once for each style and places them in the filesystem accordingly. By specifying different video formats for your styles and subclassing Paperclip::Processor, you can create your own video formats. In your model, you'd do something like this:
has_attached_file :video, :styles => { :mpeg, :ogg, :wmv }, :processors => [:my_custom_processor]
And then create a custom Processor that runs the correct FFmpeg command based on each style. See the documentation for more info, but here is a snippet:
Paperclip processors allow you to
modify attached files when they are
attached in any way you are able.
Paperclip itself uses command-line
programs for its included Thumbnail
processor, but custom processors are
not required to follow suit.
Processors are required to be defined
inside the Paperclip module and are
also required to be a subclass of
Paperclip::Processor. There is only
one method you must implement to
properly be a subclass: #make, but #initialize
may also be of use. Both methods accept 3 arguments: the file
that will be operated on (which is an
instance of File), a hash of options
that were defined in
has_attached_file’s style hash, and
the Paperclip::Attachment itself.
All #make needs to return is an
instance of File (Tempfile is
acceptable) which contains the results
of the processing.
See Paperclip.run for more
information about using command-line
utilities from within Processors.
When you create a link to a Paperclip attachment, you pass the style to determine which to link to:
<%= link_to "mpeg video", #model.video.url(:mpeg) %>
<%= link_to "ogg video", #model.video.url(:ogg) %>
I suggest you tryout paperclip-ffmpeg gem.

Browser Caching with Paperclip

I have a quite long slideshow of images, all rendered via the awesome Paperclip.
Those images won't change very often, so I'd like to make use of browser caching.
Problem is Paperclip appends a unique load id to the image url.
(eg: pancakes.png?1275839986)
Is there any way to prevent this from happening?
Thanks!
When specifying the image url using:
UrModel.image.url(:style)
# results in: /system/model/000/001/style/style.png?<atimestamp>
Pass in a second parameter which indicates whether or not to include the timestamp:
UrModel.image.url(:style, false)
# results in: /system/model/000/001/style/style.png
Read more about paperclips caching helper: https://github.com/thoughtbot/paperclip/wiki/Tips
That numeric suffix is stored in the database itself by paperclip and is not unique per page load. It's there for caching reasons, actually.

Resources