Example #1
0
def avatar(item, geometry='60x60', quality=99):
    """Build user avatar.

    Digs out the url to the visual representation from object. If no one exists
    defaults to empty string.
    """
    url = ""
    try:
        if isinstance(item, Project):
            url = get_thumbnail(item.current_image,
                                geometry,
                                crop='center',
                                quality=quality).url
        elif isinstance(item, Organisation):
            url = get_thumbnail(item.logo,
                                geometry,
                                crop='center',
                                quality=quality).url
        elif isinstance(item, ProjectUpdate):
            url = get_thumbnail(item.photo,
                                geometry,
                                crop='center',
                                quality=quality).url
    except Exception:
        pass

    return url
Example #2
0
        def get_thumb(request, name):
            if name not in [u'original', u'default']:
                try:
                    width = request.GET.get(
                        'image_thumb_{}_width'.format(name))
                    if width:
                        return get_thumbnail(value,
                                             '{}'.format(width),
                                             quality=99)
                    height = request.GET.get(
                        'image_thumb_{}_height'.format(name))
                    if height:
                        return get_thumbnail(value,
                                             'x{}'.format(height),
                                             quality=99)
                    # yes this is redundant...code is nearly identical with the width code above
                    # but for clarity of function we keep them separate
                    max_size = request.GET.get(
                        'image_thumb_{}_max_size'.format(name))
                    if max_size:
                        return get_thumbnail(value,
                                             '{}'.format(max_size),
                                             quality=99)
                except (ThumbnailParseError, IOError):
                    return None

            # no size specification matching the name found; give up
            return None
Example #3
0
def _get_carousel_data(project, updates):
    photos = []
    if project.current_image:
        try:
            im = get_thumbnail(project.current_image, '750x400', quality=99)
            photos.append({
                "url": im.url,
                "caption": project.current_image_caption,
                "credit": project.current_image_credit,
                "direct_to_url": '',
            })
        except IOError:
            pass
    for update in updates:
        if update.photo:
            direct_to = reverse('update-main',
                                kwargs={
                                    'project_id': project.pk,
                                    'update_id': update.pk
                                })
            try:
                im = get_thumbnail(update.photo, '750x400', quality=99)
                photos.append({
                    "url": im.url,
                    "caption": update.photo_caption,
                    "credit": update.photo_credit,
                    "direct_to_url": direct_to,
                })
            except IOError:
                continue
    return {"photos": photos}
Example #4
0
def _get_carousel_data(project, updates):
    photos = []
    if project.current_image:
        try:
            im = get_thumbnail(project.current_image, '750x400', quality=99)
            photos.append({
                "url": im.url,
                "caption": project.current_image_caption,
                "credit": project.current_image_credit,
                "direct_to_url": '',
            })
        except IOError:
            pass
    for update in updates:
        if update.photo:
            direct_to = reverse('update-main', kwargs={
                'project_id': project.pk,
                'update_id': update.pk
            })
            try:
                im = get_thumbnail(update.photo, '750x400', quality=99)
                photos.append({
                    "url": im.url,
                    "caption": update.photo_caption,
                    "credit": update.photo_credit,
                    "direct_to_url": direct_to,
                })
            except IOError:
                continue
    return {"photos": photos}
Example #5
0
 def get_image(self, update):
     width = '350'
     try:
         image = get_thumbnail(update.photo, width, crop='smart', quality=99)
         url = image.url
     except Exception as e:
         logger.error(
             'Failed to get thumbnail for image %s with error: %s', update.photo, e
         )
         url = update.photo.url if update.photo.name else ''
     return url
Example #6
0
 def get_image(self, update):
     width = '350'
     try:
         image = get_thumbnail(update.photo, width, crop='smart', quality=99)
         url = image.url
     except Exception as e:
         logger.error(
             'Failed to get thumbnail for image %s with error: %s', update.photo, e
         )
         url = update.photo.url if update.photo.name else ''
     return url
Example #7
0
 def get_image(self, project):
     geometry = '350x200'
     try:
         image = get_thumbnail(project.current_image, geometry, crop='smart', quality=99)
         url = image.url
     except Exception as e:
         logger.error(
             'Failed to get thumbnail for image %s with error: %s', project.current_image, e
         )
         url = project.current_image.url if project.current_image.name else ''
     return url
Example #8
0
 def get_image(self, organisation):
     width = '191'
     try:
         image = get_thumbnail(organisation.logo, width, crop='smart', quality=99)
         url = image.url
     except Exception as e:
         logger.error(
             'Failed to get thumbnail for image %s with error: %s', organisation.logo, e
         )
         default_logo = '{}{}'.format(settings.STATIC_URL, 'rsr/images/default-org-logo.jpg')
         url = organisation.logo.url if organisation.logo.name else default_logo
     return url
