getResizedImageUrl method

String getResizedImageUrl({
  1. double width = 400,
  2. double height = 400,
  3. String resize = 'clip',
  4. String crop = 'center',
})

Returns a resized imageUrl with the given width, height, resize and crop if it is from Stream CDN or Dashboard.

Read more at https://getstream.io/chat/docs/flutter-dart/file_uploads/?language=dart#image-resizing

Implementation

String getResizedImageUrl({
  // TODO: Are these sizes optimal? Consider web/desktop
  double width = 400,
  double height = 400,
  String /*clip|crop|scale|fill*/ resize = 'clip',
  String /*center|top|bottom|left|right*/ crop = 'center',
}) {
  final uri = Uri.parse(this);
  final host = uri.host;

  final fromStreamCDN = host.endsWith('stream-io-cdn.com');
  final fromStreamDashboard = host.endsWith('stream-cloud-uploads.imgix.net');

  if (!fromStreamCDN && !fromStreamDashboard) return this;

  final queryParameters = {...uri.queryParameters};

  if (fromStreamCDN) {
    if (queryParameters['h'].isNullOrMatches('*') &&
        queryParameters['w'].isNullOrMatches('*') &&
        queryParameters['crop'].isNullOrMatches('*') &&
        queryParameters['resize'].isNullOrMatches('*')) {
      queryParameters['h'] = height.floor().toString();
      queryParameters['w'] = width.floor().toString();
      queryParameters['crop'] = crop;
      queryParameters['resize'] = resize;
    }
  } else if (fromStreamDashboard) {
    queryParameters['height'] = height.floor().toString();
    queryParameters['width'] = width.floor().toString();
    queryParameters['fit'] = crop;
  }

  return uri.replace(queryParameters: queryParameters).toString();
}