0
votes

I cannot get this CarrierWave uploader to process the image and resize how I would like.

The image is initially created with this.

class Screenshot
    include Sidekiq::Worker

    def perform(link_id)
        link = Link.find(link_id)
        file = Tempfile.new(["template_#{link.id.to_s}", '.jpg'], 'tmp', :encoding => 'ascii-8bit')
        file.write(IMGKit.new(link.given_url, #quality: 50, width: 300).to_img(:jpg))
                                     :quality => 50, 
                                    :width => 300,
                                    :height => 300,
                                    "crop-w" => 300,
                                    "crop-h" => 300,
                                    "disable-smart-width" => true,
                                    "zoom" => 0.35).to_img(:jpg))
        file.flush
        link.snapshot = file
        link.save
        file.unlink
    end

end

I could not get that working, so I thought I'll just resize in the uploader. Then I modified the uploader to do that as per this:

class SnapshotUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick

  storage :file
  storage :fog
  process :convert_it

  def convert_it
    manipulate! do |img|
        img.resize '300x300'
        img
    end
  end

  def convert_it2
    resize_to_limit [300,300]
  end

  def convert_it3
    resize_to_fit [300,-1]
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end
end

And that doesn't work either. I've tried each of the convert it methods, along with just a direct

process :resize_to_limit [300,300]

The image is created. Seems to be now always at 600 width by whatever height.

The image is uploaded to AWS S3.

I have Imagemagick installed with the --disable-openmp from homebrew. I can run mogrify and convert from the command line.

I can resize an image and save it in rails console with MiniMagick without issue.

I have made sure CarrierWave initializer has config.enable_processing = true.

I dont see any error messages in the console, or the sidekiq terminal window.

I'm new at this so I'm stumped at what else to try. Why is my image not resizing to the dimensions I've asked?

EDIT: The 600 widths were coming from IMGKit which means I have still never had carrierwave actually process the image.

1
You try using a version. version :medium do process :resize_to_limit => [300, 300] endRafal
I can try that, Is processing only supposed to work with versions? I thought I had seen examples where they can just modify the original, as I only need to get the smaller version, nothing else.Chris Valentine
It should work on the original also. Is S3 creating a new image every time or replacing the current image ?Rafal
Create a new image in a folder with the post ID every time. Its merely the wrong dimensionsChris Valentine
@ChrisValentine you can call process outside of versions. However it will replace the original image with processed version.Monideep

1 Answers

0
votes

Well this turned out to be causd by Sidekiq not being restarted everytime I changed the code.

I was unaware you needed to do that, however as far as I know the uploader does not use sidekiq to do its upload, so I'm unclear why it was a side effect.

At any rate, stopping sidekiq each time I did a code change, has caused this to work.

I have also found the gem Foreman to help start/stop all services together.

If anyone can explain to me why Sidekiq interfered with this let me know. Sidekiq was responsible for making the image, and an image was being created. The resize was just not happening during upload.