Example #9
0
 def get_image(self, project):
     geometry = '350x200'
     try:
         image = get_thumbnail(project.current_image,
                               geometry,
                               crop='smart',
                               quality=99)
         url = image.url
     except Exception as e:
         logger.error('Failed to get thumbnail for image %s with error: %s',
                      project.current_image, e)
         url = project.current_image.url if project.current_image.name else ''
     return url
Example #10
0
 def get_image(self, organisation):
     width = '191'
     try:
         image = get_thumbnail(organisation.logo,
                               width,
                               crop='smart',
                               quality=99)
         url = image.url
     except Exception as e:
         logger.error('Failed to get thumbnail for image %s with error: %s',
                      organisation.logo, e)
         default_logo = '{}{}'.format(settings.STATIC_URL,
                                      'rsr/images/default-org-logo.jpg')
         url = organisation.logo.url if organisation.logo.name else default_logo
     return url
Example #11
0
 def get_thumbnail_with_timeout():
     return get_thumbnail(project.current_image,
                          geometry,
                          crop='smart',
                          quality=99)
Example #12
0
    def to_representation(self, value):
        """
        :param value: A Base64ImageField object
        :return: a path to a thumbnail with a predetermined size, the default thumb
        OR
        a dict with a number of thumbnails, one of which is the default, the others being generated
        from the query string parameters, and finally the path to the original image keyed to
        "original".

        The extended functionality, allowing the generation of one or more thumbnails from the
        original image is triggered by including "image_thumb_name" in the query string. The value
        for image_thumb_name is a comma separated list of identifiers for the generated thumbs.
        The names must not be "default" or "original".

        For each thumb thus specified a size must be supplied as a query param on the form
            image_thumb_<name>_<dimension>
        where <name> is the name of the thumb specified as one of the values for image_thumb_name
        and <dimension> is one of "width, "height" or "max_size". width and height must be an integer
        specifying that dimension in pixels. The image will be scaled correctly in the other
        dimension. max_size is width and height concatenated with an "x" and sets the maximum size
        allowed for the respective dimensions, while still maintaining the correct aspect ratio of
        the image.

        Example:
        the querystring
            ?image_thumb_name=big,small&image_thumb_small_width=90&image_thumb_big_max_size=300x200
        results in the following dict being returned:
        {
            'original': '/full/path/to/original/image.png',
            'default': '/full/path/to/default/thumbnail/image.png',
            'small': '/full/path/to/small/thumbnail/image.png',
            'big': '/full/path/to/big/thumbnail/image.png',
        }
        This dict will be converted as appropriate to JSON or XML

        NOTE: This special functionality works best when there is only one image field in a model.
        If there are more, things will still work (I think), but for each image all thumbs returned
        will have the same dimensions
        """
        def get_thumb(request, name):
            if name not in [u'original', u'default']:
                try:
                    width = request.GET.get(
                        'image_thumb_{}_width'.format(name))
                    if width:
                        return get_thumbnail(value,
                                             '{}'.format(width),
                                             quality=99)
                    height = request.GET.get(
                        'image_thumb_{}_height'.format(name))
                    if height:
                        return get_thumbnail(value,
                                             'x{}'.format(height),
                                             quality=99)
                    # yes this is redundant...code is nearly identical with the width code above
                    # but for clarity of function we keep them separate
                    max_size = request.GET.get(
                        'image_thumb_{}_max_size'.format(name))
                    if max_size:
                        return get_thumbnail(value,
                                             '{}'.format(max_size),
                                             quality=99)
                except (ThumbnailParseError, IOError):
                    return None

            # no size specification matching the name found; give up
            return None

        if not value:
            return None

        default_width = '191'  # width of update images on akvo.org/seeithappen
        try:
            default_thumb = get_thumbnail(value, default_width, quality=99)
            request = self.context['request']
        except (ThumbnailParseError, IOError, KeyError):
            return None

        # look for name(s) of thumbnail(s)
        image_thumb_name = request.GET.get('image_thumb_name')
        if image_thumb_name:
            names = image_thumb_name.split(',')
            thumbnails = {
                u'original': value.url,
                u'default': default_thumb.url
            }
            for name in names:
                thumbnail = get_thumb(request, name)
                if thumbnail is not None:
                    thumbnails[name] = thumbnail.url
        else:
            thumbnails = default_thumb.url

        return thumbnails