Giter Site home page Giter Site logo

pontusab / react-native-image-manipulator Goto Github PK

View Code? Open in Web Editor NEW
21.0 1.0 39.0 1.11 MB

🗻Manipulate the image provided via uri. Available modifications are rotating, flipping (mirroring), resizing and cropping.

Java 34.23% JavaScript 5.17% Objective-C 17.01% Ruby 3.06% Python 2.23% TypeScript 38.29%
objective-c java react-native

react-native-image-manipulator's Introduction

An API to modify images stored on the local file system.

Installation

  1. yarn add https://github.com/pontusab/react-native-image-manipulator
  2. pod 'react-native-image-manipulator', :path => './node_modules/react-native-image-manipulator/ios'
  3. pod install

API

import * as ImageManipulator from 'react-native-image-manipulator';

ImageManipulator.manipulateAsync(uri, actions, saveOptions)

Manipulate the image provided via uri. Available modifications are rotating, flipping (mirroring), resizing and cropping. Each invocation results in a new file. With one invocation you can provide a set of actions to perform over the image. Overwriting the source file would not have an effect in displaying the result as images are cached.

Arguments

  • uri (string) -- URI of the file to manipulate. Should be on the local file system.

  • actions (array) --

    An array of objects representing manipulation options. Each object should have only one of the following keys that corresponds to specific transformation:

    • resize (object) -- An object of shape { width, height }. Values correspond to the result image dimensions. If you specify only one value, the other will be calculated automatically to preserve image ratio.
    • rotate (number) -- Degrees to rotate the image. Rotation is clockwise when the value is positive and counter-clockwise when negative.
    • flip (string) -- ImageManipulator.FlipType.{Vertical, Horizontal}. Only one flip per transformation is available. If you want to flip according to both axes then provide two separate transformations.
    • crop (object) -- An object of shape { originX, originY, width, height }. Fields specify top-left corner and dimensions of a crop rectangle.
  • saveOptions (object) -- A map defining how modified image should be saved:

    • compress (number) -- A value in range 0.0 - 1.0 specifying compression level of the result image. 1 means no compression (highest quality) and 0 the highest compression (lowest quality).
    • format (string) -- ImageManipulator.SaveFormat.{JPEG, PNG}. Specifies what type of compression should be used and what is the result file extension. SaveFormat.PNG compression is lossless but slower, SaveFormat.JPEG is faster but the image has visible artifacts. Defaults to SaveFormat.JPEG.
    • base64 (boolean) -- Whether to also include the image data in Base64 format.

Returns

Returns { uri, width, height } where uri is a URI to the modified image (useable as the source for an Image/Video element), width, height specify the dimensions of the image. It can contain also base64 - it is included if the base64 saveOption was truthy, and is a string containing the JPEG/PNG (depending on format) data of the image in Base64--prepend that with 'data:image/xxx;base64,' to get a data URI, which you can use as the source for an Image element for example (where xxx is 'jpeg' or 'png').

Basic Example

This will first rotate the image 90 degrees clockwise, then flip the rotated image vertically and save it as a PNG.

import React from 'react';
import { Button, View, Image } from 'react-native';
import { Asset } from 'expo-asset';
import * as ImageManipulator from 'react-native-image-manipulator';

export default class ImageManipulatorSample extends React.Component {
  state = {
    ready: false,
    image: null,
  };

  componentDidMount() {
    (async () => {
      const image = Asset.fromModule(require('./assets/snack-icon.png'));
      await image.downloadAsync();
      this.setState({
        ready: true,
        image,
      });
    })();
  }

  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center' }}>
        {this.state.ready && this._renderImage()}
        <Button title="Rotate and Flip" onPress={this._rotate90andFlip} />
      </View>
    );
  }

  _rotate90andFlip = async () => {
    const manipResult = await ImageManipulator.manipulateAsync(
      this.state.image.localUri || this.state.image.uri,
      [{ rotate: 90 }, { flip: ImageManipulator.FlipType.Vertical }],
      { compress: 1, format: ImageManipulator.SaveFormat.PNG }
    );
    this.setState({ image: manipResult });
  };

  _renderImage = () => {
    return (
      <View style={{ marginVertical: 20, alignItems: 'center', justifyContent: 'center' }}>
        <Image
          source={{ uri: this.state.image.localUri || this.state.image.uri }}
          style={{ width: 300, height: 300, resizeMode: 'contain' }}
        />
      </View>
    );
  };
}

