Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create paperclip attachment from rmagick image

I have a problem to find a way to save an image created with RMagick in a paperclip attachment.

imageList = Magick::ImageList.new
imageList.new("images/apple.gif", "images/overlay.png")
...
picture = imageList.flatten_images

I am in a model that have an attached file

has_attached_file :picture, :url => ..., :path => ...

and i just want my image returned by imageList.flatten_images to be saved as the picture of my model.

Does anyone know how to do it easily please?

thanks

like image 241
guts Avatar asked Oct 27 '10 16:10

guts


2 Answers

Let's see if that's what you need

picture = imageList.flatten_images
file = Tempfile.new('my_picture.jpg')
picture.write(file.path)
YourModel.create(:picture => file, ...)

Change YourModel with the model you are using...

like image 145
jordinl Avatar answered Sep 26 '22 20:09

jordinl


You should force the extension on TempFile.new; in this case I pull the original image from S3 or some such, this is happening in the model of course:

orig_img = Magick::ImageList.new(self.photo.url(:original))

#process image here

# Force extension with array form:
file = Tempfile.new(['processed','.jpg'])
orig_img.write(file.path)
self.photo = file
self.save
like image 29
LucasLMartini Avatar answered Sep 22 '22 20:09

LucasLMartini