def resize_image(self, image, width=None, file_type=None, censor_nsfw=False, max_ratio=None): url = UrlParser(image['url']) is_gif = url.path.endswith('.gif') and (file_type == 'mp4' or not file_type) if is_gif: url.hostname = g.imgix_gif_domain else: url.hostname = g.imgix_domain # Let's encourage HTTPS; it's cool, works just fine on HTTP pages, and # will prevent insecure content warnings on HTTPS pages. url.scheme = 'https' # g.s3_media_direct affects how preview image urls are stored # True: http://{s3_media_domain}/mybucket/helloworld.jpg # False: http://mybucket/helloworld.jpg # If it's True, we'll need to strip the bucket out of the path if g.s3_media_direct: path_parts = url.path.split('/') path_parts.pop(1) url.path = '/'.join(path_parts) if max_ratio: url.update_query(fit='crop') # http://www.imgix.com/docs/reference/size#param-crop url.update_query(crop='faces,entropy') url.update_query(arh=max_ratio) if width: if width > image['width']: raise NotLargeEnough() # http://www.imgix.com/docs/reference/size#param-w url.update_query(w=width) if file_type and file_type in ('gif', 'jpg', 'png', 'mp4'): url.update_query(fm=file_type) # We need to disable fragmented mp4s for proper playback in Firefox if file_type == 'mp4': url.update_query(**{'mp4-fragmented': 'false'}) if censor_nsfw: # Do an initial blur to make sure we're getting rid of icky # details. # # http://www.imgix.com/docs/reference/stylize#param-blur url.update_query(blur=600) # And then add pixellation to help the image compress well. # # http://www.imgix.com/docs/reference/stylize#param-px url.update_query(px=32) if g.imgix_signing: if is_gif: url = self._sign_url(url, g.secrets['imgix_gif_signing_token']) else: url = self._sign_url(url, g.secrets['imgix_signing_token']) return url.unparse()
def test_same_url(self): u = UrlParser('http://example.com:8000/a;b?foo=bar&bar=baz#spam') u2 = UrlParser('http://example.com:8000/a;b?bar=baz&foo=bar#spam') self.assertEquals(u, u2) u3 = UrlParser('') u3.scheme = 'http' u3.hostname = 'example.com' u3.port = 8000 u3.path = '/a' u3.params = 'b' u3.update_query(foo='bar', bar='baz') u3.fragment = 'spam' self.assertEquals(u, u3)