加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – RMagick – 将文件转换为另一种格式而不保存到磁盘

发布时间:2020-12-17 03:31:16 所属栏目:百科 来源:网络整理
导读:我正在尝试将文件转换为特定格式,因此我可以将其删除.我知道保存到磁盘并指定扩展名以将其转换为另一种格式的技术,如下所示: img.write("another_filename.jpg") 我想在转换过程中不必触摸磁盘. 还有另外一种方法吗? 解决方法 您可以在调用to_blob时指定格
我正在尝试将文件转换为特定格式,因此我可以将其删除.我知道保存到磁盘并指定扩展名以将其转换为另一种格式的技术,如下所示:

img.write("another_filename.jpg")

我想在转换过程中不必触摸磁盘.

还有另外一种方法吗?

解决方法

您可以在调用to_blob时指定格式.从 fine manual:

to_blob img.to_blob [ { optional arguments } ]-> string

[…]
No required arguments,however you can specify the image format (such as JPEG,PNG,etc.) and depth by calling the format and depth attributes,as well as other Image::Info attributes as appropriate,in a block associated with the method.

所以你可以这样说:

png_bytes = img.to_blob { |attrs| attrs.format = 'PNG' }

是的,to_blob的界面有点奇怪,但奇怪的界面只是使用ImageMagick的乐趣的一部分.

您也可以在调用to_blob之前使用format= method:

img.format = 'PNG'
png_bytes  = img.to_blob

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读