The latest in my of “shouldn’t this be better covered in Google and docs if people really use CarrierWave?”-series. Creating a custom thumbnail processor in Carrierwave is pretty straightforward, but not from any search query I could construct. The gist:
class MyUploader < CarrierWave::Uploader::Base
version :custom_thumbnail do
# Here our context is the CarrierWave::Uploader object, so we have its full
# assortment of methods at our disposal. Let's say we want to open an image with
# openCV and smooth it out. That would look something like:
cv_image = OpenCV::CvMat.load *([ self.full_cache_path ]) # See past blog post on the origins of full_cache_path
# The key to telling CW what data this thumb should use is to save our output to
# the current_path of the Uploader, a la
Just call #custom_thumbnail.url on your Uploader instance, and you should get the path to the custom result you created.
Using this framework you should be able to get CW to perform whatever sort of custom image processing you want. Thanks to these fellows for helping me decode the #current_path magic here.
Have now needed to do this twice, and both times have required about an hour of sifting through Google morass to figure out how to pull this off. The situation we’re assuming here is that you have some CarrierWave file that you’ve stored to a remote location, and you want to get a copy of that file locally to manipulate it. With promising method names like “retrieve_from_cache!” and “cache!” and “move_to_cache” you too may become entangled in the maze of what the hell you’re supposed to be calling to accomplish this. Here’s what.
Step 1: Retrieve from your store (the external place your file exists) to cache (your local machine).
After running that, if you call
You should get a long string that is the filename for the file that got cached. Note that the cache status of a file is only saved on a per-object basis, so if you try something like
… you will never again see the object that did the caching, and you will never be able to use that cache. Only call #cache_stored_file! on an object you are keeping around.
Step 2: Go access the file that you cached. Not quite as easy as it sounds. For this, I mixed in the following into my uploader class
So now when you call
You’ll get the full pathname to the file you downloaded.
Hope this helps save someone else from an hour of Google hell.