예제 #1
0
    def test_watermark_filter_calculated_resizing(self):
        watermark.Filter.pre_compile()
        filter = watermark.Filter("http://dummy,0,0,0", self.context)

        for source_image_width, source_image_height in SOURCE_IMAGE_SIZES:
            for watermark_source_image_width, watermark_source_image_height in WATERMARK_IMAGE_SIZES:
                for w_ratio, h_ratio in RATIOS:
                    max_width = source_image_width * (old_div(
                        float(w_ratio), 100)) if w_ratio else float('inf')
                    max_height = source_image_height * (old_div(
                        float(h_ratio), 100)) if h_ratio else float('inf')
                    w_ratio = old_div(float(w_ratio),
                                      100.0) if w_ratio else False
                    h_ratio = old_div(float(h_ratio),
                                      100.0) if h_ratio else False

                    ratio = old_div(float(watermark_source_image_width),
                                    watermark_source_image_height)

                    watermark_image_width, watermark_image_height = filter.calc_watermark_size(
                        (source_image_width, source_image_height),
                        (watermark_source_image_width,
                         watermark_source_image_height), w_ratio, h_ratio)
                    watermark_image = old_div(float(watermark_image_width),
                                              watermark_image_height)

                    test = {
                        'source_image_width': source_image_width,
                        'source_image_height': source_image_height,
                        'watermark_source_image_width':
                        watermark_source_image_width,
                        'watermark_source_image_height':
                        watermark_source_image_height,
                        'watermark_image_width': watermark_image_width,
                        'watermark_image_height': watermark_image_height,
                        'w_ratio': w_ratio,
                        'h_ratio': h_ratio
                    }

                    test['topic_name'] = 'watermark_image_width'
                    expect(watermark_image_width).to_fit_into(
                        max_width, **test)
                    test['topic_name'] = 'watermark_image_height'
                    expect(watermark_image_height).to_fit_into(
                        max_height, **test)

                    test['topic_name'] = 'fill out'
                    expect((watermark_image_width == max_width
                            or watermark_image_height
                            == max_height)).to_be_true_with_additional_info(
                                **test)

                    test['topic_name'] = 'image ratio'
                    expect(watermark_image).to_almost_equal(ratio, 2, **test)
예제 #2
0
    def test_watermark_filter_calculated_position(self):
        watermark.Filter.pre_compile()
        filter = watermark.Filter("http://dummy,0,0,0", self.context)

        for length, pos, expected in POSITIONS:
            test = {
                'length': length,
                'pos': pos,
            }

            expect(filter.detect_and_get_ratio_position(
                pos,
                length)).to_be_equal_with_additional_info(expected, **test)
예제 #3
0
    async def test_watermark_validate_allowed_source(self):
        config = Config(
            ALLOWED_SOURCES=[
                "s.glbimg.com",
            ],
            LOADER="thumbor.loaders.http_loader",
        )
        importer = Importer(config)
        importer.import_modules()

        context = Context(config=config, importer=importer)
        filter_instance = watermark.Filter("", context)

        expect(
            filter_instance.validate("https://s2.glbimg.com/logo.jpg")
        ).to_be_false()
        expect(
            filter_instance.validate("https://s.glbimg.com/logo.jpg")
        ).to_be_true()
예제 #4
0
    async def test_watermark_filter_calculated_resizing(self):
        watermark.Filter.pre_compile()
        filter_instance = watermark.Filter("http://dummy,0,0,0", self.context)

        for source_image_width, source_image_height in SOURCE_IMAGE_SIZES:
            for (
                watermark_source_image_width,
                watermark_source_image_height,
            ) in WATERMARK_IMAGE_SIZES:
                for w_ratio, h_ratio in RATIOS:
                    max_width = (
                        source_image_width * (float(w_ratio) / 100)
                        if w_ratio
                        else float("inf")
                    )
                    max_height = (
                        source_image_height * (float(h_ratio) / 100)
                        if h_ratio
                        else float("inf")
                    )
                    w_ratio = float(w_ratio) / 100.0 if w_ratio else False
                    h_ratio = float(h_ratio) / 100.0 if h_ratio else False

                    ratio = (
                        float(watermark_source_image_width)
                        / watermark_source_image_height
                    )

                    (
                        watermark_image_width,
                        watermark_image_height,
                    ) = filter_instance.calc_watermark_size(
                        (source_image_width, source_image_height),
                        (
                            watermark_source_image_width,
                            watermark_source_image_height,
                        ),
                        w_ratio,
                        h_ratio,
                    )
                    watermark_image = (
                        float(watermark_image_width) / watermark_image_height
                    )

                    test = {
                        "source_image_width": source_image_width,
                        "source_image_height": source_image_height,
                        "watermark_source_image_width": watermark_source_image_width,
                        "watermark_source_image_height": watermark_source_image_height,
                        "watermark_image_width": watermark_image_width,
                        "watermark_image_height": watermark_image_height,
                        "w_ratio": w_ratio,
                        "h_ratio": h_ratio,
                    }

                    test["topic_name"] = "watermark_image_width"
                    expect(watermark_image_width).to_fit_into(
                        max_width, **test
                    )
                    test["topic_name"] = "watermark_image_height"
                    expect(watermark_image_height).to_fit_into(
                        max_height, **test
                    )

                    test["topic_name"] = "fill out"
                    expect(
                        (
                            watermark_image_width == max_width
                            or watermark_image_height == max_height
                        )
                    ).to_be_true_with_additional_info(**test)

                    test["topic_name"] = "image ratio"
                    expect(watermark_image).to_almost_equal(ratio, 2, **test)