使用easy thumbnails轻松为图片创建缩略图

关于Django的缩略图

我在Django的官方文档里看了一下,没有找到有关缩略图的官方库,不过在Django的Wiki里头倒是看到了一篇关于使用缩略图的介绍,请参考:https://code.djangoproject.com/wiki/ThumbNails

关于Problem的描述

The majority of applications that have images, probably use thumbnails in some capacity. There is no standard thumbnail capability in Django but there are a couple of different options out there that people have created. This page is an attempt to gather them in one place and discuss the best strategy for integrating one in Django.

Current Implementations

官方推荐的第三方库 * ​sorl-thumbnail is excellent. * ​SmileyChris's easy-thumbnails is also excellent. * ​aino-convert a versatile solution for the fastidious. * ​Nesh's Thumbnail * ​image-kit a django-ish way of resizing images, with image models * Here's a more simple filter from Django snippets - ​Simple filter * In Ticket #4115 you will find a patch with a contrib.thumbnails package - good documented; read the Google Discussion mentioned above for some thoughts about the implementation. * Quick tip for adding thumbnails - ​Blog posting * Yet another approach to adding a thumbnail - CustomUploadAndFilters * Snippet that creates thumbnails on-demand using dynamics methods - ​Snippet 619 * Yet another method to resize image on upload - ​Automatically resizing uploaded images * Custom field for image resizing on upload - ​http://djangothumbnails.com/ * Template tag for resizing to exact dimensions: ​http://bitbucket.org/winsmith/django-thumbnail/wiki/Home * Easy frontend filter for resizing on the fly (great for mobile and mediaqueries) ​django-imagefit

关于easy_thumbnails

A powerful, yet easy to implement thumbnailing application for Django 1.8+ Below is a quick summary of usage. For more comprehensive information, view the full documentation online or the peruse the project's docs directory.

了解更多请打开easy_thumbnails的项目主页,链接在上面有,公众号推文不能点击链接请访问原文。

使用步骤简介

安装

pip install easy-thumbnails

配置 编辑settings.py

INSTALLED_APPS = (
    ...
    'easy_thumbnails',
)

THUMBNAIL_ALIASES = {
    '': {
        'avatar': {'size': (50, 50), 'crop': True},
    },
}

Run manage.py migrate easy_thumbnails.

在templates中使用

{% load thumbnail %}
<img src="{{ profile.photo|thumbnail_url:'avatar' }}" alt="" />

更多使用方法请参考官方文档

About

了解更多有趣的操作请关注我的微信公众号:DealiAxy 每一篇文章都在我的博客有收录:blog.deali.cn