sorl 缩略图 + Django 生产中的问题

发布于 2024-11-15 01:54:10 字数 1273 浏览 3 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(4

浅忆流年 2024-11-22 01:54:10

对于像我这样的懒人来说,解决方案是尝试设置文件中的另一个 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.

来世叙缘 2024-11-22 01:54:10

你的syncdb完成了吗?

运行此命令来检查: python manage.py 缩略图清理

Have you done your syncdb?

Run this to check : python manage.py thumbnail cleanup

被你宠の有点坏 2024-11-22 01:54:10

简短回答:卸载除 6.2 之外的所有版本的 ligjpeg 开发文件。卸载所有版本的 PIL。使用 pip 将最新的 sorl.thumbnailPIL-1.1.7 安装到 virtualenv 中,以便 PIL 进行编译libjpeg6.2

详细答案:

我在我维护的网站上遇到了很多麻烦 - 结果是由于 PILlibjpegsorl - 有人安装了所有这些版本的很多版本。我必须将其精简为 PIL1.1.7libjpeg6.2sorl.thumbnail 11.9 的单个安装,

这就是我必须做的(这假设你正在使用 virtualenv。如果你没有,那么现在是开始的好时机):

yum erase python-imaging

哦等等,它也是通过 easy_install 安装的,所以我还必须从site-packages:

easy_install -mxN PIL
rm -rf  /usr/lib/python2.6/site-packages/PIL /usr/lib/python2.6/site-packages/PIL.pth

现在我从我的 virtualenv 中卸载 PIL

pip uninstall PIL

,我正在使用的 pip 版本似乎有一个错误 - 我必须删除 virtualenv 构建目录中的内容,否则 pip 不会安装更高版本,

rm -rf /path/to/virtualenv/build/PIL/

现在我必须删除我不想要的 libjpeg 版本...您可能应该这样做

apt-get remove libjpeg8 libjpeg8-dev

...或者任何必要的咒语。当然,如果您还有其他依赖于该版本的 libjpeg 的东西,那么您的情况就会很有趣。我确信您可以安装 PIL 并针对特定版本的 libjpeg 进行构建。那么也许你可以让 libjpeg8 正常工作 - 如果你这样做,请回复。

在我的服务器残骸上,libjpeg7libjpeg8 开发文件都是从源代码安装的(除了安装的包管理器版本 6.2 - 搞笑吧?),所以我不得不下载 libjpeg7libjpeg8 的源代码,运行 ./configure,然后运行 ​​./make卸载/usr/local/lib/ 中还剩下一些库,所以我必须执行rm /usr/local/lib/libjpeg*

我可能应该只做最后一点,但想要彻底。

检查您的系统上是否没有安装其他 PILlibjpeg 或安装残留。

现在你可以重新开始......

yum install libjpeg-devel 

在这个破旧的 CentOS 机器上,它的版本是 6.2,它似乎与 PIL1.1.7 配合得最好。在最近的 Debian/Ubuntu 上,您现在必须

apt-get install libjpeg62-dev

在我的 virtualenv run

pip install PIL sorl-thumbnail

restart webserver 中运行。幸福。

Short answer: uninstall all versions of ligjpeg dev files except 6.2. Uninstall all versions of PIL. Install an recent sorl.thumbnail and PIL-1.1.7 into a virtualenv using pip so that PIL compiles against libjpeg6.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 and sorl - someone had installed lots of versions of all of them. I had to strip it down to a single install of PIL1.1.7, libjpeg6.2 and sorl.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):

yum erase python-imaging

oh wait, it was also installed via easy_install so I also have to remove PIL from site-packages:

easy_install -mxN PIL
rm -rf  /usr/lib/python2.6/site-packages/PIL /usr/lib/python2.6/site-packages/PIL.pth

Now I uninstall PIL from my virtualenv

pip uninstall PIL

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

rm -rf /path/to/virtualenv/build/PIL/

now I have to remove the versions of libjpeg that I don't want... You should probably do

apt-get remove libjpeg8 libjpeg8-dev

... 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 get PIL to install and build against a specific version of libjpeg. Then maybe you can get libjpeg8 to work ok with this - please reply if you do.

On my trainwreck of a server both libjpeg7 and libjpeg8 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 both libjpeg7 and libjpeg8, run ./configure and then ./make uninstall. There were still some libs left in /usr/local/lib/ so I had to do rm /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 or libjpeg on your system.

Now you can start afresh...

yum install libjpeg-devel 

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 run

apt-get install libjpeg62-dev

now in my virtualenv run

pip install PIL sorl-thumbnail

restart webserver. happiness.

梦境 2024-11-22 01:54:10

尝试了一切。最终用更新的 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.

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