为什么Carrierwave和MiniMagick图像不能正确复合?

时间:2013-11-22 21:29:56

标签: carrierwave rackspace-cloud fog minimagick

我正在使用带有雾(1.18.0),mini_magick(3.6.0)和Rackspace CloudFiles的carrierwave(0.9.0)。我想合成图像。

我有两种型号:产品和电子邮件。在产品上传器中,我正在尝试创建一个循环遍历所有电子邮件的流程,并在每个email.background_image之上复合产品图像。我还想控制这个图像的名称,这是:

"#{email.name}_#{product.product_number}.jpg"

我已将我的流程和方法包括在内。将文件中的测试结果上传到名为“email_123.png”的云文件和原始图像。该名称目前不包含电子邮件的名称,图像不合成,我只收到原始的回复。有什么明显我在这里不见了?谢谢!

version :email do
  process :email_composite

  def email_composite
    Email.all.each do |email|
      email_image = email.secondary.url
      email_name = email.name
      merge(email_name, email_image)
    end
  end

  def merge(email_name, email_image)
    manipulate! do |img|
      @product = Product.find(model.id)
      email_image = ::MiniMagick::Image.open(email_image)

      img = email_image.composite(img, "jpg") do |c|
        c.gravity "Center"
      end
      # img = yield(img) if block_given?
      img = img.name "#{email_name}_#{@product.product_number}.png"

      img
    end
  end
end

已更新

首先,我想感谢您的全面答复,所涉及的努力量是显而易见的,并且值得赞赏。

在尝试实施此解决方案时,我还有一些问题。 在#compose方法中你有:

email_image = ::MiniMagick::Image.open(email_image)

应该是:

email_image = ::MiniMagick::Image.open(email_image_url)

我假设这看到下面包含的define_method调用(顺便说一下这很棒 - 我不知道你能做到这一点):

define_method(:email_image_url) { email.secondary.url }

获取未定义方法“versions_for”后,我切换了#versions_for方法,因为我总是希望所有电子邮件都有辅助版本。

在之前

versions_for
def self.versions_for emails
  reset_versions!
  email.find_each { |e| version_for_email(e) }
end
更改后的

versions_for

def self.versions_for
  reset_versions!
  Email.all.find_each { |e| version_for_email(e) }
end

这一改变让我摆脱了错误。

所有这一切的最终结果是一切看起来好像它可以工作,我可以无误地上传图像,但现在我没有电子邮件版本的结果图像。我仍然得到我的其他版本像拇指等任何想法可能导致这个?再次感谢您迄今为止提供的所有帮助。

1 个答案:

答案 0 :(得分:2)

在同一版本中多次调用manipulate!会将其块中的更改应用于相同的图像;你想要的是创建许多版本,一个版本对应于每个Email。这很棘手,因为CarrierWave真的希望你在代码中有一小组静态定义的版本;它实际上构建了一个新的匿名Uploader类来处理每个版本。

我们可以欺骗它动态构建版本,但它非常难看。另外,我们必须小心不要继续引用陈旧的上传类,否则我们会无休止地累积类并最终耗尽内存!

# in ProductUploader:

# CarrierWave stores its version classes in two places:
#
# 1. In a @versions hash, stored within the class; and
# 2. As a constant in your uploader, with a name based on its #object_id.
#
# We have to clean out both of them to be sure old versions get garbage
# collected!
def self.reset_versions!
  versions.keys.select { |k| k =~ /^email_/ }.each do |k|
    u = versions.delete(k)[:uploader]
    remove_const("Uploader#{u.object_id}".gsub('-', '_'))
  end
end

def self.version_name_for email
  "email_#{email.name}".to_sym
end

# Dynamically generate the +version+ that corresponds to the image composed
# with the image from +email+.
def self.version_for_email email
  version(version_name_for(email)) do
    process :compose

    # Use +define_method+ here so that +email+ in the block refers to the
    # argument "email" from the outer scope.
    define_method(:email_image_url) { email.secondary.url }

    # Use the same trick to override the filename for this version.
    define_method(:filename) { "#{email_name}_#{model.product_number}.png" }

    # Compose +email_image+ on top of the product image.
    def compose
      manipulate! do |img|
        email_image = ::MiniMagick::Image.open(email_image_url)

        # Do the actual composition.
        img = email_image.composite(img, "jpg") do |c|
          c.gravity "Center"
        end

        img
      end
    end
  end
end

# Clear out any preexisting versions and generate new ones based on the
# contents of +emails+.
def self.versions_for emails
  reset_versions!
  email.find_each { |e| version_for_email(e) }
end

# After you call Product.versions_for(emails), you can use this to fetch
# the version for a specific email:
def version_for_email email
  versions[self.class.version_name_for(email)]
end

要使用此功能,请务必致电Product.versions_for(Email.all)(可能在before_filter中),然后,您可以使用@p.product.version_for(email)访问特定电子邮件的版本:

# in your controller:

def action
  # Recreate a +version+ for each email.
  # If you don't want to overlay *every* email's image, you can also pass a
  # subset here.
  ProductUploader.versions_for(Email.all)

  # Upload a file and its versions to Cloud Files...
  @p = Product.find(params[:id])
  @p.product = File.open(Rails.root.join('clouds.jpg'), 'r')
  @p.save!
end

在您的观看中,您可以像往常一样使用url或任何其他帮助:

# in a view:

<% Email.all.find_each do |email| %>
  <%= image_tag @p.product.version_for_email(email).url %>
<% end %>