newImageUrl static method

String newImageUrl(
  1. String? imageUrl,
  2. ImageDealType imageDealType, {
  3. required bool canVientianeGetBlock(
    1. String checkImageUrl
    ),
  4. required double widthLadderValue,
  5. required int widthLadderCount,
  6. double? width,
  7. double? height,
  8. void lastImageUrlGetBlock(
    1. String lastImageUrl
    )?,
})

Implementation

static String newImageUrl(
  String? imageUrl,
  ImageDealType imageDealType, {
  required bool Function(String checkImageUrl) canVientianeGetBlock,
  required double widthLadderValue, // 50.0.w_pt_cj;
  required int widthLadderCount, // 8
  double? width,
  double? height,
  void Function(String lastImageUrl)? lastImageUrlGetBlock,
}) {
  if (imageUrl == null) {
    return "";
  }
  // String fileExtensionType = imageUrl.split('.').last.toLowerCase();
  // if (['jpg', 'jpeg', 'png'].contains(fileExtensionType) == false) {
  //   return imageUrl;
  // }
  if (imageDealType == ImageDealType.origin) {
    // debugPrint('imageUrl = $imageUrl');
    if (lastImageUrlGetBlock != null) {
      lastImageUrlGetBlock(imageUrl);
    }
    return imageUrl;
  }

  bool isCloudImage = canVientianeGetBlock(imageUrl);
  if (isCloudImage != true) {
    // debugPrint('imageUrl = $imageUrl');
    if (lastImageUrlGetBlock != null) {
      lastImageUrlGetBlock(imageUrl);
    }
    return imageUrl;
  }

  String newImageUrl = imageUrl;
  // 固定取2倍图,能保证清晰,万象中已做处理,也不会太大
  // 0.652173913043478是为了对标小红书让414的屏幕宽度下最终获取到的图片宽度是540
  // double multiple = 2 * 0.652173913043478;
  double multiple = 2;
  String thumbnail = '?imageMogr2/thumbnail/';
  if (width != null && width > 0) {
    List<double> ladders = [];
    // double widthLadderValue = 50.0.w_pt_cj;
    // double widthLadderCount = 8;
    for (var i = 1; i <= widthLadderCount; i++) {
      ladders.add(widthLadderValue * i);
    }
    /*
    // List<double> ladders = [36, 64, 188, 360, 750]; // 图片规范
    List<double> ladders = [
      36.0.w_pt_cj,
      64.0.w_pt_cj,
      160.0.w_pt_cj,
      188.0.w_pt_cj, // 半屏幕
      224.0.w_pt_cj,
      256.0.w_pt_cj,
      375.0.w_pt_cj, // 满屏幕宽
    ]; // 图片规范
    */

    double useWidth = width;
    int ladderCount = ladders.length;
    for (var i = 0; i < ladderCount; i++) {
      double iLadderValue = ladders[i];
      if (width < iLadderValue) {
        useWidth = max(width, iLadderValue);
        break;
      }
    }
    // 0.888972809667674腾讯万象的问题,已提交工单
    // https://console.cloud.tencent.com/workorder/detail?ticketId=202309058831
    // 100/3 舍弃当前变量的小数部分,结果为 33。返回值为 int 类型。
    thumbnail += '${(useWidth * multiple).truncate()}';
    thumbnail += 'x';

    // if (height != null && height > 0) {
    //   double useHeight = height;
    //   double widthHeightRatio = (width / height).toDouble();
    //   useHeight = useWidth / widthHeightRatio;

    //   thumbnail += '${(useHeight * multiple).truncate()}';
    // }

    thumbnail += '/';
  }

  // quality/90图片无损压缩,对标小红书
  thumbnail += 'format/webp/auto-orient/quality/90';
  newImageUrl += thumbnail;
  // newImageUrl +='&watermark/2/text/6IW-6K6v5LqRwrfkuIfosaHkvJjlm74/fill/IzNEM0QzRA/fontsize/20/dissolve/50/gravity/northeast/dx/20/dy/20/batch/1/degree/45';

  // newImageUrl +='?watermark/1/image/aHR0cDovL2V4YW1wbGVzLTEyNTEwMDAwMDQucGljc2gubXlxY2xvdWQuY29tL3NodWl5aW4uanBn/gravity/southeast';
  // debugPrint('newImageUrl = $newImageUrl');
  if (lastImageUrlGetBlock != null) {
    lastImageUrlGetBlock(newImageUrl);
  }
  return newImageUrl;
}