Adding watermarks to images with mini_magick error 256
April 8, 2009 – 10:56 amI was attempting to watermark an uploaded image using attachment_fu for uploads and the mini_magick gem for processing. I received an odd little error that Google doesn’t seem to have many answers for. That error is Error 256. This is what I was doing:
after_attachment_saved do |image|
if image.thumbnail.nil?
img = MiniMagick::Image.from_file(image.full_filename(:large))
img.combine_options do |c|
c.gravity 'SouthWest'
c.draw "image Over 0,0 0,0 "images/logo_watermark.png""
end
img.write(image.full_filename(:large))
end
end
Looks sound, right? Should work, right? Well, oddly enough it didn’t and I received the error. I know others have used that exact method in the past and it’s worked fine for them, but for those of us that receive the ambiguous error I’ve got the answer.
Command line.
Mini_magick is a simple Ruby wrapper for ImageMagick anyway, so let’s just bypass it altogether and do it ourselves. The code above uses ImageMagick’s mogrify, but since we’ve got better control we should go ahead and use ImageMagick’s composite which gives us a better looking watermark on images. The original code turns into this:
after_attachment_saved do |image|
if image.thumbnail.nil?
img = image.full_filename(:large)
system("composite -gravity SouthWest -watermark 10.0 -dissolve 75% 'images/logo_watermark.png' "#{img}" "#{img}"")
end
end
There you have it.
UPDATE: The HTML “code” tag is parsing the escaped quotes. Make sure you escape the quotes around “#{img}” so they turn into \”#{img}\”