1import * as ImageManipulator from 'expo-image-manipulator'; 2import * as FileSystem from 'expo-file-system'; 3import { Asset } from 'expo-asset'; 4 5export const name = 'ImageManipulator'; 6 7export async function test(t) { 8 t.describe('ImageManipulator', async () => { 9 let image; 10 11 t.beforeAll(async () => { 12 image = Asset.fromModule(require('../assets/example_image_1.jpg')); 13 await image.downloadAsync(); 14 }); 15 16 t.describe('manipulateAsync()', async () => { 17 t.it('returns valid image', async () => { 18 const result = await ImageManipulator.manipulateAsync(image.localUri, [ 19 { resize: { width: 100, height: 100 } }, 20 ]); 21 t.expect(result).toBeDefined(); 22 t.expect(typeof result.uri).toBe('string'); 23 t.expect(typeof result.width).toBe('number'); 24 t.expect(typeof result.height).toBe('number'); 25 }); 26 27 t.it('saves with default format', async () => { 28 const result = await ImageManipulator.manipulateAsync(image.localUri, [ 29 { resize: { width: 100, height: 100 } }, 30 ]); 31 t.expect(result.uri.endsWith('.jpg')).toBe(true); 32 }); 33 34 t.it('saves as JPEG', async () => { 35 const result = await ImageManipulator.manipulateAsync( 36 image.localUri, 37 [{ resize: { width: 100, height: 100 } }], 38 { 39 format: ImageManipulator.SaveFormat.JPEG, 40 } 41 ); 42 43 t.expect(result.uri.endsWith('.jpg')).toBe(true); 44 }); 45 46 t.it('saves as PNG', async () => { 47 const result = await ImageManipulator.manipulateAsync( 48 image.localUri, 49 [{ resize: { width: 100, height: 100 } }], 50 { 51 format: ImageManipulator.SaveFormat.PNG, 52 } 53 ); 54 55 t.expect(result.uri.endsWith('.png')).toBe(true); 56 }); 57 58 t.it('provides Base64', async () => { 59 const result = await ImageManipulator.manipulateAsync( 60 image.localUri, 61 [{ resize: { width: 100, height: 100 } }], 62 { 63 base64: true, 64 } 65 ); 66 67 t.expect(typeof result.base64).toBe('string'); 68 }); 69 70 t.it('performs compression', async () => { 71 const result = await ImageManipulator.manipulateAsync( 72 image.localUri, 73 [{ flip: ImageManipulator.FlipType.Vertical }], 74 { 75 compress: 0.0, 76 } 77 ); 78 79 const imageInfo = await FileSystem.getInfoAsync(image.localUri); 80 const resultInfo = await FileSystem.getInfoAsync(result.uri); 81 82 t.expect(imageInfo.size).toBeGreaterThan(resultInfo.size); 83 }); 84 85 t.it('rotates images', async () => { 86 const result = await ImageManipulator.manipulateAsync(image.localUri, [{ rotate: 45 }]); 87 t.expect(result.width).toBeGreaterThan(image.width); 88 }); 89 90 t.it('flips horizontally', async () => { 91 const result = await ImageManipulator.manipulateAsync(image.localUri, [ 92 { flip: ImageManipulator.FlipType.Horizontal }, 93 ]); 94 t.expect(result.width).toBe(image.width); 95 t.expect(result.height).toBe(image.height); 96 }); 97 98 t.it('flips vertically', async () => { 99 const result = await ImageManipulator.manipulateAsync(image.localUri, [ 100 { flip: ImageManipulator.FlipType.Vertical }, 101 ]); 102 t.expect(result.width).toBe(image.width); 103 t.expect(result.height).toBe(image.height); 104 }); 105 106 t.it('resizes image', async () => { 107 const result = await ImageManipulator.manipulateAsync(image.localUri, [ 108 { resize: { width: 100, height: 100 } }, 109 ]); 110 t.expect(result.height).toBe(100); 111 t.expect(result.width).toBe(100); 112 }); 113 114 t.it('cropes image', async () => { 115 const result = await ImageManipulator.manipulateAsync(image.localUri, [ 116 { crop: { originX: 20, originY: 20, width: 100, height: 100 } }, 117 ]); 118 t.expect(result.height).toBe(100); 119 t.expect(result.width).toBe(100); 120 }); 121 122 t.it('performs multiple transformations', async () => { 123 const result = await ImageManipulator.manipulateAsync(image.localUri, [ 124 { resize: { width: 200, height: 200 } }, 125 { flip: ImageManipulator.FlipType.Vertical }, 126 { rotate: 45 }, 127 { crop: { originX: 20, originY: 20, width: 100, height: 100 } }, 128 ]); 129 t.expect(result.height).toBe(100); 130 t.expect(result.width).toBe(100); 131 }); 132 }); 133 }); 134} 135