image.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'dart:math';
  2. class Image {
  3. String? id;
  4. String? userId;
  5. String? subjectId;
  6. String? src;
  7. String? srcType;
  8. String? thumbnail;
  9. String? seq;
  10. String? createdAt;
  11. int w = 0, h = 0;
  12. Image({this.id, this.userId, this.subjectId, this.src, this.srcType, this.thumbnail, this.seq, this.createdAt});
  13. Image.fromJson(Map<String, dynamic> json) {
  14. id = json['id'];
  15. userId = json['user_id'];
  16. subjectId = json['subject_id'];
  17. src = json['src'];
  18. srcType = json['src_type'];
  19. thumbnail = json['thumbnail'];
  20. seq = json['seq'];
  21. createdAt = json['created_at'];
  22. }
  23. Map<String, dynamic> toJson() {
  24. final Map<String, dynamic> data = new Map<String, dynamic>();
  25. data['id'] = this.id;
  26. data['user_id'] = this.userId;
  27. data['subject_id'] = this.subjectId;
  28. data['src'] = this.src;
  29. data['src_type'] = this.srcType;
  30. data['thumbnail'] = this.thumbnail;
  31. data['seq'] = this.seq;
  32. data['created_at'] = this.createdAt;
  33. return data;
  34. }
  35. static RegExp reg = new RegExp(r"_size([0-9]+)x([0-9]+)");
  36. double getImageAspectRatio() {
  37. if (this.w == 0 || this.h == 0) {
  38. String? url = this.src;
  39. if (url?.isNotEmpty == true) {
  40. var match = reg.firstMatch(url!);
  41. if (match != null) {
  42. this.w = int.parse(match.group(1)!);
  43. this.h = int.parse(match.group(2)!);
  44. }
  45. }
  46. }
  47. if (this.w != 0 && this.h != 0) {
  48. // print("$url w:${this.w},h:${this.h},a:${this.w / this.h}");
  49. return this.w / this.h;
  50. }
  51. return 1.5;
  52. }
  53. double getWidth(double width) {
  54. double ratio = getImageAspectRatio();
  55. if (ratio > 16 / 10) return width;
  56. double height = width / max(16 / 10, ratio);
  57. double r = height / h * w;
  58. // double? r1 = min(w / width * h, width);
  59. // double? fix = max(r, width / 3);
  60. // print("$width $height ${getImageAspectRatio()} $r -- $w $h $fix");
  61. return r;
  62. }
  63. bool isLongImage() {
  64. double s = getImageAspectRatio();
  65. return s < 0.2 || s > 3;
  66. }
  67. }