在Carrierwave中重新处理不同版本的图像

发布于 2024-10-19 10:13:51 字数 943 浏览 7 评论 0原文

使用 Carrierwave,我使用以下几行创建了 3 个版本的头像 - 一个原始版本、一个小拇指和一个大拇指:

process :resize_to_limit => [400, 400]  

   version :big_thumb do
     process :resize_to_limit => [80, 80]
   end

   version :small_thumb do
     process :resize_to_limit => [50, 50]
   end

我在 AvatarUploader 类中添加了一个附加方法:

def reprocess(x,y,w,h)
        manipulate! do |img|
            img.crop(x.to_i, y.to_i, w.to_i, h.to_i, true) 

            end
resize_to_limit(180,180)  
end

在执行更新后在我的模型中调用该方法:

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_image, :if => :cropping?

def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

private

def reprocess_image
image.reprocess(crop_x,crop_y,crop_w,crop_h)

end

我已设法裁剪并调整原始版本的大小,但我似乎无法随之更新 2 个缩略图。我尝试了几种不同的技术但没有成功。

有什么建议吗?

Using Carrierwave, I created 3 versions of an avatar - an original, a small_thumb and a large_thumb using the following lines:

process :resize_to_limit => [400, 400]  

   version :big_thumb do
     process :resize_to_limit => [80, 80]
   end

   version :small_thumb do
     process :resize_to_limit => [50, 50]
   end

I added an additional method in my AvatarUploader class:

def reprocess(x,y,w,h)
        manipulate! do |img|
            img.crop(x.to_i, y.to_i, w.to_i, h.to_i, true) 

            end
resize_to_limit(180,180)  
end

which is called in my model after an update is performed:

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_image, :if => :cropping?

def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

private

def reprocess_image
image.reprocess(crop_x,crop_y,crop_w,crop_h)

end

I have managed to crop and resize the original version, but I can't seem to update the 2 thumbnails along with it. I tried a few different techniques to no avail.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花伊自在美 2024-10-26 10:13:51

试试

image.recreate_versions!

对不起,在公共汽车上。我无法对此进行解释。

Try

image.recreate_versions!

Sorry, on the bus. I can't expound on that.

給妳壹絲溫柔 2024-10-26 10:13:51

您需要在调用 recreate_versions! 之前调用 image.cache_stored_file!

这很奇怪,因为如果文件被缓存,该方法本身会调用该方法,但由于某种原因它不起作用。

所以那就是这样的:

def reprocess_image
  image.reprocess(crop_x, crop_y, crop_w, crop_h)
  image.cache_stored_file!
  image.recreate_versions!
end

You need to call image.cache_stored_file! before calling recreate_versions!

It's weird because the method itself calls that if the file is cached, but for some reason it wasn't working.

So that would be something like:

def reprocess_image
  image.reprocess(crop_x, crop_y, crop_w, crop_h)
  image.cache_stored_file!
  image.recreate_versions!
end
吻风 2024-10-26 10:13:51

这个方法对我来说是最有帮助的(即使我不使用雾):

https://github.com/rierwaveuploader/rierwave/wiki/How-to:-Recreate-and-reprocess-your-files-stored-on-fog

我在模型上添加了一个重新处理方法,然后在 rake 任务中的每个循环中调用它:

  def reprocess
    begin
      self.process_photo_upload = true
      self.photo.cache_stored_file!
      self.photo.retrieve_from_cache!(photo.cache_name)
      self.photo.recreate_versions!
      self.save!
    rescue => e
      STDERR.puts  "ERROR: MyModel: #{id} -> #{e.to_s}"
    end
  end

Rake:


任务:reprocess_photos => : 环境做
MyModel.all.each{|mm| mm.重新处理}
结尾
PS

:Rails 4.2

This HowTo was most helpful for me (even if I don't use fog):

https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Recreate-and-reprocess-your-files-stored-on-fog

I added a reprocess method on my model and then called it in for each loop in my rake task:

  def reprocess
    begin
      self.process_photo_upload = true
      self.photo.cache_stored_file!
      self.photo.retrieve_from_cache!(photo.cache_name)
      self.photo.recreate_versions!
      self.save!
    rescue => e
      STDERR.puts  "ERROR: MyModel: #{id} -> #{e.to_s}"
    end
  end

Rake:


task :reprocess_photos => :environment do
MyModel.all.each{|mm| mm.reprocess}
end

PS: Rails 4.2

回忆躺在深渊里 2024-10-26 10:13:51

我没有尝试过,但也许会放类似的东西。

def reprocess_image
  image.reprocess(crop_x,crop_y,crop_w,crop_h)
  image.recreate_versions!
end

I haven't tried but maybe putting something like.

def reprocess_image
  image.reprocess(crop_x,crop_y,crop_w,crop_h)
  image.recreate_versions!
end
浴红衣 2024-10-26 10:13:51

检查最新的 RailsCast:

http://railscasts.com/episodes/182-cropping-images-修改

裁剪图像的一个版本后,您可以计算其他版本的裁剪坐标,或者可能更简单,以与原始图像的其他版本相同的长宽比缩小裁剪后的图像

check this latest RailsCast:

http://railscasts.com/episodes/182-cropping-images-revised

after cropping one version of the image, you can then either calculate the cropping coordinates for the other versions, or probably easier, scale down the cropped image with the same aspect ratios as the other versions of the original image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文