Question

I have an Event model that has many photographs. I have an image uploader mounted to the Photographs attribute, and regular uploads and everything is working fine.

However, when I try and duplicate an event, recreating a new photograph object for the new event, the new image is darker than the original, and if I duplicate the duplicate event, it gets darker still.

I have played around with it, but have no solution.

My Uploader code:

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick
  include CarrierWave::Processing::RMagick

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

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

  process :colorspace => :rgb

  # Remove EXIF data
  process :strip

  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_limit => [640, 640]
  end

  version :preview_thumb do
    process :resize_to_limit => [600, 600]
  end

  version :wine_thumb do
    process :resize_to_limit => [160, 440]
  end

  version :logo_thumb do
    process :resize_to_limit => [90, 90]
  end
end

And my duplcation code (in Active Admin):

member_action :create_duplicate_event, method: :post do
  old_event = Event.find(params[:id])
  photograph_urls = old_event.photographs.map(&:image_url)
  attributes = old_event.attributes.except("photographs", "id")

  new_photos = []
  photograph_urls.each do |photo|
    new_photo = Photograph.new({
      remote_image_url: photo
    })

    if new_photo.save
      new_photos << new_photo
    end
  end
  @event = Event.new(attributes)
  @event.photograph_ids = new_photos.map(&:id)

  render "/admin/events/_events_form/"
end

The :rgb tag was an attempt to fix. But no luck.

Ruby 2.1 and Rails 4.0

Était-ce utile?

La solution

Ok, after a lot of playing around and searching I managed to fix this problem.

First, you need to download the .icc color profiles, which can be found here. It says for windows but they seemed to work for me on my Mac.

Once you have put the .icc files into a /lib/color_profiles directory, add the following code to your uploader:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  process :convert_image_from_cmyk_to_rgb

  #versions, and any other uploader code go here

  def convert_image_from_cmyk_to_rgb
    manipulate! do |image|
      if image.colorspace == Magick::CMYKColorspace
        image.strip!
        image.add_profile("#{Rails.root}/lib/USWebCoatedSWOP.icc")
        image.colorspace == Magick::SRGBColorspace
        image.add_profile("#{Rails.root}/lib/sRGB.icc")
      end
      image
    end
  end
end

This converts CMYK images to RGB, and keeps the profiles keeping nice, while keeping RGB images as they were, and not ruining them.

I hope this helps someone in the future, and saves them the hours I spent working this out.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top