samarthagarwal
9/13/2018 - 12:09 PM

Wrote for Facebook post in Let's FLutter

Wrote for Facebook post in Let's FLutter

import "dart:core";

void main() {
	
  String url = "http://www.xyz.com/images/cat_53x53xyz.jpg"; 
  //You can replace xyz with any string and it will still work
  String newURL = changeSizeInUrl(url);
 
}

changeSizeInUrl(url){
  var parts = url.split('/');
  var fileName = parts[parts.length-1];
  
  var number1 = (fileName.split('_')[1]).split('x')[0];
  var number1Prefix = fileName.split('_')[0];
  var number2Container = (fileName.split('_')[1]).split('x')[1];
  var number2;
  try {
    number2 = (number2Container);
  } catch(ex) {
    number2 = "";
    for(int i=0; i<number2Container.length; i++){
      if(number2Container.codeUnitAt(i) >= 48 && number2Container.codeUnitAt(i) < 58){
        number2 = number2 + number2Container[i];
      } else {
        break;
      }
    }
  }
  
  var number2Suffix = (fileName.split(number2)[fileName.split(number2).length - 1]).split('.')[0];
  
  print(number1); // Change number1 here
  print(number2); // Change number2 here
  
  number1 = "98"; // As an example, changing 53 to 98
  number2 = "98"; // As an example, changing 53 to 98
  
  var newURL = "";
  for(int i = 0; i < parts.length - 1;  i++){
    newURL = newURL + parts[i] + "/";
  }
  
  var finalURL = newURL + number1Prefix + "_" + number1 + "x" + number2 + number2Suffix + ".jpg";
  print(finalURL);
  return finalURL;
}