Skip to content

Commit

Permalink
feat: remove pica from web
Browse files Browse the repository at this point in the history
Signed-off-by: Caijinglong <[email protected]>
  • Loading branch information
CaiJingLong committed Dec 4, 2024
1 parent dd5b101 commit 3b0d2e0
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 71 deletions.
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,27 +358,6 @@ but not `direction` information.

The web implementation is not required for many people,

This plugin uses [pica][] to implement compression.

Currently, [debug mode does not allow you to use the dynamic script loading scheme][flutter-126131].
And when you actually deploy, you may choose server deployment or cdn deployment, so here we suggest you add script node to head or body by yourself in your `<flutte_project>/web/index.html`.

[flutter-126131]: https://github.com/flutter/flutter/issues/126131

Add for `<flutte_project>/web/index.html`:

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pica.min.js" ></script>

<!-- or -->

<script src="https://unpkg.com/pica/dist/pica.min.js" ></script>
```

About web compatibility: two methods with file will throw an exception when used on the web.

[pica]: https://www.npmjs.com/package/pica?activeTab=readme

## About macOS

You need change the minimum deployment target to 10.15.
Expand All @@ -388,7 +367,6 @@ Open xcode project, select Runner target, and change the value of `macOS Deploym
And, change the `Podfile`:
Change `platform` to `platform :osx, '10.15'`.


## OpenHarmony

The currently supported image formats for parsing include JPEG, PNG, GIF, RAW, WebP, BMP, and SVG. However, the encoding output image formats are currently limited to JPEG, PNG, and WebP only.
Expand Down
2 changes: 0 additions & 2 deletions packages/flutter_image_compress/example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pica.min.js" ></script>

</head>
<body>
<script>
Expand Down
13 changes: 0 additions & 13 deletions packages/flutter_image_compress_web/assets/pica.min.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flutter_image_compress_platform_interface/flutter_image_compress
import 'package:flutter_image_compress_web/src/log.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

import 'src/pica.dart';
import 'src/compressor.dart';

class FlutterImageCompressWeb extends FlutterImageCompressPlatform {
static void registerWith(Registrar registrar) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
library pica;

import 'dart:convert';
import 'dart:js_interop';
import 'dart:typed_data';

import 'package:flutter_image_compress_platform_interface/flutter_image_compress_platform_interface.dart';
import 'package:web/web.dart';

import 'log.dart' as logger;
import 'util.dart';
import 'window.dart';

extension type Pica._(JSObject _) implements JSObject {
external JSPromise resize(
ImageBitmap from,
HTMLCanvasElement to,
);
}

Future<Uint8List> resizeWithList({
required Uint8List buffer,
Expand All @@ -26,15 +17,6 @@ Future<Uint8List> resizeWithList({
int quality = 88,
}) async {
final Stopwatch stopwatch = Stopwatch()..start();
final pica = window.pica() as Pica?;
if (pica == null) {
throw Exception(
'Pica not found. This plugin requires pica for image compression on the web. '
'See documentation for more details https://github.com/fluttercandies/flutter_image_compress?tab=readme-ov-file#web');
}
logger.jsLog('The pica instance', pica);
logger.jsLog('src image buffer', buffer);
logger.dartLog('src image buffer length: ${buffer.length}');
final bitmap = await buffer.toImageBitmap();

final srcWidth = bitmap.width;
Expand All @@ -45,18 +27,22 @@ Future<Uint8List> resizeWithList({
final width = srcWidth > minWidth ? minWidth : srcWidth;
final height = width ~/ ratio;

logger.jsLog('src size', '$srcWidth x $srcHeight');
logger.jsLog('target size', '$width x $height');

final canvas = HTMLCanvasElement();
canvas.width = width;
canvas.height = height;
await pica.resize(bitmap, canvas).toDart;

final ctx = canvas.getContext('2d') as CanvasRenderingContext2D?;
ctx?.clearRect(0, 0, width, height);
ctx?.drawImage(bitmap, 0, 0, width, height);

final blob = canvas.toDataUrl(format.type, quality / 100);
final str = blob.split(',')[1];

bitmap.close();
final result = base64Decode(str);
logger.jsLog('compressed image buffer', result);
logger.dartLog('compressed image buffer length: ${result.length}');
logger.dartLog('compressed took ${stopwatch.elapsedMilliseconds}ms');

Expand All @@ -74,8 +60,6 @@ extension CompressExt on CompressFormat {
return 'image/webp';
case CompressFormat.heic:
throw UnimplementedError('heic is not support web');
default:
return 'image/jpeg';
}
}
}
8 changes: 0 additions & 8 deletions packages/flutter_image_compress_web/lib/src/window.dart

This file was deleted.

6 changes: 3 additions & 3 deletions packages/flutter_image_compress_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 0.1.4+1
repository: https://github.com/fluttercandies/flutter_image_compress

environment:
sdk: '>=3.3.0 <4.0.0'
sdk: '>=2.12.0 <4.0.0'
flutter: ">=2.5.0"

dependencies:
Expand Down Expand Up @@ -43,8 +43,8 @@ flutter:
fileName: flutter_image_compress_web.dart

# To add assets to your plugin package, add an assets section, like this:
assets:
- assets/pica.min.js
# assets:
# - assets/pica.min.js
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
Expand Down

0 comments on commit 3b0d2e0

Please sign in to comment.