Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 773251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:56:07+00:00 2026-05-14T18:56:07+00:00

Trying to get the width and height of the uploaded image while still in

  • 0

Trying to get the width and height of the uploaded image while still in the model on the initial save.

Any way to do this?

Here’s the snippet of code I’ve been testing with from my model. Of course it fails on “instance.photo_width”.

has_attached_file :photo,
                      :styles => {
                      :original  => "634x471>",
                      :thumb => Proc.new { |instance|                       
                                  ratio = instance.photo_width/instance.photo_height
                                  min_width   = 142
                                  min_height  = 119
                                  if ratio > 1
                                    final_height  = min_height
                                    final_width   = final_height * ratio
                                  else
                                    final_width   = min_width
                                    final_height  = final_width * ratio
                                  end
                                  "#{final_width}x#{final_height}" 
                                }
                    }, 
                    :storage => :s3,
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => ":attachment/:id/:style.:extension",
                    :bucket => 'foo_bucket' 

So I’m basically trying to do this to get a custom thumbnail width and height based on the initial image dimensions.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:56:08+00:00Added an answer on May 14, 2026 at 6:56 pm

    Ahh, figured it out. I just needed to make a proc.

    Here’s the code from my model:

    class Submission < ActiveRecord::Base
    
      #### Start Paperclip ####
    
      has_attached_file :photo, 
                        :styles => {
                          :original  => "634x471>",
                          :thumb => Proc.new { |instance| instance.resize }
                        }, 
                        :storage => :s3,
                        :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                        :path => ":attachment/:id/:style.:extension",
                        :bucket => 'foo_bucket' 
    
      #### End Paperclip ####
    
      def resize     
         geo = Paperclip::Geometry.from_file(photo.to_file(:original))
    
         ratio = geo.width/geo.height  
    
         min_width  = 142
         min_height = 119
    
         if ratio > 1
           # Horizontal Image
           final_height = min_height
           final_width  = final_height * ratio
           "#{final_width.round}x#{final_height.round}!"
         else
           # Vertical Image
           final_width  = min_width
           final_height = final_width * ratio
           "#{final_height.round}x#{final_width.round}!"
         end
      end  
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a JEditorPane to highlight the full width of a displayed
I'm trying to get started with USSD . I'm familiar with other forms of
I'm trying to get started with unit testing in Python and I was wondering
I have a SOAP client in Ruby that I'm trying to get working with
I am trying to get the Edit with Vim context menu to open files
Hi I'm trying to get some practice with Linked Lists. I Defined an Object
I'm trying to get a file with ant, using the get property. I'm running
I'm trying to get the contents from another file with file_get_contents (don't ask why).
Right, I'm trying to get a label with text in it (done already obviously),
I have the following code, I'm trying to get a table with 4 columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.