def validate_request(self): self._validate_operation() self._validate_url() self._validate_signature() self._validate_client() self._validate_host() opts = self._get_save_options() ops = self._get_operations() if "resize" in ops: w, h = self.get_argument("w"), self.get_argument("h") Image.validate_dimensions(w, h) if w and int(w) > self.settings.get("max_resize_width"): raise errors.DimensionsError("Exceeds maximum allowed width") elif h and int(h) > self.settings.get("max_resize_height"): raise errors.DimensionsError("Exceeds maximum allowed height") opts.update(self._get_resize_options()) if "rotate" in ops: Image.validate_degree(self.get_argument("deg")) opts.update(self._get_rotate_options()) if "region" in ops: Image.validate_rectangle(self.get_argument("rect")) if "scale-ar" in ops: Image.validate_scale_ar_options(self.get_argument("ar")) Image.validate_options(opts)
def test_valid_default_options_with_empty_values(self): opts = dict(mode=None, filter=None, background=None, position=None, quality=None) Image.validate_options(opts)
def _validate_request(self): self._validate_url() self._validate_signature() self._validate_client() self._validate_host() Image.validate_dimensions( self.get_argument("w"), self.get_argument("h")) Image.validate_options(self._get_resize_options())
def _resize(self, resp): image = Image(resp.buffer, self.settings) opts = self._get_resize_options() resized = image.resize( self.get_argument("w"), self.get_argument("h"), **opts) self._forward_headers(resp.headers) for block in iter(lambda: resized.read(65536), b""): self.write(block) resized.close()
def test_resize_using_settings(self): for case in get_image_resize_cases(): if case.get("mode") == "crop" and case.get("position") == "face": continue with open(case["source_path"], "rb") as f: img = Image(f, case).resize( case["width"], case["height"], mode=case["mode"]) with open(case["expected_path"], "rb") as expected: msg = "%s does not match %s" \ % (case["source_path"], case["expected_path"]) self.assertEqual(img.read(), expected.read(), msg)
def _assert_expected_resize(self, case): with open(case["source_path"], "rb") as f: img = Image(f).resize( case["width"], case["height"], mode=case["mode"], background=case.get("background"), filter=case.get("filter"), position=case.get("position"), quality=case.get("quality")) with open(case["expected_path"], "rb") as expected: msg = "%s does not match %s" \ % (case["source_path"], case["expected_path"]) self.assertEqual(img.read(), expected.read(), msg)
def _assert_expected_resize(self, case): with open(case["source_path"], "rb") as f: img = Image(f).resize(case["width"], case["height"], mode=case["mode"], background=case.get("background"), filter=case.get("filter"), position=case.get("position"), quality=case.get("quality")) with open(case["expected_path"], "rb") as expected: msg = "%s does not match %s" \ % (case["source_path"], case["expected_path"]) self.assertEqual(img.read(), expected.read(), msg)
def validate_request(self): self._validate_operation() self._validate_url() self._validate_signature() self._validate_client() self._validate_host() opts = self._get_save_options() ops = self._get_operations() if "resize" in ops: w, h = self.get_argument("w"), self.get_argument("h") Image.validate_dimensions(w, h) if w and int(w) > self.settings.get("max_resize_width"): raise errors.DimensionsError("Exceeds maximum allowed width") elif h and int(h) > self.settings.get("max_resize_height"): raise errors.DimensionsError("Exceeds maximum allowed height") opts.update(self._get_resize_options()) if "rotate" in ops: Image.validate_degree(self.get_argument("deg")) opts.update(self._get_rotate_options()) if "region" in ops: Image.validate_rectangle(self.get_argument("rect")) if "watermark" in ops: url = self.get_argument("watermark_img") text = self.get_argument("watermark_txt") if url and not url.startswith("http://") and not url.startswith( "https://"): raise errors.DimensionsError("Unsupported protocol") if text is not None and not text: raise errors.DimensionsError("Watermark text cannot be empty") if text is None and url is None: raise errors.DimensionsError( "Watermark requires either watermark_img or watermark_txt") Image.validate_options(opts)
def validate_request(self): self._validate_operation() self._validate_url() self._validate_signature() self._validate_client() self._validate_host() opts = self._get_save_options() ops = self._get_operations() if "resize" in ops: Image.validate_dimensions( self.get_argument("w"), self.get_argument("h")) opts.update(self._get_resize_options()) if "rotate" in ops: Image.validate_degree(self.get_argument("deg")) opts.update(self._get_rotate_options()) if "region" in ops: Image.validate_dimensions( self.get_argument("w"), self.get_argument("h"), True) Image.validate_rectangle(self.get_argument("rect")) Image.validate_options(opts)
def _process_response(self, resp): ops = self._get_operations() if "noop" in ops: return (resp.buffer, None) image = Image(resp.buffer) for operation in ops: if operation == "resize": self._image_resize(image) elif operation == "rotate": self._image_rotate(image) elif operation == "region": self._image_region(image) return (self._image_save(image), image.img.format)
def _validate_request(self): self._validate_operation() self._validate_url() self._validate_signature() self._validate_client() self._validate_host() opts = self._get_save_options() ops = self._get_operations() if "resize" in ops: Image.validate_dimensions(self.get_argument("w"), self.get_argument("h")) opts.update(self._get_resize_options()) if "rotate" in ops: Image.validate_degree(self.get_argument("deg")) opts.update(self._get_rotate_options()) if "region" in ops: Image.validate_rectangle(self.get_argument("rect")) Image.validate_options(opts)
def validate_request(self): self._validate_operation() self._validate_url() self._validate_signature() self._validate_client() self._validate_host() opts = self._get_save_options() ops = self._get_operations() if "resize" in ops: w, h = self.get_argument("w"), self.get_argument("h") Image.validate_dimensions(w, h) if w and int(w) > self.settings.get("max_resize_width"): raise errors.DimensionsError("Exceeds maximum allowed width") elif h and int(h) > self.settings.get("max_resize_height"): raise errors.DimensionsError("Exceeds maximum allowed height") opts.update(self._get_resize_options()) if "rotate" in ops: Image.validate_degree(self.get_argument("deg")) opts.update(self._get_rotate_options()) if "region" in ops: Image.validate_rectangle(self.get_argument("rect")) Image.validate_options(opts)
def test_valid_default_options(self): Image.validate_options(dict())
def test_valid_dimensions(self): Image.validate_dimensions(100, 100) Image.validate_dimensions("100", "100")
def test_bad_format(self): path = os.path.join(DATADIR, "test-bad-format.gif") with open(path, "rb") as f: image = Image(f) self.assertRaises(FormatError, image.resize, 100, 100)
def _pilbox_operation(self, operation): """ Use Pilbox to transform an image """ image = PilboxImage(BytesIO(self.data)) if operation == "region": image.region(self.options.get("rect").split(',')) elif operation == "rotate": image.rotate(deg=self.options.get("deg"), expand=self.options.get("expand")) elif operation == "resize": max_width = self.options.get("max-width") max_height = self.options.get("max-height") resize_width = self.options.get("w") resize_height = self.options.get("h") # Make sure widths and heights are integers if resize_width: resize_width = int(resize_width) if resize_height: resize_height = int(resize_height) if max_width: max_width = int(max_width) if max_height: max_height = int(max_height) # Image size management with WandImage(blob=self.data) as image_info: # Don't allow expanding of images if resize_width or resize_height: width_oversize = resize_width > image_info.width height_oversize = resize_height > image_info.height if (width_oversize or height_oversize): expand_message = ( "Resize error: Maximum dimensions for this image " "are {0}px wide by {1}px high.").format( image_info.width, image_info.height) raise PilboxError(400, log_message=expand_message) # Process max_width and max_height if not resize_width and max_width: if max_width < image_info.width: resize_width = max_width if not resize_height and max_height: if max_height < image_info.height: resize_height = max_height if resize_height or resize_width: image.resize(width=resize_width, height=resize_height, mode=self.options.get("mode"), filter=self.options.get("filter"), background=self.options.get("bg"), retain=self.options.get("retain"), position=self.options.get("pos")) self.data = image.save(quality=self.options.get("q")).read()
def _process_response(self, resp): image = Image(resp.buffer) image.resize(self.w, self.h) return image.save()