Get image dimensions with Paperclip in Rails

Hi, it’s been some time since the last post. Lots of work and excuses.
Today I will present a challenge that I had in a new project. The client wanted the original dimensions when uploading images. So I made a private method to extract it with the help of ImageMagick’s Geometry module.

First, add new columns through a migration:

bundle exec rails g migration AddImageDimensionsToPosts
class AddImageDimensionsToPosts < ActiveRecord::Migration[5.0]
  def up
    add_column :posts, :image_width, :integer
    add_column :posts, :image_height, :integer
  end

  def down
    remove_column :posts, :image_width, :integer
    remove_column :posts, :image_height, :integer
  end
end
bundle exec rails db:migrate

Then implement the method:

class User < ApplicationRecord
  belongs_to :team
  has_attached_file :image, {
    styles: {
      new_thumb: 'x1144',
      new_medium: '1242x',
      web_feed: '600x',
      thumb: '200x'
    }
  }

  before_save :extract_dimensions

  # .....

  private
    def extract_dimensions
    tempfile = self.temp_image_url
    unless tempfile.nil?
      geometry = Paperclip::Geometry.from_file(tempfile.sub('https', 'http'))
      geometry.auto_orient
      self.image_width = geometry.width.to_i
      self.image_height = geometry.height.to_i
    end
  end
end

As you saw we replaced https with http because it gave problems with Amazon’s AWS security which is being used to save the files.

Next we wanted to update all the users’ images that were already in the system. And as a need we created the following script:

heroku run rails console

def update_posts
  time = Time.now
  posts_count = 0
  posts_total = Post.where(image_width: nil, image_height: nil).count
  Post.where(image_width: nil, image_height: nil).find_each do |post|
    image = post.image(:original)
    geometry = Paperclip::Geometry.from_file(image.sub('https', 'http'))
    geometry.auto_orient
    post.update_attributes(image_width: geometry.width.to_i, image_height: geometry.height.to_i)
    posts_count = posts_count + 1
    puts "#{posts_count}/#{posts_total}"
  end
  puts"#{Time.now - time}"
end

update_posts

We used the find_each method instead of each, with each all the objects are loaded into memory and find_each loads one by one and in our case was much faster as it did not stress the server.

Taking more than you can handle

Some time ago a friend gave me a coding challenge which I decided to make it in Ruby on Rails, I hesitated because was expecting that an Enterprise would give me a task to build something in JavaScript/HTML/CSS, so the time passed. Then I applied for a company that is hiring any kind of developer that understands how the Full Stack works and got another coding challenge. This time in Nodejs and Express. Perhaps I do not need to say that I have to learn a whole new technology to get the challenge working and net the job. Besides all of this I enrolled in a course from Berkeley University of California called SaaS (Software as a Service) and the framework they use is… You got it! Ruby on Rails!
How am I going to do all of this? Since I like to finish what I’ve started (which sometimes is not possible), I feel stressed about this situation and wasn’t expecting all that’s happening. I heard somewhere that it is not about the how but the why. Why do I want to be a Developer? Why do I want to learn? Why am I pushing through all the resistance? Why do I care about Self-Development and read/listen to a lot of books? The answer is simple, I want to become the best, learn with the best, be on the top 3%. Is it simple or easy? I don’t think so. Do I think it is possible? Absolutely.

My first programming interview

First of all I finished after work the Ruby Koans, it was an awesome experience and made me understand much more about testing. It happened so fast that I felt sad for being so short but at the same time felt an inner feeling that I have accomplished something good. Rails Tutorial is next (for a second time).

My first interview. Oh when I received a phone call from a recruiter scheduling an interview for a Python position I was in complete awe! I remembered her saying: “next Tuesday at 4:30PM”. I was like: “Yes! Sure! “The following week I studied Python as a voracious bear that hunts his prey, mastered my skills and was prepared. When I went to the subway and read the confirmation email, saw that it was changed to 4PM, and the time was 3:55PM. I rushed to call the recruiter but she didn’t answer at first, the while on the train called again and got in touch with her.
I arrived late and had to wait an excruciating 20 minutes! Finally comes the girl that was going to interview me. She asked me a few pseudo technical questions which I got right and then asked me if I had completed my college education which I said no, and then she said that I wasn’t suitable for the job because of that. No tests whatsoever, then the entire interview questions were about Ruby and soon I realized that she had 0 knowledge about programming. She didn’t know that Ruby was an Object Oriented Language and that Ruby is not Ruby on Rails the later is a framework and the former is the language.
I was really pissed off and lost an opportunity (a story for a later post). It can be seen that the interviewer haven’t read my resume and also does not have respect for other people’s time.

After all life goes on and I grew from this experience and attacked coding with more power. There will be other interviews and the job that I want will come true. With hard work, habit and will-power, great things happen.