How to update model in a paperclip processor? - ruby-on-rails

I've build a processor that determine which image format is best to compress my model attached thumbnail. The processor simply build images in png and jpg and check which one is the smallest.
Since Paperclip use the original thumbnail format to build it's thumbnail style URL I had to create a field in my model to store the format of each of the styled thumbnail.
ex: thumbnail_small_content_type, would be "image/png"
In my processor I tried to save the format by using the Paperclip:attachment method : instance_write.
#attachment.instance_write "#{#style_name}_content_type", "image/#{optimised_format}"
Strangely it work perfectly when a create a new model, but failed to work when I use the paperclip method reprocess! to crop my image. Any idea how I could workaround that limitation?

Related

Refinery CMS - How to resize a resource image?

I have a TestimonialResource model which has a resource field type. The backend works okay where I am able to pick a file to upload as a resource to the testimonial.
However, I am unable to generate a thumbnail of the resource(if it is an image). I know it works with an image field type and have done it in the past.
I have also looked at the available methods in the Rails console and it does list thumb as a method.
However, the following does not work in the front-end view.
<%= testimonial_resource.content.thumb('400x400#').url %>
Content is the RefineryCMS resource.
Can someone point me in the right direction?
Dragonfly stores an image once and produces image variations (thumbnails, formats and enhancements) on-the-fly.
So, all related methods are available only to the image model. That means you can't run those methods on everything, but you have a link or local address to the image. So, you can always build a thumb manually:
require "mini_magick"
image = MiniMagick::Image.open("input.jpg")
image.resize "400x400"
image.format "png"
image.write "output.png"
For the existing image object:
image.thumbnail(:geometry => '400x400#c').url

Extract image thumbnail from data

I'm using CGImageSource to read Exif (and other) data of image file. Some image files are embedded with their own thumbnail image (used for file icon).
I know I can extract a file icon using NSWorkspace.shared.icon(forFile:), but I'm already holding a CGImageSource ref, (and not sure of the cost of icon(forFile:) in performance).
Sure I can use the CGImageSourceCreateThumbnailAtIndex(.. to generate a thumbnail, but it would be more efficient to just fetch it (if it exists).
A 'you cannot do that' answer is also a good answer, (then I can go with plan B).

Save image that has been segmented in matlab

I would like to know how can we save an image that has been segmented (using fuzzy c-means method) in MATLAB where the end product are images of each cluster group. I would like to save the images to use later.
I assume you just want to save an image, this should be independent on how you produces that.
If I understood correctly you just have to use the function
imwrite(M, filename)
Where M is the matrix containing your image data. You just need to do this for each matrix/image you have.
Then you can reload the image from filename using imread.
imread(filename)
Note that if you want to specify the format for imwrite and not obtain it via the filename extension, you just to add an additional parameter as follow:
imwrite(M, filename, format)

Session loses its data with Carrierwave multi files upload using jquery file upload

Simply I've a model called Estate which has many :images (Image model) and
I'm using CarrierWave & jQueryFileUpload to handle these in Estate New/Edit forms.
While creating new estate, and upload an image, jQueryFileUpload is doing that using AJAX request, so I make all uploaded images paths be stored in a session array then I use this array in create or update actions to save the images from tmp directory to actual directory.
This works fine with me, but The problem is when I've selected more than one photo at a time, session array store only last selected image, and not all images be pushed to the session array except the last one.
def images_url_list
#image = Image.new(image_params)
session[:cached_images_paths] << #image.image_file.current_path
end
I've debugged this action and I found that if I selected 5 images at a time, images_url_list action is fired 5 times, so say that I upload an image called "path0" then I uploaded 5 images which paths called ["path1","path2","path3","path4","path5"] and the session already has a path called "path0", first time after uploading first image the session will be ["path0", "path1"], second time ["path0","path2"] and so on until last image which is path5.
So the final count of image paths is only 2 rather than 6 image paths.
Can anyone tell me what exactly the problem is?
I think the problem is the multi-request and the session save.
In my case using a similar environment, only the last uploaded file was saved. I couldn't make it work adding it in a filesUploaded array, so I though about an alternative solution.
A good approach might be:
Generate a session upload hash and store it for each uploaded file on the upload listener
Use it to identify the uploaded files after the multi-upload is finished

Saving files using Carrierwave without forms

I have 2 models - Album and AlbumImage.
Each album has album images associated with them, and they are uploaded via the AlbumImageUploader class using Carrierwave.
Now I want to select an album cover for each album using the existing associated album images. I need to process (crop and resize) this selected image before I use it as the album cover. I have the cropping and the resizing functions down, and i created an AlbumCoverUploader class to save this processed version of the album image to.
The problem is that this time I am not using a form to upload a new image file and instead using an existing album image in the file system, and I'm not sure how to transfer this image from my AlbumImageUploader class to my AlbumCoverUploader class.
Any ideas?
This is really simple.
You have to configure your AlbumCoverUploader the same way as if you would upload it from a form.
Though, to use an image which is associated with an existing record, you must do the following:
album = Album.find(id) # your existing album
album_image = album.album_images.first # the image you want as cover
album.cover = File.open(album_image.image.current_path)
album.save
This will grab the image file and use as an input for the AlbumCoverUploader to create its own copy of the image.

Resources