不同版本有不同的文件扩展名

发布于 2024-10-21 21:24:45 字数 640 浏览 1 评论 0原文

我正在使用 Carrierwave 上传图像。我需要我的主图像版本 保留其原始格式,但要转换其他版本 到 gif。

目前我正在做这样的事情:

 def filename
   change_ext_to_gif(super)
 end

 def change_ext_to_gif(ext)
   ext.chomp(File.extname(ext)) + ".gif"
 end

 version :preview do
   process :resize_to_fill => [60, 60]
   process :convert => "gif"
 end

 version :full do
   process :resize_to_limit => [320, 320]
   process :convert => "gif"
 end

 version :mobile do
   process :resize_to_limit => [72, 96]
   process :convert => "gif"
 end

当然,这也会更改我的原始文件的扩展名。是 有什么办法解决这个问题吗?我想我需要重写一些方法 在版本的块中。但我无法弄清楚它们(我 尝试覆盖文件名和网址,这有帮助,但会阻止版本 文件被删除)。

I am using carrierwave to upload images. I need my main image version
to remain in its original format, but other versions to be converted
to gif.

At the moment i am doing something like this:

 def filename
   change_ext_to_gif(super)
 end

 def change_ext_to_gif(ext)
   ext.chomp(File.extname(ext)) + ".gif"
 end

 version :preview do
   process :resize_to_fill => [60, 60]
   process :convert => "gif"
 end

 version :full do
   process :resize_to_limit => [320, 320]
   process :convert => "gif"
 end

 version :mobile do
   process :resize_to_limit => [72, 96]
   process :convert => "gif"
 end

Of course, this changes extension of my original file as well. Is
there any way to solve this? I guess i need to override some methods
in the version's blocks. But i was not able to figure out them (I
tried overriding filename and url this helps but prevents version`s
files from being deleted).

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

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

发布评论

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

评论(1

不念旧人 2024-10-28 21:24:45

您可以像这样修改每个版本使用的文件名:

 version :mobile do
   process :resize_to_limit => [72, 96]
   process :convert => "gif"
   def full_filename(for_file = model.logo.file)
     "fiename here"
   end
 end

因此,只需保留原始文件名,然后按版本更改它即可。维基百科上还有更多示例:

https://github.com/jnicklas/rierwave/wiki/How-To%3A-Move-version-name-to-end-of-filename%2C-instead-of-front

You can ovvierde the filename that gets used per version like so:

 version :mobile do
   process :resize_to_limit => [72, 96]
   process :convert => "gif"
   def full_filename(for_file = model.logo.file)
     "fiename here"
   end
 end

So just leave the original filename as you'd like and then alter it per version. There are further examples on the wiki here:

https://github.com/jnicklas/carrierwave/wiki/How-To%3A-Move-version-name-to-end-of-filename%2C-instead-of-front

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