def generic_cached_admin_thumb(instance): image = instance.images.first() if image: cached = ImageCacheFile(AdminThumbnailSpec(image.image_file)) cached.generate() return cached return None
def add_thumb_if_not_none(files, field): from imagekit.cachefiles import ImageCacheFile if field: image_generator = U24Thumbnail(source=field.file) file = ImageCacheFile(image_generator) file.generate() files.append(os.path.join(settings.MEDIA_ROOT, file.name))
def cached_admin_thumb(instance): if instance.img: # `image` is the name of the image field on the model cached = ImageCacheFile(AdminThumbnailSpec(instance.img)) # only generates the first time, subsequent calls use cache cached.generate() return cached return ""
def cached_list_thumb(instance): # 'image' is the name of the image field on the model cached = '' if instance.image: cached = ImageCacheFile(ListThumbnail(instance.image)) # only generates the first time, subsequent calls use cache cached.generate() return cached
def get_thumb_url(field: ImageFieldFile): from imagekit.cachefiles import ImageCacheFile if field: image_generator = PreviewThumbnail(source=field.file) file = ImageCacheFile(image_generator) file.generate() return file.url return None
def _crop_image(self): if not self.is_image: raise Attachment.NotAnImageException("Attachment is not an image. Use is_image to check" " if attachment is an image before asking for the " "cropped version.") generator = Attachment.CroppedImage(source=self.file) cache_file = ImageCacheFile(generator) try: cache_file.generate() except FileNotFoundError as e: logger.warn("File not found when generating cache file: " + str(e)) return cache_file
def get(self, request, *args, **kwargs): photo = self.get_object() width = int(request.GET.get('w', photo.image.width)) height = int(request.GET.get('h', photo.image.height)) style = request.GET.get('s', 'fill') proc = CROP_STYLES.get(style, ResizeToFill) spec = CropSpec(source=photo.image) spec.processors = [proc(width, height)] icf = ImageCacheFile(spec) icf.generate() return HttpResponseRedirect(icf.url)
def thumb(content, thumb_type=None): """A filter to return an image file used to preview a Content as a thumbnail. The particular image selected depends on the type of Content. """ image = None # The ImageField that we're using as a source for the thumbnail if ( isinstance(content, models.Story) or isinstance(content, models.Video) ) and content.cover: image = content.cover.source elif isinstance(content, models.Image): image = content.source elif isinstance(content, models.Gallery) and content.entries.count() > 0: # Gallery cache currently messed up, so no thumbnail for now # image = thumb(content.entries_in_order()[0]) return content.entries_in_order()[0].source.url if image is None: return None if thumb_type is None: return True # Check which thumbnail generator we're using klass = { "small": SmallThumbnail, "medium": MediumThumbnail, "large": LargeThumbnail, "huge": HugeThumbnail, }[thumb_type] # Make the thumbnail generator and cache the result try: generator = klass(source=image) cached_file = ImageCacheFile(generator) cached_file.generate() # This seems to be necessary to save the file return cached_file.url except FileNotFoundError: return None except (IOError, SyntaxError): # Error actually parsing the file return image.url
def test_no_source_error(): spec = TestSpec(source=None) file = ImageCacheFile(spec) file.generate()
def create_size(self, photosize): generator = PhotologueSpec(photo=self, photosize=photosize) cache = ImageCacheFile(generator) cache.generate()
def get_image(self, image, w, h): generator = self.get_generator(w, h)(source=image) file = ImageCacheFile(generator) file.generate() return file
def cached_tiny_thumb(self): generator = ImageCacheFile(self.tiny_thumb) return generator.generate()
def cached_default(self): generator = ImageCacheFile(self.default) return generator.generate()
def cached_bigger(self): generator = ImageCacheFile(self.bigger) return generator.generate()
def cached_full_fixed(self): generator = ImageCacheFile(self.full_fixed) return generator.generate()
def images(request, image_in: ImageIn): image_input_processor_func = getattr(settings, 'CDN_IMAGE_INPUT_PROCESSOR', None) if image_input_processor_func is not None: get_by_qname(image_input_processor_func, 'image input processor')(image_in) kwargs = getattr(settings, 'CDN_IMAGE_REQUEST_ARGS', {'timeout': 5.0}) response = requests.get(image_in.origin, **kwargs) try: response.raise_for_status() except requests.HTTPError as exc: if 400 <= exc.response.status_code < 500: return exc.response.status_code, { 'detail': [{ 'loc': ['query', 'origin'], 'msg': str(exc), 'type': 'requests_error.http_4xx', }] } raise name = unquote(urlparse(response.url).path.lstrip('/')) content = response.content with ContentFile(content, name=name) as image_fd: if image_in.width is None: generator = generator_registry.get( 'cdn:origin_resolution', source=image_fd, format=image_in.format, ) else: generator = generator_registry.get( 'cdn:thumbnail', source=image_fd, width=image_in.width, format=image_in.format, crop=False, upscale=False, ) file = ImageCacheFile(generator) file.generate(force=image_in.force) image_out = ImageOut( url=file.url, origin=response.url, width=image_in.width, format=image_in.format, force=image_in.force, ) image_output_processor_func = getattr(settings, 'CDN_IMAGE_OUTPUT_PROCESSOR', None) if image_output_processor_func is not None: get_by_qname(image_output_processor_func, 'image output processor')(image_out) return image_out
def cached_admin_thumb(instance): cached = ImageCacheFile(AdminThumbnailSpec(instance.image)) cached.generate() return cached
def cached_preview(self): generator = ImageCacheFile(self.preview) return generator.generate()