1.0.2 • Published 5 years ago

@jarred/react-native-photo-manipulator v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

React Native Photo Manipulator

Image processing library to edit photo programmatically in React Native

Build Status npm version

Getting started

$ yarn add react-native-photo-manipulator

Mostly automatic installation

$ react-native link react-native-photo-manipulator

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-photo-manipulator and add RNPhotoManipulator.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNPhotoManipulator.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.guhungry.rnphotomanipulator.RNPhotoManipulatorPackage; to the imports at the top of the file
  • Add new RNPhotoManipulatorPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-photo-manipulator'
    project(':react-native-photo-manipulator').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-photo-manipulator/android')
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      implementation project(':react-native-photo-manipulator')

Usage

Import library with

import RNPhotoManipulator from 'react-native-photo-manipulator';

API

Types

ImageSource

Image resource can be url or require()

TypeDescription
numberImage from require('path/to/image')
stringImage from url supports file://, http://, https:// and ftp://

PhotoBatchOperations

Represent overlay image or print text operation

PhotoBatchOverlay

Overlay image batch operation

PropertyTypeDescription
operation"overlay"
overlayImageSourceThe overlay image
positionPointhe position of overlay image in background image

PhotoBatchPrintText

Print text batch operation

PropertyTypeDescription
operation"text"
optionsTextOptionsThe text attributes

Point

Represent position (x, y) from top-left of image

PropertyTypeDescription
xnumberThe x-axis coordinate from top-left
ynumberThe y-axis coordinate from top-left

Rect

Represent area of region

PropertyTypeDescription
xnumberThe x-axis coordinate from top-left
ynumberThe y-axis coordinate from top-left
widthnumberThe width of the region
heightnumberThe height of the region

Size

Represent size (width, height) of region or image

PropertyTypeDescription
widthnumberThe width of the region
heightnumberThe height of the region

TextOptions

Represent attributes of text such as text, color, size, and etc.

PropertyTypeDescription
positionPointThe position of the text in background image
textstringThe value of the text
textSizenumberThe size of the text
colorstringThe color of the text
thicknessnumberThe thickness (border width) of the region

Method

Crop and resize

Crop image with cropRegion and resize to targetSize if specified

Signature
static crop(image: ImageSource, cropRegion: Rect, targetSize?: Size) => Promise<string>
ParameterTypeRequiredDescription
imageImageSourceYesThe image
cropRegionRectYesThe region of image to be cropped
targetSizeSizeNoThe target size of result image
Returns

Promise with image path in cache directory

Example
const image = "https://unsplash.com/photos/qw6qQQyYQpo/download?force=true";
const cropRegion = { x: 5, y: 30, size: 400, width: 250 };
const targetSize = { size: 200, width: 150 };

PhotoManipulator.crop(image, cropRegion, targetSize).then(path => {
    console.log(`Result image path: ${path}`);
});

Optimize

Save result image with specified quality between 0 - 100 in jpeg format

Signature
static optimize(image: ImageSource, quality: number) => Promise<string>
ParameterTypeRequiredDescription
imageImageSourceYesThe image
qualitynumberYesThe quality of result image between 0 - 100
Returns

Promise with image path in cache directory

Example
const image = "https://unsplash.com/photos/qw6qQQyYQpo/download?force=true";
const quality = 90;

PhotoManipulator.optimize(image, 90).then(path => {
    console.log(`Result image path: ${path}`);
});

Overlay Image

Overlay image on top of background image

Signature
static overlayImage(image: ImageSource, overlay: ImageSource, position: Point) => Promise<string>
ParameterTypeRequiredDescription
imageImageSourceYesThe background image
overlayImageSourceYesThe overlay image
positionPointYesThe position of overlay image in background image
Returns

Promise with image path in cache directory

Example
const image = "https://unsplash.com/photos/qw6qQQyYQpo/download?force=true";
const overlay = "https://www.iconfinder.com/icons/1174949/download/png/128";
const position = { x: 5, y: 20 };

PhotoManipulator.overlayImage(image, overlay, position).then(path => {
    console.log(`Result image path: ${path}`);
});

Print Text

Print texts into image

Signature
static printText(image: ImageSource, texts: TextOptions[]) => Promise<string>
ParameterTypeRequiredDescription
imageImageSourceYesThe image
textsTextOptions[]YesThe list of text operations
Returns

Promise with image path in cache directory

Example
const image = "https://unsplash.com/photos/qw6qQQyYQpo/download?force=true";
const texts = [
    { position: { x: 50, y: 30 }, text: "Text 1", textSize: 30, color: "#000000" },
    { position: { x: 50, y: 30 }, text: "Text 1", textSize: 30, color: "#FFFFFF", thickness: 3 }
];

PhotoManipulator.printText(image, texts).then(path => {
    console.log(`Result image path: ${path}`);
});

Batch

Crop, resize and do operations (overlay and printText) on image

Signature
static batch(image: ImageSource, operations: PhotoBatchOperations[], cropRegion: Rect, targetSize?: Size, quality?: number) => Promise<string>
ParameterTypeRequiredDescription
imageImageSourceYesThe image
operationsPhotoBatchOperations[]YesThe list of operations
cropRegionRectYesThe region of image to be cropped
targetSizeSizeNoThe target size of result image
qualitynumberNoThe quality of result image between 0 - 100
Returns

Promise with image path in cache directory

Example
const image = "https://unsplash.com/photos/qw6qQQyYQpo/download?force=true";
const cropRegion = { x: 5, y: 30, size: 400, width: 250 };
const targetSize = { size: 200, width: 150 };
const operations = [
    { operation: "text", options: { position: { x: 50, y: 30 }, text: "Text 1", textSize: 30, color: "#000000" } },
    { operation: "overlay", overlay: "https://www.iconfinder.com/icons/1174949/download/png/128", position: { x: 5, y: 20 } },
];
const quality = 90;

PhotoManipulator.batch(image, cropRegion, targetSize, operations, quality).then(path => {
    console.log(`Result image path: ${path}`);
});