3

私は現在、インターネット上の他のシステムと同様のカスタム画像トリミングシステムをコーディングしようとしています。このシステムでは、ユーザーがトリミング領域を選択し、それに応じて画像をトリミングすることができます。アプリケーションはRailsにあり、ファイルを保存するためにAmazonS3でPaperclipを使用しています。RMagickにS3からファイルを適切に切り抜いてもらうのに、私は多くの問題を抱えています。現在のコードは次のとおりです(機能しません):

   if params[:width].to_i > 0 and params[:height].to_i > 0 then
      photo = Photo.find(params[:id])
      image_data = Net::HTTP.get_response(URI.parse(photo.photo.url(:big))).body
      orig_img = Magick::ImageList.new
      orig_img.from_blob(image_data)

      args = [params[:x1].to_i, params[:y1].to_i, params[:width].to_i, params[:height].to_i]
      orig_img.crop!(*args)
      photo.update_attributes({:photo => orig_img.to_blob})

      photo.photo.reprocess!
      photo.save
    end

主な問題は、トリミングされた画像がペーパークリップを介してS3にアップロードされないため、適切にトリミングされないことです。誰かが以前にペーパークリップでこのようなことを試みたことがありますか?これは不可能かもしれませんが、どんな助けでも大歓迎です。

4

3 に答える 3

0

You could define some attr_accessors on the model and set them in the controller action before creating the actual file. The below should work ..

class Poodle < ActiveRecord::Base
  has_attached_file :avatar, :styles => Proc.new{|a| a.instance.get_styles}
  attr_accessor :width, :height


  def get_styles(style = "medium")
    return {style.to_sym => [self.width, self.height].join("x") + ">" }
  end
 end
于 2009-03-06T23:40:14.067 に答える