Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoMethodError - undefined method `write' for "":String in custom processor #436

Closed
kmarsh opened this issue Aug 18, 2011 · 12 comments
Closed

Comments

@kmarsh
Copy link

kmarsh commented Aug 18, 2011

I have a custom processor, using MiniMagick's manipulate! method. Here's a summary of my code:

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

  storage :file

  def filename
    "#{model.id}.#{file.extension}"
  end

  def store_dir
    "uploads"
  end  

  version :blurred do
    process :blur
  end

  def blur
    manipulate! do |img|
      img = img.radial_blur 10
    end
  end
end

Problem is, when processing this, CarrierWave throws an error:

NoMethodError - undefined method `write' for "":String:
/Users/kmarsh/.rvm/gems/ruby-1.8.7-p302/gems/carrierwave-0.5.5/lib/carrierwave/processing/mini_magick.rb:246:in `manipulate!'

It seems as though current_path is returning "" for some reason, but I'm not too sure why.

@trevorturk
Copy link
Contributor

Can you provide a sample app that uses the most recent version of CW? A full stack trace would help, too. Here's an example I made to track something down: https://github.com/trevorturk/carrierwave-issue-381

@brewster1134
Copy link

i too am struggling with this using mini_magick's resize

  def resize_by_height(height)
    manipulate! do |img|
      img = img.resize("x#{height}")
    end
  end

if i tweak the argument (img.resize("'x#{height}'")) so it throws the CarrierWave::ProcessingError exception, the error is more helpful...

CarrierWave::ProcessingError: Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: Command ("mogrify -resize "'x75'" /var/folders/60/22_1pphd513_xj6j36smvdy80000gn/T/mini_magick20110818-83173-1j425yg.jpg") failed: {:status_code=>1, :output=>"mogrify: invalid argument for option `'x75'': -resize @ error/mogrify.c/MogrifyImageCommand/5423.\n"}

essentially it seems that it is is wrapping the command line arguments in quotes

@brewster1134
Copy link

oops. forgot to include yield(img) if block_given?. now its working fine. perhaps its the same issue for @willcodeforfoo

@trevorturk
Copy link
Contributor

@brewster1134 can you provide an example before/after?

@kmarsh
Copy link
Author

kmarsh commented Aug 19, 2011

Didn't seem to make a difference for me, current_path is still "".

@trevorturk: I used your sample Sinatra app posted on the Engine Yard blog recently as a basis for a simple app that demonstrates the issue: https://gist.github.com/1157829

@trevorturk
Copy link
Contributor

This seems to work for me. Maybe you'd be interested in making a wiki page about it?

@trevorturk
Copy link
Contributor

Forgot the link :) https://gist.github.com/5d66897d88d1b5dfbea5

@kmarsh
Copy link
Author

kmarsh commented Aug 19, 2011

That did work! So the keys for processors are:

  1. Call method directly on existing img rather than assigning to img
  2. Make sure to yield if a block is given, img = yield(img) if block_given?
  3. Return img at the end of the processor

lib/carrierwave/processing/mini_magick.rb should probably be edited to reflect this, as it was where I was getting the code from, thinking that img = img.foo would work:

  # Or create your own helpers with the powerful manipulate! method. Check
  # out the ImageMagick docs at http://www.imagemagick.org/script/command-line-options.php for more
  # info
  #
  #     class MyUploader < CarrierWave::Uploader::Base
  #       include CarrierWave::MiniMagick
  #
  #       process :do_stuff => 10.0
  #
  #       def do_stuff(blur_factor)
  #         manipulate! do |img|
  #           img = img.sepiatone
  #           img = img.auto_orient
  #           img = img.radial_blur blur_factor
  #         end
  #       end
  #     end

Would you like a pull request for this change?

I'll see about making a wiki page for custom processing actions.

@trevorturk
Copy link
Contributor

Yeah, pull requests and wiki pages would definitely help. I agree the docs need some work. It was hard for me to tell what was the "right way" to do this :)

@dlindahl
Copy link

The details of this Wiki addition should also be reflected in the RDOC output as found here:

http://carrierwave.rubyforge.org/rdoc/classes/CarrierWave/MiniMagick.html

@trevorturk
Copy link
Contributor

http://carrierwave.rubyforge.org/ is no longer updated

@Awea
Copy link

Awea commented Nov 26, 2012

I get the same error but for a different processing :

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

  # S3 Storage
  storage :fog

  def extension_white_list
    %w(jpg jpeg gif png)
  end

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

  version :project_list, :if => :is_project_image? do
    process :cropper
  end

  def cropper
    manipulate! do |img|
      if model.crop_x.blank?
        image = MiniMagick::Image.open(current_path)
        model.crop_w = (image[:width] * 0.8).to_i
        model.crop_h = (image[:height] * 0.8).to_i
        model.crop_x = (image[:width] * 0.1).to_i
        model.crop_y = (image[:height] * 0.1).to_i
      end
      img.crop "#{model.crop_w}x#{model.crop_h}+#{model.crop_x}+#{model.crop_y}"
    end 
  end

  def is_project_image?(new_file)
    begin
      return model.model_with_project_image unless model.model_with_project_image.nil?
    rescue
      false
    end
  end
end

I'm missing something or it's a real issue ? ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants