Skip to content
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

Ugly Image Path #482

Open
asdasdasdasdasdasdasdad opened this issue Dec 24, 2016 · 4 comments
Open

Ugly Image Path #482

asdasdasdasdasdasdasdad opened this issue Dec 24, 2016 · 4 comments

Comments

@asdasdasdasdasdasdasdad

Model

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    image = models.ImageField(upload_to='polls')

Template

{% thumbnail question.image "300" crop="center" as im %}
            <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}

And my thumb path
http://127.0.0.1:8000/media/cache/2a/cf/2acf000bda53bcad6bf9573c00c3f8f2.jpg
This is bad for seo. I want orginal filename on thumb. E.g
http://127.0.0.1:8000/media/orginal-name-300x500.jpg
http://127.0.0.1:8000/media/orginal-name-medium.jpg
Is this possible? Thanks in advance

@jplehmann
Copy link

Doubtful.

See this comment:
http://sorl-thumbnail.readthedocs.io/en/latest/reference/settings.html#thumbnail-backend

This is the entry point for generating thumbnails, you probably want to keep the default one but just in case you would like to generate thumbnails filenames differently or need some special functionality you can override this and use your own implementation.

@kedare
Copy link

kedare commented Jan 14, 2017

I don't think it's a good idea to use the kind of name you are looking for.

Here you have a full guarantee that the filename will not be reused. So you can for example cache it forever in a CDN or at least for a very long duration.

If you use predictable names like the ones you are looking for, if for any reason you need to regenerate the image, they will take the same names again and then you may have stale images cached everywhere.
Also depending of the way you manage your medias, the current naming allows better partitioning distribution.

@Flimm
Copy link
Contributor

Flimm commented Nov 29, 2017

Does this answer your question, @ErkinAkbudak ? Have you tried override the default thumbnail backend and creating your own one?

@x0nix
Copy link

x0nix commented Oct 6, 2021

I had the similar need and this worked for me:

# myproject/thumbnail_backend.py
from pathlib import Path

from django.utils.text import slugify
from sorl.thumbnail.base import EXTENSIONS, ThumbnailBackend
from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import serialize, tokey


class NamingThumbnailBackendMixin(object):
    def _get_thumbnail_filename(self, source, geometry_string, options):
        """
        Computes the destination filename.
        """
        key = tokey(source.key, geometry_string, serialize(options))
        # make some subdirs
        path = '%s/%s/%s' % (key[:2], key[2:4], key)
        # get filename_stem from options or use source name
        filename_stem = slugify(options.get('filename_stem') or Path(source.name).stem)
        return '%s%s/%s.%s' % (settings.THUMBNAIL_PREFIX, path, filename_stem, EXTENSIONS[options['format']])


class NamingThumbnailBackend(NamingThumbnailBackendMixin, ThumbnailBackend):
    pass
# settings.py
...
THUMBNAIL_BACKEND = 'myproject.thumbnail_backend.NamingThumbnailBackend'
...

That creates thumbnail path like this: /media/cache/2a/cf/2acf000bda53bcad6bf9573c00c3f8f2/original-name.jpg
I think that this path:

  • has all advantages mentioned by @kedare
  • is better for seo
  • is better if user downloads the file

And (optionally) you can also modify the name:

{% thumbnail author.image "300x300" crop="center" filename_stem=author.name as im %}
     <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants