sorl thumbnail + django issues in production

2019-07-01 09:29发布

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?

4条回答
劳资没心,怎么记你
2楼-- · 2019-07-01 09:31

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-07-01 09:33

Have you done your syncdb?

Run this to check : python manage.py thumbnail cleanup

查看更多
姐就是有狂的资本
4楼-- · 2019-07-01 09:38

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.

查看更多
Emotional °昔
5楼-- · 2019-07-01 09:43

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.

查看更多
登录 后发表回答