1

I am trying to understand how Django ImageKit works with respect to creating thumbnail files (for example). I am using the example code:

from django.db import models
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill

class Profile(models.Model):
    avatar = models.ImageField(upload_to='avatars')
    avatar_thumbnail = ImageSpecField(source='avatar',
                                      processors=[ResizeToFill(100, 50)],
                                      format='JPEG',
                                      options={'quality': 60})

I am uploading the avatar image from an app. This works fine with an entry made in the Profile table and the file created in AWS S3. What I am struggling to understand is when/where/how the avatar_thumbnail is created. Do I have to do something explicit to get it stored in AWS S3 along with the avatar image? Or is the avatar_thumbnail only ever created on the fly? I need it stored somewhere for later use.

Bill Noble
  • 6,466
  • 19
  • 74
  • 133

2 Answers2

1

I don't get it 100%, but from what I understood the thumbnail is a generator that is only called when the thumbnail is first requested, and is then cached.

My personal experience with it suggests so as well. I created a dummy instance of the model (same code as above) through the admin interface. I then created an html page that displays the thumbnails with template tagging (<img src="instance.thumbnail.url">). Checking my folders, no images generated so far. Then I launch a server, navigate to that page. It takes unusual time to load (that's an indication that the thumbnails are being created) on the first try, but then it speeds up. And the files are there.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Kostas Mouratidis
  • 1,145
  • 2
  • 17
  • 30
1

By default, ImageKit generates ImageSpecField images when they are needed, not when the model object is created. To change the behavior, you can use the cache file strategies. Default value of IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY is JustInTime, which can be changed to Optimistic which creates images on model object creation, or to custom strategy.

Moreover, you can set different strategies for individual ImageSpecFields by providing the cachefile_strategy parameter.

Kueruetsch
  • 11
  • 3