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 8498893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:31:33+00:00 2026-06-11T00:31:33+00:00

I would like to use jpegoptim or optipng to compress the image uploaded by

  • 0

I would like to use jpegoptim or optipng to compress the image uploaded by users via Paperclip.

I have a Paperclip model configured as:

  has_attached_file :image,
                    :styles => {:thumb => '50x50>', :preview => '270x270>' },
                    :url => "/system/:class/:attachment/:id/:basename_:style.:extension",
                    :path => ":rails_root/public/system/:class/:attachment/:id/:basename_:style.:extension"

Question 1:
Is it possible to compress the original image uploaded by user, then let Paperclip resize it , so there’s only one compress process? and how to do it?

Question 2:
I am going to do it via the after_post_process callback, and I could get all the instances of three files from image.queued_for_write and I would like to trigger jpegoptim/optipng by the file extension, but when I use current_format = File.extname(file.path), I get something like: .jpg20120508-7991-cqcpf2. Is there away to get the extension string jpg? or is it safe that I just check if the extension string is contained in that string?

  • 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-06-11T00:31:34+00:00Added an answer on June 11, 2026 at 12:31 am

    Since there’s no other answers, here is how I do it in my project and it seems it’s being working ok for several months.

    class AttachedImage < ActiveRecord::Base
      belongs_to :attachable, :polymorphic => true
    
      validates_attachment_presence :image
      validates_attachment_content_type :image, :content_type=>['image/jpeg', 'image/png', 'image/gif']
    
      has_attached_file :image,
                        :styles => {:thumb => '50x50>', :preview => '270x270>' },
                        # :processors => [:image_compressor],
                        :url => "/system/:class/:attachment/:id/:basename_:style.:extension",
                        :path => ":rails_root/public/system/:class/:attachment/:id/:basename_:style.:extension"
    
    
      after_post_process :compress
    
      private
      def compress
        current_format = File.extname(image.queued_for_write[:original].path)
    
        image.queued_for_write.each do |key, file|
          reg_jpegoptim = /(jpg|jpeg|jfif)/i
          reg_optipng = /(png|bmp|gif|pnm|tiff)/i
    
          logger.info("Processing compression: key: #{key} - file: #{file.path} - ext: #{current_format}")
    
          if current_format =~ reg_jpegoptim
            compress_with_jpegoptim(file)
          elsif current_format =~ reg_optipng
            compress_with_optpng(file)
          else
            logger.info("File: #{file.path} is not compressed!")
          end
        end
      end
    
      def compress_with_jpegoptim(file)
        current_size = File.size(file.path)
        Paperclip.run("jpegoptim", "-o --strip-all #{file.path}")
        compressed_size = File.size(file.path)
        compressed_ratio = (current_size - compressed_size) / current_size.to_f
        logger.debug("#{current_size} - #{compressed_size} - #{compressed_ratio}")
        logger.debug("JPEG family compressed, compressed: #{ '%.2f' % (compressed_ratio * 100) }%")
      end
    
      def compress_with_optpng(file)
        current_size = File.size(file.path)
        Paperclip.run("optipng", "-o7 --strip=all #{file.path}")
        compressed_size = File.size(file.path)
        compressed_ratio = (current_size - compressed_size) / current_size.to_f
        logger.debug("#{current_size} - #{compressed_size} - #{compressed_ratio}")
        logger.debug("PNG family compressed, compressed: #{ '%.2f' % (compressed_ratio * 100) }%")   
      end                              
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of Integers in Java, I would like use only a
I would like use a panel whose children have coordinates specified as percentage of
I would like to use PyroCMS for a golf company website. PyroCMS have very
Would like to use an Image button in my widget layout. I want an
I currently have some large strings that I would like use as test data
I have for example: $('#field').click(function(){ alert('One'); alert('Two'); }) and i would like use trigger:
I would like to use R to extract the speaker out of scripts formatted
I would like to use Maven's password encryption such as it uses for nodes
I would like to use the logout function from Django but not sure how
I would like to use D3.js (or maybe Raphaël ) for backend-generated reports using

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.