sorl 缩略图 + Django 生产中的问题
我正在 Django 中使用 sorl 缩略图。在我的本地设置中它工作正常,但在生产中未制作缩略图。
我的代码如下所示:
{% load thumbnail %}
{% thumbnail up.image "32x32" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% empty %}
<img src="{{ MEDIA_URL }}/images/missing_small.png" alt="" title="" />
{% endthumbnail %}
我启用了日志记录,跟踪如下所示:
Traceback (most recent call last):
[...]
File "/usr/local/lib/python2.6/dist-packages/PIL/ImageFile.py", line
215, in load
raise_ioerror(e)
File "/usr/local/lib/python2.6/dist-packages/PIL/ImageFile.py", line
52, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
该错误不是很有帮助,因为文件在那里并且所有人都可读。我不确定如何获得更明确的错误,或者尝试修复什么。
更令人困惑的是,它使用 manage.py shell
工作
In [1]: from sorl.thumbnail import get_thumbnail
In [2]: im = get_thumbnail('/myproject/static/images/user_profiles/1/11-20-2010-2_5.jpg',
'32x32', crop='center' )
In [3]: im
Out[3]: <sorl.thumbnail.images.ImageFile object at 0x29fe090>
In [4]: im.url
Out[4]: 'http://example.com/cache/ff/31/ff318b4a995ff345d1d48e79b67ec62b.jpg'
它制作了缩略图,只是不会通过模板代码制作缩略图。
有人吗?
I'm using sorl thumbnail with Django. On my local setup it works fine, but in production the thumbnails are not made.
My code looks like this:
{% load thumbnail %}
{% thumbnail up.image "32x32" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% empty %}
<img src="{{ MEDIA_URL }}/images/missing_small.png" alt="" title="" />
{% endthumbnail %}
I enabled logging and the trace looks like this:
Traceback (most recent call last):
[...]
File "/usr/local/lib/python2.6/dist-packages/PIL/ImageFile.py", line
215, in load
raise_ioerror(e)
File "/usr/local/lib/python2.6/dist-packages/PIL/ImageFile.py", line
52, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
The error isn't very helpful since the file is there and is readable by all. I'm not sure how to get a more explicit error, or what to try and fix.
And then more baffling is the fact that it works using manage.py shell
In [1]: from sorl.thumbnail import get_thumbnail
In [2]: im = get_thumbnail('/myproject/static/images/user_profiles/1/11-20-2010-2_5.jpg',
'32x32', crop='center' )
In [3]: im
Out[3]: <sorl.thumbnail.images.ImageFile object at 0x29fe090>
In [4]: im.url
Out[4]: 'http://example.com/cache/ff/31/ff318b4a995ff345d1d48e79b67ec62b.jpg'
It made the thumbnail, just won't make one via the template code.
Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于像我这样的懒人来说,解决方案是尝试设置文件中的另一个 THUMBNAIL_ENGINE 。
我面临着同样的问题。由于这显然是由于图像库的问题造成的,因此对于我的情况,我决定简单地使用不同的图像库 - 并且它有效。目前有 3 个可能的图像库:PIL(默认)、Pgmagick 和 ImageMagick/GraphicsMagick。详细信息请参见此处。
当然,必须在服务器上安装替代库之一。否则,这个解决方案将不起作用。
A solutions for lazy people like me is to try another THUMBNAIL_ENGINE in the settings file.
I was facing the same problem. As it obviously results from problems with the image library, for my case I've decided to simply use a different image library - and it worked. Currently there are 3 possible image libraries: PIL (default), Pgmagick, and ImageMagick/GraphicsMagick. Details are explained here.
Of course, one of the alternative libraries must be installed on the server. Otherwiese, this solution won't work.
你的syncdb完成了吗?
运行此命令来检查: python manage.py 缩略图清理
Have you done your syncdb?
Run this to check : python manage.py thumbnail cleanup
简短回答:卸载除 6.2 之外的所有版本的 ligjpeg 开发文件。卸载所有版本的
PIL
。使用pip
将最新的sorl.thumbnail
和PIL-1.1.7
安装到 virtualenv 中,以便PIL
进行编译libjpeg6.2
。详细答案:
我在我维护的网站上遇到了很多麻烦 - 结果是由于
PIL
、libjpeg
和sorl
- 有人安装了所有这些版本的很多版本。我必须将其精简为PIL1.1.7
、libjpeg6.2
和sorl.thumbnail 11.9
的单个安装,这就是我必须做的(这假设你正在使用 virtualenv。如果你没有,那么现在是开始的好时机):
哦等等,它也是通过 easy_install 安装的,所以我还必须从site-packages:
现在我从我的 virtualenv 中卸载 PIL
,我正在使用的 pip 版本似乎有一个错误 - 我必须删除 virtualenv 构建目录中的内容,否则 pip 不会安装更高版本,
现在我必须删除我不想要的 libjpeg 版本...您可能应该这样做
...或者任何必要的咒语。当然,如果您还有其他依赖于该版本的
libjpeg
的东西,那么您的情况就会很有趣。我确信您可以安装PIL
并针对特定版本的libjpeg
进行构建。那么也许你可以让libjpeg8
正常工作 - 如果你这样做,请回复。在我的服务器残骸上,
libjpeg7
和libjpeg8
开发文件都是从源代码安装的(除了安装的包管理器版本 6.2 - 搞笑吧?),所以我不得不下载libjpeg7
和libjpeg8
的源代码,运行./configure
,然后运行 ./make卸载
。/usr/local/lib/
中还剩下一些库,所以我必须执行rm /usr/local/lib/libjpeg*
。我可能应该只做最后一点,但想要彻底。
检查您的系统上是否没有安装其他
PIL
或libjpeg
或安装残留。现在你可以重新开始......
在这个破旧的 CentOS 机器上,它的版本是 6.2,它似乎与
PIL1.1.7
配合得最好。在最近的 Debian/Ubuntu 上,您现在必须在我的 virtualenv run
restart webserver 中运行。幸福。
Short answer: uninstall all versions of
ligjpeg
dev files except 6.2. Uninstall all versions ofPIL
. Install an recentsorl.thumbnail
andPIL-1.1.7
into a virtualenv usingpip
so thatPIL
compiles againstlibjpeg6.2
.Detailed answer:
I had a lot of trouble with this on a site I mantain - turns it was due to version conflicts between
PIL
,libjpeg
andsorl
- someone had installed lots of versions of all of them. I had to strip it down to a single install ofPIL1.1.7
,libjpeg6.2
andsorl.thumbnail 11.9
Here's what I had to do (this assumes you're using virtualenv. If you're not, well now's a good time to start):
oh wait, it was also installed via easy_install so I also have to remove PIL from site-packages:
Now I uninstall PIL from my virtualenv
there seems to be a bug in the version of pip i'm using - I have to remove stuff in the virtualenv build dir or pip won't install a later version
now I have to remove the versions of libjpeg that I don't want... You should probably do
... or whatever incantation is necessary. Of course if you have other stuff that depends on that version of
libjpeg
then you're in an interesting situation. I'm sure you can getPIL
to install and build against a specific version oflibjpeg
. Then maybe you can getlibjpeg8
to work ok with this - please reply if you do.On my trainwreck of a server both
libjpeg7
andlibjpeg8
dev files had been installed from source (in addition to the package manager installed version 6.2 - hilarious eh?) so I had to download the source for bothlibjpeg7
andlibjpeg8
, run./configure
and then./make uninstall
. There were still some libs left in/usr/local/lib/
so I had to dorm /usr/local/lib/libjpeg*
.I should have probably just done that last bit but wanted to be thorough.
Check that there are no other installs or remnants of installs of
PIL
orlibjpeg
on your system.Now you can start afresh...
On this battered old CentOS box that gets version 6.2 which seems to work best with
PIL1.1.7
. On recent Debian/Ubuntu you'd have to runnow in my virtualenv run
restart webserver. happiness.
尝试了一切。最终用更新的 Linux 发行版解决了这个问题。在 Ubuntu 10.04 上不起作用,但之后它对我、11.04、11.10 等都工作得很好。
Tried everything. Ended up solving itself with a newer Linux distro. Wouldn't work on Ubuntu 10.04 but after that it works fine for me, 11.04, 11.10, etc.