matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.27k stars 275 forks source link

Cache or thread problem #372

Open Nabu-thinker-ru opened 8 years ago

Nabu-thinker-ru commented 8 years ago

I have this view:

from os import                                  path, makedirs
import                                          imghdr

from django.http import                         HttpResponse, Http404
from django.conf import                         settings

from imagekit import                            ImageSpec
from imagekit.processors import                 ResizeToFit

class Thumbnail(ImageSpec):
    processors = []
    format = 'JPEG'
    options = {'quality': 71}
    def __init__(self, **kwargs):
        self.processors.append(ResizeToFit(width = kwargs['width'],
                                           height = kwargs['height'],
                                           upscale = False)
                               )
        return super(Thumbnail, self).__init__(source = kwargs['source'])

def customizepic(request, *args, **kwargs):
    '''
    resize image to size defined in url like
    cp/(?P<params>.+?)/(?P<img_path>.*)
    '''
    customized_img = path.join(settings.CP_ROOT, kwargs['params'] , kwargs['img'])

    #image exist and nginx should serve this url, but for development server
    if path.isfile(customized_img):
        return HttpResponse(open(customized_img,'rb'), content_type="image/jpeg")
    original_img = path.join(settings.MEDIA_ROOT, kwargs['img'] )
    if path.isfile(original_img) and imghdr.what(original_img) == 'jpeg': 
        width, height = kwargs['params'].split('h')
        width = int(width[1:])
        height = int(height)
        if not width or not height:        
            raise Http404

        source = open(original_img,'rb')
        print height
        image_generator = Thumbnail(source = source, height = height, width = width)

        if not path.isdir(path.split(customized_img)[0]):
            makedirs(path.split(customized_img)[0])

        with open(customized_img, 'wb') as w:
            w.write(image_generator.generate().read())

        with open(customized_img, 'rb') as f:
            return HttpResponse(f, content_type="image/jpeg")
    raise Http404

So, i run django development server and request page which contains two different sizes of one image. Theirs url /cp/w100h100/media/sdsds.jpg and /cp/w200h200/media/sdsds.jpg. Everything works fine, but both generated thumbnail has 200x200 size, like the last requested.

Is it cache problem? How i can disable it at all?

vstoykov commented 8 years ago

Would you explain the problem and how it can be reproduced?

Nabu-thinker-ru commented 8 years ago

Sorry. I pressed submit button accidentally. i've corrected issue

vstoykov commented 8 years ago

If you are trying to set dummy cache if the problem still persist?

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}
vstoykov commented 8 years ago

@Nabu-thinker-ru can you check what I asked for in the comment above?

vstoykov commented 7 years ago

Without more information I'm unable to help