Package: django-imagefit
Django Image Fit - Resize an image on the flyImagefit allows you to render an image in a template and specify its dimensions.
It preserves the original image file.It is compatible with various sources of images such as django-filebrowser's
FileBrowseField, user uploaded images, static images, …Works on Python 3.x; Django 4. Compatible with Django 4.2.
For previous versions of Django please refer to version 0.7.0 and previous versions of Pil.Benefitsonly 1 image file exists on the server, therefore it's always easy to replace
and adapt the image per template or zone.no model to adapt for large image and thumbnail that may vary when redesigning
the website.perfect match with mediaqueries to adapt on mobile, tablets and
multi-size screens.better quality than html/css resizing and no large file download, great for
lower bandwidth.Quick tourExample 1: render/static/myimage.pngimage at a maximum size of 200 x 150 px:{{ "/static/myimage.png"|resize:"200x150" }}Example 2: render model'snews.imageas a thumbnail:{{ news.image|resize:"thumbnail" }}Example 3: render/static/myimage.pngimage at a maximum cropped size of 150 x 150 px:{{ "/static/myimage.png"|resize:"150x150,C" }}Example 4: renderhttps://example.com/test.pngimage at a maximum cropped size of 150 x 150 px:{{ "https://example.com/test.png"|external_resize:"150x150,C" }}What this is notFor creating specific model fields that resize image when model saves, seedjango-imagekitIf you wish to avoid very large image on the server, consider resizing your original image
before uploading itInstallationDownloadVia pippipinstalldjango-imagefitor the bleeding edge versionpip install -e git+https://github.com/vinyll/django-imagefit.git#egg=django-imagefitupdate INSTALLED_APPSInsettings.py, addimagefitin your INSTALLED_APPSINSTALLED_APPS=(…,'imagefit',)And add the path relative to your project (seeconfigurationbelow)IMAGEFIT_ROOT="public"urls.pyImagefit is a resize service, therefore include its urls.Prefix it with whatever you want (here "imagefit" for example):fromdjango.urlsimportre_pathurlpatterns=urlpatterns('',…re_path(r'^imagefit/',include('imagefit.urls')),)Congratulations, you're all set!Usageyour_template.html{% load imagefit %}<imgsrc="{{ "/static/image.png"|resize:'thumbnail'}}"/><imgsrc="{{ "/static/image.png"|resize:'320x240'}}"/><imgsrc="{{ "/static/image.png"|resize:'320x240,C'}}"/><imgsrc="{{ "https://example.com/test.png"|external_resize:'320x240'}}"/>This will display your/static/image.png:in thethumbnailformat (80 x 80 px)resized in a custom 320 x 240 pixelsresized and cropped in a custom 320 x 240 pixelsthe,Cmodifier stands forCroppingConfigurationRoot pathYou should most probably customize the path to the root folder of your images.
The url your specify in your model will be concatenated to this IMAGEFIT_ROOT
to find the appropriate image on your system.The path will be relative to the project folder.If starting with a "/", it will be an absolute path (quid about Windows).IMAGEFIT_ROOT="public"So with this example the image url "/static/image.png" would be pointing to/PATH/TO/YOUR/PROJECT/public/static/image.pngTemplatetagsresize(value, size)  # path is relative to you settings.IMAGE_ROOT
static_resize(value, size)  # path is relative to you settings.STATIC_ROOT
media_resize(value, size)  # path is relative to you settings.MEDIA_ROOT
external_resize(value, size) # path is an http/https urlCan be used in templates as so :{{ "/static/logo.png"|resize:'320x240' }}
{{ "logo.png"|static_resize:'320x240' }}
{{ "user_avatar.png"|media_resize:'320x240' }}
{{ "https://example.com/test.png"|external_resize:'320x240' }}PresetsPresets are configuration names that hold width and height (and maybe more later on).
Imagefit is already shipped with 3 presets :thumbnail(80x80),medium(320x240)
andoriginal(no resizing).You may override them or create new ones through settings.pyCustom presets examples :IMAGEFIT_PRESETS={'thumbnail':{'width':64,'height':64,'crop':True},'my_preset1':{'width':300,'height':220},'my_preset2':{'width':100},}CacheBecause resizing an image on the fly is a big process, django cache is enabled
by default.Therefore you are strongly invited to set your imagefit cache preferences to
False for local development.You can customize the default cache preferences by overriding default values
described below via settings.py :# enable/disable server cacheIMAGEFIT_CACHE_ENABLED=True# set the cache name specific to imagefit with the cache dictIMAGEFIT_CACHE_BACKEND_NAME='imagefit'CACHES={'imagefit':{'BACKEND':'django.core.cache.backends.filebased.FileBasedCache','LOCATION':os.path.join(tempfile.gettempdir(),'django_imagefit')}}Note thatCACHESdefault values will be merge with yours fromsettings.pyFormatsImagefit uses PIL to resize and crop the images and this library requires to
specify the format of the output file. Imagefit allows you to specify an output
format depending of the output filename. Please note that the the output extension
is left unchanged.You can customize the default mapping by overriding default values described below
via settings.py :# Example extension -> format.IMAGEFIT_EXT_TO_FORMAT={'.jpg':'jpeg','.bmp':'png'}# Disallow the fall-back to a default format: Raise an exception in such case.IMAGEFIT_EXT_TO_FORMAT_DEFAULT=NoneExpires HeaderDjango Imagefit comes with Expires header to tell the browser whether it should request the resource from the server or use the cached version.This has two core benefits. The browser will be using the cached version of the resource in the second load and page load will be much faster. Also, it will require fewer requests to the server.As a page score parameter, static resources used in a web page should be containing an Expires information for better performance.The default value of the expires header is set to 30 days from now. You can override this value via settings.py as:IMAGEFIT_EXPIRE_HEADER=3600# for 1 hourTroubleshooting"decoder jpeg not available" on Mac OSXYou may have installed PIL through pip or easy_install that
does not install libjpeg dependency.If so :Uninstall pil via pipInstall pip via homebrew:brew install pilReinstall pil via pip:pip install pilTodoRefactorviews.resizeMake resize quality/speed configurableMore examples for docenable URL images in addition to system filesImagefit DevelopersAka note to self: Deploy to pypi usingmake deploy.