def __init__(self, relative_source, requested_size, opts=None, quality=None, basedir=None, subdir=None, prefix=None): # Set the absolute filename for the source file source = self._absolute_path(relative_source) quality = get_thumbnail_setting('QUALITY', quality) imagemagick_path = get_thumbnail_setting('CONVERT') wvps_path = get_thumbnail_setting('WVPS') # Call super().__init__ now to set the opts attribute. generate() won't # get called because we are not setting the dest attribute yet. super(DjangoThumbnail, self).__init__(source, requested_size, opts=opts, quality=quality, imagemagick_path=imagemagick_path, wvps_path=wvps_path) # Get the relative filename for the thumbnail image, then set the # destination filename relative_thumbnail = \ self._get_relative_thumbnail(relative_source, basedir=basedir, subdir=subdir, prefix=prefix) self.dest = self._absolute_path(relative_thumbnail) # Call generate now that the dest attribute has been set self.generate() # Set the relative & absolute url to the thumbnail self.relative_url = \ iri_to_uri('/'.join(relative_thumbnail.split(os.sep))) self.absolute_url = '%s%s' % (settings.MEDIA_URL, self.relative_url)
def __init__( self, relative_source, requested_size, opts=None, quality=None, basedir=None, subdir=None, prefix=None, relative_dest=None, ): if not isinstance(relative_source, types.StringTypes): # relative_source = relative_source.field.generate_filename(relative_source.instance, relative_source._name) new_path = relative_source.storage.path(relative_source.name).replace( os.path.abspath(settings.MEDIA_ROOT), "" ) if new_path[0] == "/": new_path = new_path[1:] relative_source = new_path # Set the absolute filename for the source file source = self._absolute_path(relative_source) quality = get_thumbnail_setting("QUALITY", quality) imagemagick_path = get_thumbnail_setting("CONVERT") wvps_path = get_thumbnail_setting("WVPS") # Call super().__init__ now to set the opts attribute. generate() won't # get called because we are not setting the dest attribute yet. super(DjangoThumbnail, self).__init__( source, requested_size, opts=opts, quality=quality, imagemagick_path=imagemagick_path, wvps_path=wvps_path ) # Get the relative filename for the thumbnail image, then set the # destination filename if relative_dest is None: self.relative_dest = self._get_relative_thumbnail( relative_source, basedir=basedir, subdir=subdir, prefix=prefix ) else: self.relative_dest = relative_dest self.dest = self._absolute_path(self.relative_dest) # Call generate now that the dest attribute has been set self.generate() # Set the relative & absolute url to the thumbnail self.relative_url = iri_to_uri("/".join(self.relative_dest.split(os.sep))) self.absolute_url = "%s%s" % (settings.MEDIA_URL, self.relative_url)
def _get_relative_thumbnail(self, relative_source, basedir=None, subdir=None, prefix=None): """ Returns the thumbnail filename including relative path. """ basedir = get_thumbnail_setting("BASEDIR", basedir) subdir = get_thumbnail_setting("SUBDIR", subdir) prefix = get_thumbnail_setting("PREFIX", prefix) path, filename = os.path.split(relative_source) basename, ext = os.path.splitext(filename) name = "%s%s" % (basename, ext.replace(".", "_")) size = "%sx%s" % self.requested_size opts_list = self.opts_list[:] opts_list.append("") opts = "_".join(opts_list) thumbnail_filename = "%s%s_%s_%sq%s.jpg" % (prefix, name, size, opts, self.quality) return os.path.join(basedir, path, subdir, thumbnail_filename)