Carrierwave 未使用 rmagick 调整版本大小

发布于 2024-12-22 23:48:56 字数 557 浏览 0 评论 0原文

我正在尝试使用载波来管理图像。我的问题是我上传的图像的所有版本都已创建,但均为完整尺寸。代码:

class TechnologyImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Process files as they are uploaded:
  #process :scale => [100, 100]

  version :small do
     process :resize_to_fit => [25,25]
  end
  version :medium do
     process :resize_to_fit => [50,50]
  end
end

所有图片版本均显示为原始上传的大小。

I am trying to use carrierwave to manage images. My problem is that all versions of the images I upload are created, but at the full size. Code:

class TechnologyImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Process files as they are uploaded:
  #process :scale => [100, 100]

  version :small do
     process :resize_to_fit => [25,25]
  end
  version :medium do
     process :resize_to_fit => [50,50]
  end
end

All image versions are displayed as the size of the original upload.

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

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

发布评论

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

评论(3

你的呼吸 2024-12-29 23:48:57

我的应用程序也有类似的问题。
虽然我想我发现,在使用版本时,
将每个“进程”设置为一个版本很有帮助......
否则我注意到有些方法正在“覆盖”其他方法......
奇怪的。

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  if Rails.env == "production"
    storage :aws
  else
    storage :file
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  #this is the 'first' process, 'while you upload', the one that seems to be an issue
  process resize_to_fill: [228, 250]


  version :industry do
    process resize_to_fit: [228, 250]
  end

  version :portrait do
    process resize_to_fill: [360, 200]
  end

  version :modal do
    process resize_to_fill: [330, 300]
  end

end

然后将变成 ::

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  if Rails.env == "production"
    storage :aws
  else
    storage :file
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  #i put everything as a version and it sorts the problem out..

  version :base do
    process resize_to_fill: [228, 250]
  end

  version :industry do
    process resize_to_fit: [228, 250]
  end

  version :portrait do
    process resize_to_fill: [360, 200]
  end

  version :modal do
    process resize_to_fill: [330, 300]
  end

end

有用

我希望这是“真实的”并且对其他用户Marin

I have a somehow similar problem with my app.
Though i think i figured out that, when using versions,
it is helpful to set every 'process' as a version...
Otherwise i've noticed that some methods were 'overwriting' others...
Bizarre.

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  if Rails.env == "production"
    storage :aws
  else
    storage :file
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  #this is the 'first' process, 'while you upload', the one that seems to be an issue
  process resize_to_fill: [228, 250]


  version :industry do
    process resize_to_fit: [228, 250]
  end

  version :portrait do
    process resize_to_fill: [360, 200]
  end

  version :modal do
    process resize_to_fill: [330, 300]
  end

end

which would then become ::

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  if Rails.env == "production"
    storage :aws
  else
    storage :file
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  #i put everything as a version and it sorts the problem out..

  version :base do
    process resize_to_fill: [228, 250]
  end

  version :industry do
    process resize_to_fit: [228, 250]
  end

  version :portrait do
    process resize_to_fill: [360, 200]
  end

  version :modal do
    process resize_to_fill: [330, 300]
  end

end

I hope this is 'true' and will be useful to other users

Marin

私野 2024-12-29 23:48:56

不确定你们是否因为与我相同的原因而遇到这个问题,但也许吧。
我需要将上传的文件移动到私人文件夹中,我相信您也这样做了。

上传后,我想删除缓存,我做了什么:

after :store, delete_cache

def delete_cache( new_file )
  FileUtils.rm_rf %{#{Rails.root.to_s}/public/uploads}
end

这样做的问题是,创建版本后,将触发 after :store ,因此应用程序会删除缓存目录,因此其他版本方法无法使用不再阅读该文件了。

对我来说,临时解决方案是将cache_dir 移动到私人文件夹。我稍后需要以不同的方式清空什么,我需要弄清楚,所以:

def cache_dir
  %{#{Rails.root.to_s}/tmp/uploads}
end

Not sure if you guys have this issue because of the same cause as I, but maybe.
I needed to move the uploaded files to a private folder, witch I'm sure you did as well.

After my upload I wanted to delete the cache what I did with:

after :store, delete_cache

def delete_cache( new_file )
  FileUtils.rm_rf %{#{Rails.root.to_s}/public/uploads}
end

The problem with this, is that after a version is created, the after :store will be triggered, so the app removes the cache directory, so the other version methods couldn't read that file anymore.

For me a temporal solution was to move the cache_dir to a private folder. What I need to empty later in a different way, that i'll need to figure out, so:

def cache_dir
  %{#{Rails.root.to_s}/tmp/uploads}
end
梦晓ヶ微光ヅ倾城 2024-12-29 23:48:56

我的问题的解决方案是 Rails 环境在服务器上被命名为“staging”,在 Mac 上被命名为“development”。

文件 config/initializers/rierwave.rb 中的第 4 行(第 4 行)禁用名为“staging”的环境的载波处理。

为了使处理工作,我需要启用这一行:

config.enable_processing = true

The solution to my problem was that the rails environment was named 'staging' on the server and 'development' on the mac.

Line 4 at file config/initializers/carrierwave.rb (line 4) disables the carrierwave processing for environments named 'staging'.

In order to make the processing work, I needed to enable with this line:

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