10

I need to resize images before uploading to the server.

I am using io and image package.

import 'dart:io';  
import 'package:image/image.dart' as Img;

using this function

uploadImages(File image_File) async {

    Img.Image image_temp = Img.decodeImage(image_File.readAsBytesSync());

    Img.Image resized_img = Img.copyResize(image_temp, 800);

    File resized_file = File('resized_img.jpg')
      ..writeAsBytesSync(Img.encodeJpg(resized_img));

    var stream = new http.ByteStream(DelegatingStream.typed(resized_file.openRead()));
    var length = await resized_file.length();

    var uri = Uri.parse("https://myserver.com/upload.php");

    var request = new http.MultipartRequest("POST", uri);
    var multipartFile = new http.MultipartFile('file', stream, length,
        filename: p.basename("resized_image.jpg"));


    request.files.add(multipartFile);
    var response = await request.send();
    print(response.statusCode);
    response.stream.transform(utf8.decoder).listen((value) {
      print(value);
    });
  }

When I run this code, and the app just freezes and the images didn't upload to the server.

5
  • no errors in your debug console? Commented Oct 2, 2018 at 7:46
  • [VERBOSE-2:shell.cc(181)] Dart Error: Unhandled exception: FileSystemException: Cannot open file, path = 'thumbnail.jpg' (OS Error: Operation not permitted, errno = 1) #0 _File.throwIfError (dart:io/file_impl.dart:647:7) #1 _File.openSync (dart:io/file_impl.dart:491:5) #2 _File.writeAsBytesSync (dart:io/file_impl.dart:616:31) Commented Oct 2, 2018 at 7:52
  • So the problem is that it cannot open the file called 'thumbnail.jpg'. Is this an asset or an image that was downloaded? Where is it looking for this image? Commented Oct 2, 2018 at 9:02
  • @user7380419 you can't access files in the filesystem like that directly. You have to use the path_provider plugin to write the file in the app's temporary or application directory. In practice though, Richard's answer below is a much better option. Commented Oct 2, 2018 at 15:02
  • how can I convert resized_img to Unit8List? Commented May 29, 2020 at 9:07

2 Answers 2

12

There's no need to write it to a file; you can send the resized image directly from memory.

uploadImages(File image_File) async {
  img.Image image_temp = img.decodeImage(image_File.readAsBytesSync());
  img.Image resized_img = img.copyResize(image_temp, 800);

  var request = new http.MultipartRequest(
    'POST',
    Uri.parse('https://myserver.com/upload.php'),
  );
  var multipartFile = new http.MultipartFile.fromBytes(
    'file',
    img.encodeJpg(resized_img),
    filename: 'resized_image.jpg',
    contentType: MediaType.parse('image/jpeg'),
  );

  request.files.add(multipartFile);
  var response = await request.send();
  print(response.statusCode);
  response.stream.transform(utf8.decoder).listen((value) {
    print(value);
  });
}

Note: when you import packages, you should use a lower case label (i.e. img). You'll need to import package:http_parser to get MediaType too.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks Richard! the function is working good, but it takes 2mins to resize one image, is it normal?
Yes. The image package runs in pure dart and isn't heavily optimized. Wherever possible, capture the image in lower resolution. See, for example Image size taken from Flutter Image_Picker plugin is way too big
3

While i am using

package:image/image.dart

facing below issues

  • showing blank page while converting image ( may be taking so much process while compressing image)
  • After compression image was stretched and not looking good

Then used below plugin, same working fine without any issue, even faster and what i expected

https://github.com/btastic/flutter_native_image.git

steps and method available in above link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.