react-native-image-manipulator's People

Contributors

pontusab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-native-image-manipulator's Issues

Can't find com.facebook.imagepipeline

Awesome library! I've got it up and running on iOS with RN 0.60 without any issues. On Android I'm getting dependency errors. Here's the first one in a long list:

> Task :react-native-image-manipulator:compileDebugJavaWithJavac FAILED
/Users/david/Documents/Business/Projects/PayTrace/Code/Paytrace-go/node_modules.nosync/react-native-image-manipulator/android/src/main/java/com/reactnativeimagemanipulator/ImageManipulatorModule.java:13: error: cannot find symbol
import com.facebook.imagepipeline.common.RotationOptions;
                                        ^
  symbol:   class RotationOptions
  location: package com.facebook.imagepipeline.common

Error when build APK

Task :@pontusab_react-native-image-manipulator:verifyReleaseResources FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':@pontusab_react-native-image-manipulator:verifyReleaseResources'.

A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
Android resource linking failed
/Users/anhdt/.gradle/caches/transforms-2/files-2.1/23bdf04dc9395ecc69d28ed4a9de945a/appcompat-1.0.2/res/values-v26/values-v26.xml:5:5-8:13: AAPT: error: resource android:attr/colorError not found.

 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/23bdf04dc9395ecc69d28ed4a9de945a/appcompat-1.0.2/res/values-v26/values-v26.xml:9:5-12:13: AAPT: error: resource android:attr/colorError not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/23bdf04dc9395ecc69d28ed4a9de945a/appcompat-1.0.2/res/values-v26/values-v26.xml:13:5-16:13: AAPT: error: style attribute 'android:attr/keyboardNavigationCluster' not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/23bdf04dc9395ecc69d28ed4a9de945a/appcompat-1.0.2/res/values-v28/values-v28.xml:5:5-8:13: AAPT: error: resource android:attr/dialogCornerRadius not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/23bdf04dc9395ecc69d28ed4a9de945a/appcompat-1.0.2/res/values-v28/values-v28.xml:9:5-12:13: AAPT: error: resource android:attr/dialogCornerRadius not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:57:5-88:25: AAPT: error: resource android:attr/fontStyle not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:57:5-88:25: AAPT: error: resource android:attr/font not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:57:5-88:25: AAPT: error: resource android:attr/fontWeight not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:57:5-88:25: AAPT: error: resource android:attr/fontVariationSettings not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:57:5-88:25: AAPT: error: resource android:attr/ttcIndex not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:89:5-125:25: AAPT: error: resource android:attr/startX not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:89:5-125:25: AAPT: error: resource android:attr/startY not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:89:5-125:25: AAPT: error: resource android:attr/endX not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:89:5-125:25: AAPT: error: resource android:attr/endY not found.
     
 /Users/anhdt/.gradle/caches/transforms-2/files-2.1/7f239875c19cf928f2890ed630c2fcc2/core-1.0.1/res/values/values.xml:126:5-132:25: AAPT: error: resource android:attr/offset not found.

Package not found

After running command: npm install react-native-image-manipulator --save
Getting this error:
npm ERR! 404 Not Found: react-native-image-manipulator@latest

Problems during installation

The following is displayed and I cannot proceed
pod install

No podspec found for react-native-image-manipulator in ../node_modules/react-native-image-manipulator/ios


pod 'React', :path => '../node_modules/react-native/'
pod 'react-native-image-manipulator', :path => '../node_modules/react-native-image-manipulator/ios'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.