image_compress.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. const String COMPRESS_LEVEL_1 = "10";
  2. const String COMPRESS_LEVEL_2 = "20";
  3. const String COMPRESS_LEVEL_3 = "30";
  4. const String COMPRESS_LEVEL_4 = "40";
  5. const String COMPRESS_LEVEL_5 = "50";
  6. const String COMPRESS_LEVEL_6 = "60";
  7. const String COMPRESS_LEVEL_7 = "70";
  8. const String COMPRESS_LEVEL_8 = "80";
  9. const String COMPRESS_LEVEL_9 = "90";
  10. const String COMPRESS_LEVEL_10 = "100";
  11. extension ImageCompress on String {
  12. // ignore: non_constant_identifier_names
  13. String compress_value() {
  14. bool isHttpUrl = this.startsWith("http://img.kaiyanapp.com/") ||
  15. this.startsWith("https://img.kaiyanapp.com/");
  16. bool isImageRes = this.contains(".png") ||
  17. this.contains(".jpeg") ||
  18. this.contains(".jpg") ||
  19. this.contains(".gif");
  20. if (isHttpUrl && isImageRes) {
  21. Uri uri = Uri.parse(this);
  22. String compressUrl = "http://${uri.host}${uri.path}?imageMogr2/thumbnail";
  23. if (!this.contains("?imageMogr2/quality")) {
  24. compressUrl = "$compressUrl/!${COMPRESS_LEVEL_3}p";
  25. } else {
  26. compressUrl = "$compressUrl/!${COMPRESS_LEVEL_5}p";
  27. }
  28. return compressUrl;
  29. }
  30. return this;
  31. }
  32. }