RMagick/ImageMagick 错误“此图像格式没有解码委托”使用 Ruby 临时文件
我在使用 RMagick/ImageMagick 打开通过 Rails 表单上传的文件时遇到问题。我相信这是因为 Rails Tempfile 行为是消除文件扩展名。根据我的阅读,ImageMagick 应该更喜欢使用幻数而不是文件扩展名来检测图像文件格式,但在我的安装中似乎并非如此。我正在这样做:
img = Magick::Image.read(full_size_image.path)
其中 full_size_image 是从上传表单控制器返回的 Tempfile 对象,这会产生如下错误:
此图像格式“/var/folders/uI/uIsldaxoHkiXKWYFuTWs4++++TI/-Tmp-/RackMultipart20101031-10144-1hiwlw-0”没有解码委托@ error/constitute.c/ReadImage/532
这发生在 Mac OS 上X 10.6 与 Rails 2.3.8、RMagick 2.13.1 和 ImageMagick 6.6.5-5。我已经确认 ImageMagick 可以成功转换我用来测试上传表单的文件,并且临时文件路径/文件实际上存在/可读。
我怀疑 a) 我的 ImageMagick 安装中的某些内容以某种方式阻止它使用幻数来检测文件类型(?? - 让 ImageMagick 从源代码在 Snow Leopard 上工作本身就是一次冒险)或 b) 在某些情况下会出现这种情况不起作用,我需要以某种方式使 Tempfile 保留原始文件扩展名。
我知道我可以从 Tempfile 对象中获取原始文件名,但我还没有找到重命名文件的方法。我想我可以在使用 File 对象的地方进行一些组装,并以某种方式创建带有原始扩展名的 Tempfile 副本,但这似乎不合时宜。解决这个问题的正确方法是什么?
感谢您的帮助。
I'm having a problem using RMagick/ImageMagick to open a file uploaded through a Rails form. I believe this is due to the fact that Rails Tempfile behavior is to eliminate the file extension. From what I've read, ImageMagick should prefer to use magic number rather than file extension to detect the image file format, but that doesn't seem to be the case in my installation. I'm doing this:
img = Magick::Image.read(full_size_image.path)
where full_size_image is the Tempfile object returned from the upload form controller, which creates an error like:
no decode delegate for this image format `/var/folders/uI/uIsldaxoHkiXKWYFuTWs4++++TI/-Tmp-/RackMultipart20101031-10144-1hiwlw-0' @ error/constitute.c/ReadImage/532
This is happening on Mac OS X 10.6 with Rails 2.3.8, RMagick 2.13.1 and ImageMagick 6.6.5-5. I've confirmed that ImageMagick can convert the file I've been using to test through the upload form successfully, and that the tempfile path/file actually exists/is readable.
I suspect that either a) something in my ImageMagick installation is somehow preventing it from using magic number to detect the file type (?? - getting ImageMagick working on Snow Leopard from source was an adventure itself) or b) there are just cases where this doesn't work and I somehow need to make Tempfile preserve the original file extension.
I know that I can get the original file name from the Tempfile object, but I haven't found a way to rename the file. I suppose I could make some kludge where I use a File object and somehow create a copy of the Tempfile with the original extension on it, but that just seems unseemly. What's the right way to solve this problem?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题 - DYLD_LIBRARY_PATH 环境变量在我的 shell 中正确设置,我在 shell 中测试 Convert 的命令行版本,但在运行我的 Mongrel 服务器时未正确设置。我修好这个问题后,它就开始工作了。
I figured out the problem - the DYLD_LIBRARY_PATH environment variable was set correctly in my shell where I was testing the command line version of convert but was not set correctly where my Mongrel server was running. After I fixed this, it started working.