Skip to content

fix : image compress above 10mb #1462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions lib/widgets/compose_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mime/mime.dart';

import 'package:path_provider/path_provider.dart';
import 'package:image/image.dart' as img;
import '../api/exception.dart';
import '../api/model/model.dart';
import '../api/route/messages.dart';
Expand All @@ -21,7 +22,6 @@ import 'inset_shadow.dart';
import 'store.dart';
import 'text.dart';
import 'theme.dart';

/// Compose-box styles that differ between light and dark theme.
///
/// These styles will animate on theme changes (with help from [lerp]).
Expand Down Expand Up @@ -966,19 +966,23 @@ class _AttachMediaButton extends _AttachUploadsButton {
const _AttachMediaButton({required super.controller});

@override
IconData get icon => ZulipIcons.image;
Future<Iterable<_File>> getFiles(BuildContext context) async {
final files = await _getFilePickerFiles(context, FileType.media);

@override
String tooltip(ZulipLocalizations zulipLocalizations) =>
zulipLocalizations.composeBoxAttachMediaTooltip;
// Compress each image file before returning
return Future.wait(files.map((file) async {
File selectedFile = File(file.path);

@override
Future<Iterable<_File>> getFiles(BuildContext context) async {
// TODO(#114): This doesn't give quite the right UI on Android.
return _getFilePickerFiles(context, FileType.media);
// Compress the image
File compressedFile = await compressImage(selectedFile);

// Create a new _File object with the compressed file path
return _File(path: compressedFile.path, name: file.name, size: compressedFile.lengthSync());
}));
}
}


class _AttachFromCameraButton extends _AttachUploadsButton {
const _AttachFromCameraButton({required super.controller});

Expand All @@ -1000,8 +1004,17 @@ class _AttachFromCameraButton extends _AttachUploadsButton {
// so just stick with images for now. We could add another button for
// videos, but we don't want too many buttons.
result = await ZulipBinding.instance.pickImage(
source: ImageSource.camera, requestFullMetadata: false);
} catch (e) {
source: ImageSource.camera, requestFullMetadata: false);

if (result != null && result.path != null) {
File selectedFile = File(result.path);

// Compress the image before using it
File compressedFile = await compressImage(selectedFile);

// Use compressedFile instead of selectedFile
uploadImage(compressedFile);
} catch (e) {
if (!context.mounted) return [];
if (e is PlatformException && e.code == 'camera_access_denied') {
// iOS has a quirk where it will only request the native
Expand Down Expand Up @@ -1050,7 +1063,7 @@ class _AttachFromCameraButton extends _AttachUploadsButton {
)];
}
}

}
class _SendButton extends StatefulWidget {
const _SendButton({required this.controller, required this.getDestination});

Expand Down Expand Up @@ -1597,3 +1610,15 @@ class _ComposeBoxState extends State<ComposeBox> with PerAccountStoreAwareStateM
return _ComposeBoxContainer(body: body, banner: null);
}
}
Future<File> compressImage(File imageFile) async {
final int maxSize = 10 * 1024 * 1024; // 10MB
if (imageFile.lengthSync() > maxSize) {
img.Image? image = img.decodeImage(imageFile.readAsBytesSync());
if (image != null) {
List<int> compressedBytes = img.encodeJpg(image, quality: 80);
File compressedFile = File(imageFile.path)..writeAsBytesSync(compressedBytes);
return compressedFile;
}
}
return imageFile;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Sat Mar 22 09:43:45 IST 2025
gradle.version=8.9
Empty file.
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
html: ^0.15.1
http: ^1.0.0
http_parser: ^4.0.2
image: ^4.5.4
image_picker: ^1.0.0
json_annotation: ^4.9.0
mime: ^2.0.0
Expand All @@ -64,6 +65,7 @@ dependencies:
wakelock_plus: ^1.2.8
zulip_plugin:
path: ./packages/zulip_plugin

# Keep list sorted when adding dependencies; it helps prevent merge conflicts.

dependency_overrides:
Expand Down
Loading