kirtithorat / carrierwave-crop

Carrierwave extension to crop uploaded images using Jcrop plugin with preview.
MIT License
94 stars 58 forks source link

Code breaks if image is large #11

Open alekseyl opened 9 years ago

alekseyl commented 9 years ago

Hi I have a problem with your solution: If image is too large it's get resized by jCrop or browser ( didn't get quite well who is doing it ) ( For example when I tried 3000x2000 actually I get something near 1700x1400 ). And as a result cropped area is not as I expected ( it's smaller than you see in preview ).

I have Ad and photo instead of User and avatar, and using MiniMagick, so my monkey patch looked like this:

1) I added photo_displayed_height and photo_displayed_width accessors to model which owns attachment, so I can store in it size of photo actually displayed in browser

2) added them to form:

 = f.previewbox :photo, width: 100, height: 100
  =f.hidden_field :photo_displayed_height
  =f.hidden_field :photo_displayed_width

3) added this code to coffee:

update: (coords) =>
    $('#ad_photo_displayed_height').val($('#ad_photo_cropbox').height())
    $('#ad_photo_displayed_width').val($('#ad_photo_cropbox').width()) 

4) AND the most important part in processors I reprocess attributes ( it can be done also in the controller ):

version :thumb do
    process :transform_crop_attributes
    process crop: :photo  ## Crops this version based on original image
    resize_to_limit(100,100)
  end

  def transform_crop_attributes
    if file && model
      real_width, real_height = MiniMagick::Image.open(file.file)[:dimensions]
      if real_height != model.photo_displayed_height || real_width != model.photo_displayed_width
        [:photo_crop_y, :photo_crop_w].each do |attr|
          model.send(attr) && model.send("#{attr}=", model.send(attr).to_i * real_height.to_i / model.photo_displayed_height.to_i )
        end
        [:photo_crop_x, :photo_crop_h].each do |attr|
          model.send(attr) && model.send("#{attr}=", model.send(attr).to_i * real_width.to_i / model.photo_displayed_width.to_i )
        end
      end
    end
  end
kirtithorat commented 9 years ago

@alekseyl I am a little confused with the issue that you are facing. Is it like you get a large image, preview it, crop it and the cropped image is not what you set in the preview? And you face this issue only for large images?

RoxasShadow commented 9 years ago

I'm facing that problem too, I'll try my best to explain it successfully.

The following works fine:

wallhaven-3590

schermata 2015-05-28 alle 10 40 58

This one is badly cropped

wallhaven-74042

schermata 2015-05-28 alle 10 37 50

Both have the same dimensions (2560x1600), but while the first one is 3.5mb, the second one is just 1mb.

My CoverUploader saves three versions of that image: the original, the medium and the cropped. The first two ones are fine, but the last one is the black sheep.

  include CarrierWave::RMagick

  version :medium do
    process resize_to_fit: [255, 9999]
  end

  version :cropped do
    process crop: :cover
    resize_to_fill 1140, 272
  end

Thank you in advance.

alekseyl commented 9 years ago

Is it like you get a large image, preview it, crop it and the cropped image is not what you set in the >preview?

Yes

And you face this issue only for large images?

Yes only XXL images faced that problem. And only if displayed for cropping image dimensions is smaller than original dimensions.

alekseyl commented 9 years ago

I'm facing that problem too, I'll try my best to explain it successfully.

Are you sure that crop is correct in first place? I've seen little differences between preview and cropped_result, so it may crop incorrectly but the result may be close to the one is expected.

RoxasShadow commented 9 years ago

I've seen little differences between preview and cropped_result

Actually the image I put before the cropped one is the original image. Which preview are you referring to?