![]() # returns the cover image instanceĭid you notice that these images are generated instantaneously? This means that the image conversion happens in the same thread and execution is blocked until it completes. Check the versions created by Carrierwave: # returns the thumb image instance This creates 2 new versions, along with the original image. To avoid this, create multiple versions of the file. We won’t have a copy of the original file for any future edits. However, the uploaded image is immediately processed and overwritten with the 200×200 image. The uploaded image is available under the store_dir. Try creating a new image from the Rails console: media = File.open("/Users/test/Desktop/image/jpg") NOTE: If you’re on ActiveRecord or DataMapper, the configurations would be slightly different and the official Carrierwave documentation will show you the way.īundle to fetch all these gems: bundle installĬreate our first uploader: #app/uploaders/image_uploader.rb Gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid' It also interacts nicely with MiniMagick, making our lives much simpler. Now, let’s see how we can tie this up with our Rails app.Ĭarrierwave is a wonderful gem which simplifies file uploads in Ruby. In my defense, I’m just trying to show all the cool stuff that you can do with MiniMagick :). You can also combine multiple operations in a single block: bine_options do |i| Image.write "public/uploads/test.png" Going Crazy MiniMagick makes this very simple: image.format "png" One of the most frequent operations you’ll do is convert images to different formats. To persist it to disk, simply call the write method: image.write "public/uploads/test.jpg" Converting Image The manipulated image is stored in a temp path and will be washed away. Wait a second, where is this changed file stored? image.path # temp path # Now get the image's new width and height ![]() Let’s see if we can resize this to fit our iPad: image.resize "2048x1536" Open up the Rails console ( rails c) and run the following: # Open an image from a website Let’s play around with some of the MiniMagick’s features before building anything serious.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |