Ejemplo n.º 1
0
    def __call__(self, *args, **kw):
        if len(args) < 1:
            cherrypy.response.status = 404
            return "not found"

        path = "/".join(args)

        image = jpeg(path=path)

        cache_full_path = None

        if self.should_cache:
            cache_full_path = self.get_cache_path(path)
            if self.fs.exists(cache_full_path):
                return static.serve_file(cache_full_path, 'image/jpeg')

        if len(args) >= 3 and args[0] == 'crop':
            proportion = re.match(r'(?P<width>\d+)x(?P<height>\d+)', args[1])
            if proportion:
                width = int(proportion.group('width'))
                height = int(proportion.group('height'))

                image = picture(path="/".join(args[2:]),
                                width=width,
                                height=height)

        if self.should_cache:
            dir_path = self.fs.dirname(cache_full_path)
            self.fs.mkdir(dir_path)
            img_file = self.fs.open_raw(cache_full_path, 'w')
            img_file.write(image)
            img_file.close()

        return image
Ejemplo n.º 2
0
    def __call__(self, *args, **kw):
        if len(args) < 1:
            cherrypy.response.status = 404
            return "not found"

        path = "/".join(args)

        image = jpeg(path=path)

        cache_full_path = None

        if self.should_cache:
            cache_full_path = self.get_cache_path(path)
            if self.fs.exists(cache_full_path):
                return static.serve_file(cache_full_path, 'image/jpeg')

        if len(args) >= 3 and args[0] == 'crop':
            proportion = re.match(r'(?P<width>\d+)x(?P<height>\d+)',
                                  args[1])
            if proportion:
                width = int(proportion.group('width'))
                height = int(proportion.group('height'))

                image = picture(path="/".join(args[2:]),
                                width=width,
                                height=height)

        if self.should_cache:
            dir_path = self.fs.dirname(cache_full_path)
            self.fs.mkdir(dir_path)
            img_file = self.fs.open_raw(cache_full_path, 'w')
            img_file.write(image)
            img_file.close()

        return image
Ejemplo n.º 3
0
def test_picture_with_center_true_will_create_new_image_and_paste():
    base_path = '/base/path'
    path = 'image.jpg'

    mox = Mox()

    mox.StubOutWithMock(image, 'Image')
    mox.StubOutWithMock(image, 'StringIO')

    img_mock = mox.CreateMockAnything()
    img_mock.size = 300, 300

    stringio_mock = mox.CreateMockAnything()
    return_mock = mox.CreateMockAnything()

    stringio_mock.getvalue().AndReturn(return_mock)
    image.StringIO.StringIO().AndReturn(stringio_mock)

    cherrypy.config['image.dir'] = base_path

    new_img_mock = mox.CreateMockAnything()

    new_img_mock.paste(img_mock, (-100, -100))
    new_img_mock.save(stringio_mock, 'JPEG', quality=100)

    image.Image.open(join(base_path, path)).AndReturn(img_mock)
    image.Image.new('RGBA', (100, 100), 0xffffff).AndReturn(new_img_mock)

    mox.ReplayAll()
    ret = image.picture(path, 100, 100, crop=False, center=True)

    assert ret == return_mock, "Expected %r. Got %r." % (return_mock, ret)
    mox.VerifyAll()

    del cherrypy.config['image.dir']
Ejemplo n.º 4
0
def test_picture_with_crop_true_will_crop_to_fit():
    base_path = '/basepath/for/test_picture_success'
    path = 'my_picture.jpg'

    mox = Mox()

    mox.StubOutWithMock(image, 'Image')
    mox.StubOutWithMock(image, 'StringIO')
    mox.StubOutWithMock(image, 'crop_to_fit')

    img_mock = mox.CreateMockAnything()
    img_mock.size = 300, 300

    stringio_mock = mox.CreateMockAnything()
    return_mock = mox.CreateMockAnything()

    stringio_mock.getvalue().AndReturn(return_mock)

    image.StringIO.StringIO().AndReturn(stringio_mock)

    cherrypy.config['image.dir'] = base_path

    image.Image.open(join(base_path, path)).AndReturn(img_mock)
    img_mock.save(stringio_mock, 'JPEG', quality=100)
    image.crop_to_fit(img_mock, (100, 100)).AndReturn(img_mock)

    mox.ReplayAll()

    ret = image.picture(path, 100, 100, crop=True, center=False)
    assert ret == return_mock, "Expected %r. Got %r." % (return_mock, ret)

    mox.VerifyAll()
    del cherrypy.config['image.dir']
Ejemplo n.º 5
0
def test_picture_with_center_true_will_create_new_image_and_paste():
    base_path = '/base/path'
    path = 'image.jpg'

    mox = Mox()

    mox.StubOutWithMock(image, 'Image')
    mox.StubOutWithMock(image, 'StringIO')

    img_mock = mox.CreateMockAnything()
    img_mock.size = 300, 300

    stringio_mock = mox.CreateMockAnything()
    return_mock = mox.CreateMockAnything()

    stringio_mock.getvalue().AndReturn(return_mock)
    image.StringIO.StringIO().AndReturn(stringio_mock)

    cherrypy.config['image.dir'] = base_path

    new_img_mock = mox.CreateMockAnything()

    new_img_mock.paste(img_mock, (-100, -100))
    new_img_mock.save(stringio_mock, 'JPEG', quality=100)

    image.Image.open(join(base_path, path)).AndReturn(img_mock)
    image.Image.new('RGBA', (100, 100), 0xffffff).AndReturn(new_img_mock)

    mox.ReplayAll()
    ret = image.picture(path, 100, 100, crop=False, center=True)

    assert ret == return_mock, "Expected %r. Got %r." % (return_mock, ret)
    mox.VerifyAll()

    del cherrypy.config['image.dir']
Ejemplo n.º 6
0
def test_picture_with_crop_true_will_crop_to_fit():
    base_path = '/basepath/for/test_picture_success'
    path = 'my_picture.jpg'

    mox = Mox()

    mox.StubOutWithMock(image, 'Image')
    mox.StubOutWithMock(image, 'StringIO')
    mox.StubOutWithMock(image, 'crop_to_fit')

    img_mock = mox.CreateMockAnything()
    img_mock.size = 300, 300

    stringio_mock = mox.CreateMockAnything()
    return_mock = mox.CreateMockAnything()

    stringio_mock.getvalue().AndReturn(return_mock)

    image.StringIO.StringIO().AndReturn(stringio_mock)

    cherrypy.config['image.dir'] = base_path

    image.Image.open(join(base_path, path)).AndReturn(img_mock)
    img_mock.save(stringio_mock, 'JPEG', quality=100)
    image.crop_to_fit(img_mock, (100, 100)).AndReturn(img_mock)

    mox.ReplayAll()

    ret = image.picture(path, 100, 100, crop=True, center=False)
    assert ret == return_mock, "Expected %r. Got %r." % (return_mock, ret)

    mox.VerifyAll()
    del cherrypy.config['image.dir']