flutter_webp

Flutter plugin for converting (encoding) images to webp format.


License
MIT

Documentation

flutter_webp

Convert and save image as WebP.

This library currently working only on Android. We need your help!

Usage

dependencies:
  flutter_webp: ^0.2.0
import 'package:flutter_webp/flutter_webp.dart';

Use as:

  Future<List<int>> testCompressFile(File file) async {
    var result = await FlutterWebp.compressWithFile(
      file.absolute.path,
      width: 2300,
      height: 1500,
      quality: 94,
      rotate: 90,
    );
    print(file.lengthSync());
    print(result.length);
    return result;
  }

  Future<File> testCompressAndGetFile(File file, String targetPath) async {
    var result = await FlutterWebp.compressAndGetFile(
        file.absolute.path,
        targetPath,
        height: 1920,
        width: 1080,
        quality: 88,
        rotate: 180,
      );

    print(file.lengthSync());
    print(result.lengthSync());

    return result;
  }

  Future<List<int>> testCompressAsset(String assetName) async {
    var list = await FlutterWebp.compressAssetImage(
      assetName,
      height: 1920,
      width: 1080,
      quality: 96,
      rotate: 180,
    );

    return list;
  }

  Future<List<int>> testComporessList(List<int> list) async {
    var result = await FlutterWebp.compressWithList(
      list,
      height: 1920,
      width: 1080,
      quality: 96,
      rotate: 135,
    );
    print(list.length);
    print(result.length);
    return result;
  }

Result

The result of returning a List collection will not have null, but will always be an empty array.

The returned file may be null. In addition, please decide for yourself whether the file exists.

About List<int> and Uint8List

You may need to convert List<int> to Uint8List to display images.

To use Uint8List, you need import package to your code like so:

img

final image = Uint8List.fromList(imageList)
ImageProvider provider = MemoryImage(Uint8List.fromList(imageList));

Usage in Image Widget:

List<int> image = await testCompressFile(file);
ImageProvider provider = MemoryImage(Uint8List.fromList(image));

Image(
  image: provider ?? AssetImage("img/img.jpg"),
),

Write to file usage:

void writeToFile(List<int> image, String filePath) {
  final file = File(filePath);
  file.writeAsBytes(image, flush: true, mode: FileMode.write);
}

Troubleshooting

Compressing returns null

Sometimes, compressing will return null. You should check if you can read/write the file, and the parent folder of the target file must exist.

For example, use the path_provider plugin to access some application folders, and use a permission plugin to request permission to access SD cards on Android/iOS.

Android build error

Caused by: org.gradle.internal.event.ListenerNotificationException: Failed to notify project evaluation listener.
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:86)
        ...
Caused by: java.lang.AbstractMethodError
        at org.jetbrains.kotlin.gradle.plugin.KotlinPluginKt.resolveSubpluginArtifacts(KotlinPlugin.kt:776)
        ...

See flutter/flutter/issues#21473

You need to upgrade your Kotlin version to 1.2.71+.

If Flutter supports more platforms (Windows, Mac, Linux, etc) in the future and you use this library, propose an issue or PR!