CarrierWave how to change object path after downloaded - ruby-on-rails

I've to duplicate a Model which contains a CarrierWave image but I need to use the same image file, the problem is that my file path it's using the model UUID and after saving new object the UUID of the image is updated too.
How can I change the image file path after saving the object to keep the original image path?
This is my image stored at aws:
#mounted_as=:my_cover, #storage=#<CarrierWave::Storage::Fog:0x0000000002ef #uploader=#<CoverUploader::Uploader82999640:0x0000000002ef ...>>, #file=#<CarrierWave::Storage::Fog::File:0x0000000002ef #uploader=#<CoverUploader::Uploader82999640:0x0000000002ef
I can access Model.my_cover.file.path or Model.my_cover.cover.path but I can't change the path value.
I don't want to duplicate the image because this will double the file storage space.
Thanks for any help!

When duplicating the object, you should be able to copy along the image from the previous object before saving the new one.
Let's say your model is called Model and has an image attribute called my_cover.
a = Model.new
b = a.dup
b.my_cover = a.my_cover
b.save
Carrierwave uses an image object that references an image URL and that you can copy to a different object.

Related

How to use version numbers with CarrierWave?

Suppose you want to have an ImageUploader, and when an image is uploaded, it is saved using the filename "/image_1.png". Each time a new image is uploaded, the number is incremented: "/image_2.png".
What is a good way to do this?
Do you add a field in the ImageUploader to store the version_number? Or does this field need to be added to the Image model?
What is a good way to increment when a new file is uploaded?
for i=1:10
filename=[ 'image_'num2str( i )'.txt' ]
end
or add '/' before 'image_'

RoR + Paperclip - Have two objects reference same paperclip image

I'm creating a demo portion to my website. Where each new demo account generates dummy data. The dummy data includes images. It takes way too long and takes up unnecessary space when creating these dummy images though. They are just copies of images that already exist in the database.
I have an Image object with the relationship has_attached_file :pc_image. I want a new image to reference the pc_image in an old image, not create a duplicate. I've been trying something like this (p statements added to show the difference), but it just won't work:
p old_image
=> images/001/original/old_image.jpg
new_image.pc_image = old_image.pc_image
new_image.save
p new_image
=> images/002/original/old_image.jpg
Notice the ID after images... 001 and 002. Paperclip seems to automatically create a new URL using the ID of its parent object instead of using the old URL. It doesn't duplicate the images though, which is what I want. But I'm getting a missing image in the view because the pc_image URL is pointing to an image that was never created instead of pointing to the old_image URL, which is what I want. Is this possible? Is there a good workaround?

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