mga*_*mga 2 ruby-on-rails image-processing heroku image-uploading amazon-s3
我正在创建一个Rails应用程序,该应用程序托管在Heroku上,允许用户基于在Web中某处托管的原始JPG(将其视为裁剪调整大小的应用程序)动态生成动画GIF.我试过Paperclip,但是,AFAIK,它不处理动态生成的文件.我正在使用aws-sdk
gem,这是我的控制器的代码片段:
im = Magick::Image.read(@animation.url).first
fr1 = im.crop(@animation.x1,@animation.y1,@animation.width,@animation.height,true)
str1 = fr1.to_blob
fr2 = im.crop(@animation.x2,@animation.y2,@animation.width,@animation.height,true)
str2 = fr2.to_blob
list = Magick::ImageList.new
list.from_blob(str1)
list.from_blob(str2)
list.delay = @animation.delay
list.iterations = 0
Run Code Online (Sandbox Code Playgroud)
这是为了基本创建一个两帧动画.RMagick可以使用以下行在我的开发计算机中生成GIF:
list.write("#{Rails.public_path}/images/" + @animation.filename)
Run Code Online (Sandbox Code Playgroud)
我尝试将list
结构上传到S3:
# upload to Amazon S3
s3 = AWS::S3.new
bucket = s3.buckets['mybucket']
obj = bucket.objects[@animation.filename]
obj.write(:single_request => true, :content_type => 'image/gif', :data => list)
Run Code Online (Sandbox Code Playgroud)
但是我没有一种size
方法RMagick::ImageList
可以用来指定它.我尝试将GIF"预编译"成另一个RMagick::Image
:
anim = Magick::Image.new(@animation.width, @animation.height)
anim.format = "GIF"
list.write(anim)
Run Code Online (Sandbox Code Playgroud)
但Rails崩溃时出现了分段错误:
/path/to/my_controller.rb:103: [BUG] Segmentation fault ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
Abort trap: 6
Run Code Online (Sandbox Code Playgroud)
线103对应于list.write(anim)
.
所以现在我不知道该怎么做,并希望得到任何帮助.
按照@ mga的要求回答他原来的问题......
基于非文件系统的方法非常简单
rm_image = Magick::Image.from_blob(params[:image][:datafile].read)[0]
# [0] because from_blob returns an array
# the blob, presumably, can have multiple images data in it
a_thumbnail = rm_image.resize_to_fit(150, 150)
# just as an example of doing *something* with it before writing
s3_bucket.objects['my_thumbnail.jpg'].write(a_thumbnail.to_blob, {:acl=>:public_read})
Run Code Online (Sandbox Code Playgroud)
瞧!读取上传的文件,使用RMagick进行操作,然后将其写入s3,而不必触及文件系统.
由于这个项目托管在 Heroku 中,我无法使用文件系统,因此我尝试通过代码完成所有操作。我发现 Heroku 确实有一个临时可写文件夹:http://devcenter.heroku.com/articles/read-only-filesystem
这在我的情况下工作得很好,因为在此请求之后我不需要该文件。
结果代码:
im = Magick::Image.read(@animation.url).first
fr1 = im.crop(@animation.x1,@animation.y1,@animation.width,@animation.height,true)
fr2 = im.crop(@animation.x2,@animation.y2,@animation.width,@animation.height,true)
list = Magick::ImageList.new
list << fr1
list << fr2
list.delay = @animation.delay
list.iterations = 0
# gotta packet the file
list.write("#{Rails.root}/tmp/#{@animation.filename}.gif")
# upload to Amazon S3
s3 = AWS::S3.new
bucket = s3.buckets['mybucket']
obj = bucket.objects[@animation.filename]
obj.write(:file => "#{Rails.root}/tmp/#{@animation.filename}.gif")
Run Code Online (Sandbox Code Playgroud)
了解非文件系统写入解决方案是否可行将会很有趣。
归档时间: |
|
查看次数: |
2925 次 |
最近记录: |