fluttercandies / flutter_image_compress

flutter image compress
MIT License
642 stars 217 forks source link

[Bug report] Hashing image before and after `compressWithFile` results diff depending on the format #313

Open raulmabe-labhouse opened 3 months ago

raulmabe-labhouse commented 3 months ago

Version

2.3.0

Platforms

dart

Device Model

iPhone 13

flutter info

[✓] Flutter (Channel stable, 3.22.1, on macOS 14.5 23F79 darwin-arm64, locale en-ES)
    • Flutter version 3.22.1 on channel stable at /Users/raulmateo/fvm/versions/3.22.1
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision a14f74ff3a (9 weeks ago), 2024-05-22 11:08:21 -0500
    • Engine revision 55eae6864b
    • Dart version 3.4.1
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/raulmateo/Library/Android/sdk
    • Platform android-34, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C65
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[✓] VS Code (version 1.91.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.92.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 126.0.6478.183

[✓] Network resources
    • All expected network resources are available.

• No issues found!

How to reproduce?

Import crypto package.

  1. Use sha256 to convert image to hash.
  2. Call the compressWithFile method with PNG.
  3. Use sha256 to hash the resulting image again
  4. Test that the first hash and the second hash are the same.

Now do the same with WEBP, and you'll see the hashes are different.

Logs

No response

Example code (optional)

Future<String> removeExif(String path) async {
    final bytes = await FlutterImageCompress.compressWithFile(path, quality: 100, format: CompressFormat.png);
    final newPath = await FilesMixin.getNewTempPath('tmp', 'png');
    File(newPath).writeAsBytesSync(bytes!);
    return newPath;
  }

  Future<String> generateUniqueImageHash(File file) async {
    final digest = await sha256.bind(file.openRead()).first;
    return digest.toString();
  }

Contact

raul.mateo@labhouse.io