-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when trying to access width/height after url in templates #350
Comments
I have encountered the same issue with ImageKit 3.3, Python 2.7.5 and Django 1.9. I haven't been able to trace it completely, but it seems that after the cache creation the File object that gets passed on points to a temporary file (in /tmp, in my case) instead of the newly created cache file. This temporary file gets deleted sometime during the process which triggers the exception. |
@Terr, exactly! The simple workaround is to use |
I investigated the issue and found the line causing the problem. When image is generated then temporary file is created and attached to the I will try to see a better way of generating thumbnails that will fix the issue but if possible without extra calling the storage backend for performance reason. For now you can use the workaround with putting the |
Any updates on this? I've encountered the same thing continually running into the after upgrading to imagekit 3.3:
Without much luck for a workaround. In my case, I'm trying to generate two thumbnails from a single image. When setting the models |
To further elaborate on the issue, my model roughly looks like the following: from imagekit.models.fields import ProcessedImageField
class MyModel(models.Model):
original = ProcessedImageField(upload_to='somepath',
processors=[XLargeImageProcessor()],
blank=True, null=True)
thumbnail = ProcessedImageField(
upload_to='somepath',
processors=[ResizeToFit(width=500,
height=200,
upscale=True)],
options={'quality': 90},
blank=True,
null=True)
thumbnail_square = ProcessedImageField(
upload_to='somepath',
processors=[ResizeToFill(200, 200)],
blank=True,
null=True)
def save(self, *args, **kwargs):
if not self.thumbnail:
self.thumbnail = self.original.file
if self.thumbnail and \
hasattr(self.thumbnail, 'file') and \
not self.thumbnail_square:
self.thumbnail_square = self.thumbnail.file
super(MyModel, self).save(*args, **kwargs) When saving, that's when I see the:
This worked without issues with django 1.8.8, imagekit 3.2.7. |
Ok I will check this. Thanks for the info. |
No problem. I'm in the middle of an upgrade right now so let me know if I can help in any way. Thanks. |
@troygrosfield Your code works with Django==1.8.8 and django-imagekit==3.2.7 and does not work with same Django but django-imagekit==3.3? @a1tus Can you confirm that your code works with django-imagekit==3.2.7 and not with 3.3 or it does not work with 3.2.7 also? |
I was mistaken, the error actually happened with the change between django-imagekit 3.2.6 and 3.2.7. I just assumed it was the 3.3 version because I jumped from 3.2.6 to 3.3. Here's the tests that show the issue: Models.py from imagekit.models.fields import ProcessedImageField
from pilkit.processors.resize import ResizeToFill
from pilkit.processors.resize import ResizeToFit
class MyModel(models.Model):
original = ProcessedImageField(blank=True, null=True)
thumbnail = ProcessedImageField(
processors=[ResizeToFit(width=500,
height=200,
upscale=True)],
options={'quality': 90},
blank=True,
null=True)
thumbnail_square = ProcessedImageField(
processors=[ResizeToFill(200, 200)],
blank=True,
null=True)
def save(self, *args, **kwargs):
if not self.thumbnail:
self.thumbnail = self.original.file
if self.thumbnail and \
hasattr(self.thumbnail, 'file') and \
not self.thumbnail_square:
self.thumbnail_square = self.original.file
super(MyModel, self).save(*args, **kwargs) Tests class ModelTests(TestCase):
def test_my_model(self):
img = open('/Users/troy/tmp/test-pic.jpg', 'rb')
spec = ImageSpec(source=img)
with NamedTemporaryFile(delete=True) as tmp:
tmp.write(spec.generate().read())
tmp.flush()
obj = MyModel.objects.create(original=ImageFile(tmp))
self.assertIsNotNone(obj.original)
self.assertIsNotNone(obj.thumbnail)
self.assertIsNotNone(obj.thumbnail_square) All test passed when running against:
However, when running tests against:
I get the following error:
|
Thanks a lot for bisecting this and giving to me a code with which I will reproduce the problem. When I have time I will check this to identify the issue. |
Had the same issue with 3.3, I don't use the template tag. |
Same issue here, not using the template tag, imagekit 3.2.7 |
@a1tus @Terr @troygrosfield @dbortolz @jakob-o Hello guys and sorry for taking so long. Can some of you test the code in #380 to see if the problem persists? I was unable to recreate a test case with this exact problem but when I was trying to port django-imagekit to Django 1.10 I received an error related to some temporary file (created from imagekit) which I suspect is the reason for this problem. If some of you confirm that the problem no longer persists then we will have "two rabbits with one bullet". |
Hi guys I was able to reproduce the issue outside of the test and found that the regression happened in 3.2.7 by this PR #335. The issue is only happening if cached thumbnail was not present in the system and need to be generated on the fly. I'm trying to see for a solution for this |
While you're thinking about it, it was a long-standing issue that width and height should be cached too 😊 |
…ter url If the file is closed and something is calling `open` now the file will be opened correctly event if it was already closed
@matthewwithanm when I prepared the patch for enabling Django 1.10 support I found that probably they need to be cached for performance improvement but still the underling issue need to be solved first. I think that now I solved the problem in #384 (this time for real). If someone can confirm we can merge it and probably release a new version. |
👍 Keep up there good work! |
Thanks @vstoykov. I'm getting hundreds of server error emails on this per day, so hopefully you (or someone else) can reap this sweet bounty :) Speaking of which, @matthewwithanm, any chance you'll be setting up a flattr or gratipay or anything like that for imagekit? I have a moderately-sized web site with millions of images managed under imagekit and would love to drop a few bucks to help support development. |
Fixed #350: Error when trying to access width/height after url
* release/4.0: Bump version to 4.0 Universal wheels! Replaces #301 autodiscover works with AppConfig Ignore autogenerated CTags file Do not try south modelinspector when not needed Make it possible to configure IMAGEKIT_CACHE_TIMEOUT Test against Django 1.11 Close the file only if it has been opened locally Cleanup caching configuration updated readme.rst with a svg badge honor post_save's update_fields and only fire the source_saved signal when needed Ignore VSCode workspace config files Fixed #350: Error when trying to access width/height after url Fixes #382: Tests no longer leave junk Fixes #379 Support for Django 1.10 Ignore .idea from git Include the test suite in the sourcetarball but do not install it. Make travis happy Drop support for older Django and Python versions Replace Lenna image in tests with a truly free alternative. Move compat module outside of templatetags package Fix test requirements for django 1.9 and Python3.5
Error (see traceback below) occurs if you generate image with assignment tag and then access width or height after url. If you use width/height before url - everything goes fine. Also error disappears if you try to access already generated images.
All the related settings are the default ones.
Examples:
It's quite strange and I'm not sure if that problem existed before because even in docs there's an example that is doing exactly the same things.
Traceback:
PS. Confirmed for ImageKit 3.3 / Django 1.8-1.9 / Python 3.4.3
The text was updated successfully, but these errors were encountered: