├── LICENSE ├── README.md ├── android ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── existfragger │ └── rnimagesize │ ├── RNImageSizeModule.java │ └── RNImageSizePackage.java ├── index.d.ts ├── index.js ├── js ├── RNImageSize.android.js ├── RNImageSize.ios.js └── RNImageSize.windows.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 I.P. Sorokin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## react-native-image-size (iOS + Android) 2 | 3 | --- 4 | 5 | Friends, sorry, but I can't now support this project as too busy and I am not working with react-native projects already few years.... 6 | 7 | If someone believes that he can support the project - let me know and I can add you to the Collaborators 8 | (existfragger@gmail.com) 9 | 10 | --- 11 | 12 | 13 | [![NPM version](https://badge.fury.io/js/react-native-image-size.svg)](http://badge.fury.io/js/react-native-image-size) 14 | 15 | Android 4.0 (API level 14) introduced the ability to get original image size. 16 | 17 | iOS uses Image.getSize https://facebook.github.io/react-native/docs/image#getsize 18 | 19 | 20 | ### Installation 21 | 22 | Download via NPM 23 | 24 | ```shell 25 | npm i -S react-native-image-size 26 | ``` 27 | 28 | Download via Yarn 29 | 30 | ```shell 31 | yarn add react-native-image-size 32 | ``` 33 | 34 | Afterward make sure to rebuild app, not just refresh bundler. 35 | 36 | ## Linking (for React Native <= 0.59 only, React Native >= 0.60 skip this as auto-linking should work) 37 | 38 | **-- Automaticaly --** 39 | 40 | Link, either via `react-native link` or manually 41 | 42 | ```shell 43 | react-native link react-native-image-size 44 | ``` 45 | 46 | **-- Manually --** 47 | 48 | #### android/settings.gradle 49 | ```diff 50 | +include ':react-native-image-size' 51 | +project(':react-native-image-size').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-image-size/android') 52 | ``` 53 | #### android/app/build.gradle 54 | ```diff 55 | dependencies { 56 | ... 57 | + implementation project(':react-native-image-size') 58 | ... 59 | } 60 | ``` 61 | #### android/app/src/main/java/.../MainApplication.java 62 | ```diff 63 | +import com.existfragger.rnimagesize.RNImageSizePackage; 64 | ... 65 | @Override 66 | protected List getPackages() { 67 | return Arrays.<~>asList( 68 | - new MainReactPackage() 69 | + new MainReactPackage(), 70 | + new RNImageSizePackage() 71 | ); 72 | } 73 | ``` 74 | 75 | ### How to use 76 | 77 | ```js 78 | import ImageSize from 'react-native-image-size' 79 | ... 80 | ImageSize.getSize(uri).then(size => { 81 | // size.height 82 | // size.width 83 | }) 84 | ``` 85 | 86 | You can also use async/await, if you would prefer. 87 | 88 | ```js 89 | import ImageSize from 'react-native-image-size' 90 | ... 91 | foo = async () => { 92 | const { width, height } = await ImageSize.getSize(uri); 93 | // do stuff with width and height 94 | } 95 | ``` 96 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | jcenter() 5 | } 6 | 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.3.0' 9 | } 10 | } 11 | 12 | apply plugin: 'com.android.library' 13 | 14 | def safeExtGet(prop, fallback) { 15 | rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback 16 | } 17 | 18 | android { 19 | compileSdkVersion safeExtGet('compileSdkVersion', 28) 20 | buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3') 21 | 22 | defaultConfig { 23 | minSdkVersion safeExtGet('minSdkVersion', 19) 24 | targetSdkVersion safeExtGet('targetSdkVersion', 28) 25 | versionCode 1 26 | versionName "1.0" 27 | } 28 | lintOptions { 29 | abortOnError false 30 | } 31 | } 32 | 33 | repositories { 34 | mavenCentral() 35 | } 36 | 37 | dependencies { 38 | implementation "com.facebook.react:react-native:${safeExtGet('reactNative', '+')}" 39 | } 40 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /android/src/main/java/com/existfragger/rnimagesize/RNImageSizeModule.java: -------------------------------------------------------------------------------- 1 | package com.existfragger.rnimagesize; 2 | 3 | import android.content.ContentResolver; 4 | import android.graphics.Bitmap; 5 | import android.graphics.BitmapFactory; 6 | import android.media.ExifInterface; 7 | import android.net.Uri; 8 | import android.os.Build; 9 | 10 | import com.facebook.react.bridge.Arguments; 11 | import com.facebook.react.bridge.Promise; 12 | import com.facebook.react.bridge.ReactApplicationContext; 13 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 14 | import com.facebook.react.bridge.ReactMethod; 15 | import com.facebook.react.bridge.WritableMap; 16 | 17 | import java.io.InputStream; 18 | import java.net.URL; 19 | 20 | public class RNImageSizeModule extends ReactContextBaseJavaModule { 21 | private final ReactApplicationContext reactContext; 22 | 23 | public RNImageSizeModule(final ReactApplicationContext reactContext) { 24 | super(reactContext); 25 | this.reactContext = reactContext; 26 | } 27 | 28 | @ReactMethod 29 | public void getSize(String uri, final Promise promise) { 30 | try { 31 | Uri u = Uri.parse(uri); 32 | String scheme = u.getScheme(); 33 | BitmapFactory.Options options = new BitmapFactory.Options(); 34 | options.inJustDecodeBounds = true; 35 | ExifInterface exifInterface = null; 36 | 37 | int height = 0; 38 | int width = 0; 39 | int rotation = 0; 40 | 41 | if (ContentResolver.SCHEME_FILE.equals(scheme) || ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { 42 | // ContentResolver.openInputStream() can only handle SCHEME_FILE, SCHEME_CONTENT and SCHEME_ANDROID_RESOURCE 43 | // https://developer.android.com/reference/android/content/ContentResolver?hl=en#openInputStream(android.net.Uri) 44 | ContentResolver contentResolver = getReactApplicationContext().getContentResolver(); 45 | BitmapFactory.decodeStream( 46 | contentResolver.openInputStream(u), 47 | null, 48 | options 49 | ); 50 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 51 | exifInterface = new ExifInterface(contentResolver.openInputStream(u)); 52 | } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { 53 | // For SCHEME_FILE, Use ExifInterface(String filename) if the SDK version is less than 24(N) 54 | exifInterface = new ExifInterface(u.getPath()); 55 | } 56 | } else { 57 | // ContentResolver.openInputStream() cannot handle this scheme, treat it as remote uri 58 | URL url = new URL(uri); 59 | BitmapFactory.decodeStream(url.openStream(), null, options); 60 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 61 | exifInterface = new ExifInterface(url.openStream()); 62 | } 63 | } 64 | height = options.outHeight; 65 | width = options.outWidth; 66 | if (exifInterface != null) { 67 | int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 68 | if (orientation == ExifInterface.ORIENTATION_ROTATE_90) 69 | rotation = 90; 70 | else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) 71 | rotation = 180; 72 | else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) 73 | rotation = 270; 74 | } 75 | 76 | WritableMap map = Arguments.createMap(); 77 | 78 | map.putInt("height", height); 79 | map.putInt("width", width); 80 | map.putInt("rotation", rotation); 81 | 82 | promise.resolve(map); 83 | } catch (Exception e) { 84 | promise.reject(e); 85 | } 86 | } 87 | 88 | @Override 89 | public String getName() { 90 | return "RNImageSize"; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /android/src/main/java/com/existfragger/rnimagesize/RNImageSizePackage.java: -------------------------------------------------------------------------------- 1 | package com.existfragger.rnimagesize; 2 | 3 | import com.facebook.react.ReactPackage; 4 | import com.facebook.react.bridge.NativeModule; 5 | import com.facebook.react.bridge.ReactApplicationContext; 6 | import com.facebook.react.uimanager.ViewManager; 7 | 8 | import java.util.ArrayList; 9 | import java.util.Collections; 10 | import java.util.List; 11 | 12 | public class RNImageSizePackage implements ReactPackage { 13 | @Override 14 | public List createViewManagers(ReactApplicationContext reactContext) { 15 | return Collections.emptyList(); 16 | } 17 | 18 | @Override 19 | public List createNativeModules(ReactApplicationContext reactContext) { 20 | List modules = new ArrayList<>(); 21 | modules.add(new RNImageSizeModule(reactContext)); 22 | return modules; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export function getSize(uri: string): Promise<{ width: number; height: number; rotation?: number; }>; 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./js/RNImageSize'); 2 | -------------------------------------------------------------------------------- /js/RNImageSize.android.js: -------------------------------------------------------------------------------- 1 | import { NativeModules } from 'react-native'; 2 | 3 | const { RNImageSize } = NativeModules; 4 | 5 | export default { getSize: RNImageSize.getSize }; 6 | -------------------------------------------------------------------------------- /js/RNImageSize.ios.js: -------------------------------------------------------------------------------- 1 | import { Image } from 'react-native'; 2 | 3 | const getSize = (uri) => { 4 | return new Promise((resolve, reject) => { 5 | Image.getSize(uri, (width, height) => resolve({ width, height }), reject); 6 | }) 7 | } 8 | 9 | export default { getSize }; 10 | -------------------------------------------------------------------------------- /js/RNImageSize.windows.js: -------------------------------------------------------------------------------- 1 | import { Image } from 'react-native'; 2 | 3 | const getSize = (uri) => { 4 | return new Promise((resolve, reject) => { 5 | Image.getSize(uri, (width, height) => resolve({ width, height }), reject); 6 | }) 7 | } 8 | 9 | export default { getSize }; 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-image-size", 3 | "description": "Get original image size (iOS + Android)", 4 | "main": "index.js", 5 | "author": "Ivan Sorokin ", 6 | "version": "1.1.6", 7 | "repository": { 8 | "type": "git", 9 | "url": "git+https://github.com/eXist-FraGGer/react-native-image-size.git" 10 | }, 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "homepage": "https://github.com/eXist-FraGGer/react-native-image-size#readme", 15 | "keywords": [ 16 | "react", 17 | "react-native", 18 | "image", 19 | "size", 20 | "dimensions", 21 | "resolution", 22 | "width", 23 | "height", 24 | "android", 25 | "ios" 26 | ], 27 | "license": "MIT" 28 | } 29 | --------------------------------------------------------------------------------