Create picture thumbnails
Originally created by Miha Filej, @mfilej
The Internet is all about speed. The faster your page loads, the less likely people are to click away. And we want people to stick around to share our great ideas with our ideas app! Also, people with data plans visiting your website will thank you for requiring less data to be transmitted.
One way to speed up page loads is by displaying images in a smaller size. The smaller an image is, the fast it will be transmitted over the Internet.
We’ll be using the ImageMagick tool to resize the pictures uploaded to your ideas.
Run the following command in the Terminal app:
If you are on Ubuntu, run the following command in the Terminal app:
Download and run the ImageMagick installer (use the first download link). In the installation wizard, make sure you check the checkbox to install legacy utilities.
Explain what is ImageMagick and how is it different from libraries/gems we used before?
Install a Ruby gem for ImageMagick
For Ruby to talk with ImageMagick, we’ll be using the
mini_magick Ruby gem. First we will need to add it to our app and install it.
Gemfile in your Text Editor and add this line:
below the line:
In the Terminal app run this command:
Make sure to (re)start your Rails server after installation.
Tell your app to create thumbnails
Now that we have a way to talk to ImageMagick through the
mini_magick Ruby gem, we can tell the file upload gem
carrierwave to create thumbnails for every picture you upload.
app/uploaders/picture_uploader.rb and find the line that looks like this:
# sign at the front of the line.
Explain the concept of comments in code.
Below the line you just changed, add these lines:
The images uploaded from now on will be resized to a smaller size, but the ones we already have haven’t been updated. Instead, let’s edit an idea and add a new picture. When saved the idea now has a thumbnail for the uploaded picture.
Display the thumbnail
We haven’t changed how the idea pictures are displayed, so it should still be showing the original larger image. Let’s change the views to display the thumbnail instead.
app/views/ideas/_idea.html.erb and change the line:
to this line:
Take a look at the list of ideas in the Browser to see if your ideas now have a thumbnail.
Explain what specifying the image width in HTML and how it differs from resizing images on the server. Both images may look small, but only one is resized as a thumbnail.
If you’re ever stuck during a guide, please ask your coach for help and also consult this handy cheatsheet for Ruby, Rails, the console, the Text Editor etc.
- Guide 1: Start of the guide
- Guide 2: Get to know the tools
- Guide 3: Guide to install Rails
- Guide 4: Build Your First App
- Guide 5: Style your app using HTML and CSS
- Guide 6: Add a new page to your app
- Guide 7: Add a new homepage to your app
- Guide 8: Add picture uploads
- Guide 9: Push Your App to GitHub
- Guide 10: Put your app online with one of these services:
- Guide 11: Style the idea pages using HTML and CSS
- Guide 12: Add comments to your app
- Guide 13: Create picture thumbnails (Current page!)
- Guide 14: Test your app with RSpec
View all guides