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

  • Home
  • SEARCH
  • 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 6158755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:00:49+00:00 2026-05-23T21:00:49+00:00

My problem is the next: I am trying resize a image size depending a

  • 0

My problem is the next:

I am trying resize a image size depending a proportial size. Example If i have a image whose size is 1440*1000 its new size will be 648*440 (I use a proportion depending a max_size)

NOTE: Then i post my code so you will understand the size relations.

Ok. so I am reading this stackoverflow post:

Getting width and height of image in model in the Ruby Paperclip GEM

Now i post my code and then i will describe my problem.

 class ProductImage < ActiveRecord::Base
      belongs_to :product, :dependent => :destroy

      MAXIMUM_SIZE = 650

      has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => {:real_size => Proc.new { |instance| instance.real_size }, :original => "400x400>", :medium => "300x300>", :principal => "240x240>", :thumb => "100x100>", :small => "80x50>"}

     def real_size
        #image = Paperclip::Geometry.from_file(photo.to_file(:maximum_size))
        #OBTAIN REAL IMAGE SIZE, NOT ATTACHMENT SIZES
if image_less_than_maximum_size?
          return "#{image.width}x#{image.height}"
        else
          return adjust_image_size(self.width, self.height)
        end
      end

      def adjust_image_size(image_width, image_height)
        ratio                           = (image_width/image_height).to_f
        difference_between_size         = (image_width - image_height).abs
        percentage_difference           = ratio > 1 ? difference_between_size * 100.0 / image_width : difference_between_size * 100.0 / image_height
        difference_respect_maximum_size = ratio > 1 ? MAXIMUM_SIZE * 100.0 / image_width : MAXIMUM_SIZE * 100.0 / image_height
        width                           = height = 0.0

        if ratio > 1
          #USE 101.0 FOR INCREMENT OR DECREMENT THE VALUE A LITTLE BIT
          width  = image_width * difference_respect_maximum_size / 101.0
          height = width - (percentage_difference * width / 101.0)
        else
          heigth = image_height * difference_respect_maximum_size / 101.0
          width  = height - (percentage_difference * height / 101.0)
        end

        return "#{width}x#{height}"
      end

      def image_less_than_maximum_size?
        if self.width > self.height
          return self.width < MAXIMUM_SIZE
        else
          return self.height < MAXIMUM_SIZE
        end
      end
    end

My problem is how could i obtain the “real_size”?.
i.e, if image size is “1440*1000” to obtain this size (no attachment size)

UPDATE:

I am thinking a solution. So i think in declare two temp variable to ProductImage model and during initialize method use a before_post_process paperclip callback.

    class ProductImage < ActiveRecord::Base
      belongs_to :product, :dependent => :destroy
      attr_accessor :height, :width

      MAXIMUM_SIZE = 650

      has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => {:real_size => Proc.new { |instance| instance.real_size }, :original => "400x400>", :medium => "300x300>", :principal => "240x240>", :thumb => "100x100>", :small => "80x50>"}
      before_post_process :image?
      before_post_process :assign_size

    ...

    def assign_size
            @width = Paperclip::Geometry.from_file(remote_original_photo_path).width
            @height = Paperclip::Geometry.from_file(remote_original_photo_path).height
    end

end

Then i could use this size in the other method.

My new problem is how could I determine the remote_original_photo_path in model?

in controller i use the params[:product][:product_images_attributes][index][:photo].

I could save the temp path in model. However because my real_size method during initilize i don´t know how to pass the params info.

Thanks in advance again

  • 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-23T21:00:50+00:00Added an answer on May 23, 2026 at 9:00 pm

    With using a gem like image_size ?

    [EDIT]

    To determine the original upload path may be you can use :

    remote_original_photo_path  = File.basename(upload['datafile'].original_path)
    
    • 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 regexp to solve the next problem: I have some
Greetings, I have problem with errorPlacement, I'm trying to place the error message next
I have the next problem: I am trying to deploy an application on my
I am trying to resize an image using only CSS, the problem is I
I have the next problem: I need to process only 1 request at a
I have the next problem. In one of the cells (UITableViewCell) I need to
I have the following problem: We need to find the next august. I other
I have a small problem where I want to find the next active item
I have the following problem: list.c struct nmlist_element_s { void *data; struct nmlist_element_s *next;
I'm trying to use next() to toggle a div. The problem is that the

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.