LCOV - code coverage report
Current view: top level - lib/src - image_transform.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 111 118 94.1 %
Date: 2020-05-04 13:08:30 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:async';
       2             : import 'dart:convert';
       3             : import 'package:contentstack/client.dart';
       4             : import 'package:contentstack/contentstack.dart';
       5             : import 'package:contentstack/src/image/filter.dart';
       6             : import 'package:contentstack/src/image/fit.dart';
       7             : import 'package:contentstack/src/image/format.dart';
       8             : import 'package:contentstack/src/image/orientation.dart';
       9             : import 'package:contentstack/src/query_params.dart';
      10             : import 'package:logging/logging.dart';
      11             : 
      12             : class ImageTransformation {
      13             :   final String _imageUrl;
      14             :   final HttpClient client;
      15             :   final Logger log = Logger('ImageTransformation');
      16             :   final Map<String, String> queryParameter = <String, String>{};
      17             :   final URLQueryParams query = URLQueryParams();
      18           1 :   ImageTransformation(this._imageUrl, this.client);
      19             : 
      20             :   /// The auto function lets you enable the functionality that automates certain image optimization features.
      21             :   /// As of now, there is only one possible value for this field, i.e., webp. When the auto parameter is set to webp,
      22             :   /// it enables WebP image support. WebP images have higher compression rate with minimum loss of quality.
      23             : 
      24             :   /// For more details, Read documentation:
      25             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#automate-optimization
      26             :   ///
      27             :   ///Example:
      28             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
      29             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
      30             :   ///  await imageTransformation.auto(auto: 'webp', format: 'pjpg').fetch();
      31           1 :   void auto({String auto, String format}) {
      32             :     if (auto != null) {
      33           2 :       query.append('auto', auto);
      34             :     }
      35             :     if (format != null) {
      36           2 :       query.append('formate', format);
      37             :     }
      38             :   }
      39             : 
      40             :   /// The quality function lets you control the compression level of images that have Lossy file format.
      41             :   /// The value for this parameters can be entered in any whole number (taken as a percentage)
      42             :   /// between 1 and 100. The lower the number, the smaller will be file size and lower quality, and vice versa.
      43             :   /// If the source image file is not of Lossy file format, this parameter will be ignored.
      44             :   ///
      45             :   /// For more details, Read documentation:
      46             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#quality
      47             :   ///
      48             :   /// Example:
      49             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
      50             :   /// final imageTransformation = stack.imageTransform(imageUrl);
      51             :   /// final response = await imageTransformation.quality(2).fetch();
      52             :   ///
      53           1 :   void quality(int quality) {
      54           3 :     query.append('quality', quality.toString());
      55             :   }
      56             : 
      57             :   /// The format function lets you converts a given image from one format to another.
      58             :   /// The formats that the source image can be converted to are gif, png, jpg (for JPEG),
      59             :   /// pjpg (for Progressive JPEG), webp, webpll (Lossless), and webply (Lossy).
      60             :   ///
      61             :   /// for more details read documentation:
      62             :   /// For more details, Read documentation:
      63             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#convert-formats
      64             :   ///
      65             :   ///  Example:
      66             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
      67             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
      68             :   ///  final response = await imageTransformation.convert(Format.pjpg).fetch();
      69           1 :   void convert(Format format) {
      70           2 :     format.when(gif: (formatResult) {
      71           2 :       query.append('format', 'gif');
      72           1 :     }, png: (formatResult) {
      73           2 :       query.append('format', 'png');
      74           1 :     }, jpg: (formatResult) {
      75           2 :       query.append('format', 'jpg');
      76           1 :     }, pjpg: (formatResult) {
      77           2 :       query.append('format', 'pjpg');
      78           1 :     }, webp: (formatResult) {
      79           2 :       query.append('format', 'webp');
      80           1 :     }, webplossy: (formatResult) {
      81           2 :       query.append('format', 'webply');
      82           1 :     }, webplossless: (formatResult) {
      83           2 :       query.append('format', 'webpll');
      84             :     });
      85             :   }
      86             : 
      87             :   /// this function lets you dynamically resize the width of the output image by specifying pixels or percentage values
      88             :   /// for more details read documentation:
      89             :   /// The disable function disables the functionality that is enabled by default.
      90             :   /// For instance, upscale is always enabled, in which the image is upscaled
      91             :   /// if the output image (by specifying the width or height) is bigger than the source image.
      92             :   /// To disable this feature, you can use the query ?disable=upscale.
      93             :   /// This ensures that even if the specified height or width is much bigger than the actual image,
      94             :   /// it will not be rendered disproportionately.
      95             :   ///
      96             :   /// For more details, Read documentation:
      97             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#resize-images
      98             :   ///
      99             :   /// Example:
     100             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     101             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     102             :   /// final response = await imageTransformation.resize(width: 100, disable: true ).fetch();
     103           1 :   void resize({int width, int height, bool disable}) {
     104             :     if (width != null) {
     105             :       //queryParameter['width'] = width.toString();
     106           3 :       query.append('width', width.toString());
     107             :     }
     108             :     if (height != null) {
     109           3 :       query.append('height', height.toString());
     110             :     }
     111             :     if (disable != null && disable) {
     112           2 :       query.append('disable', 'upscale');
     113             :     }
     114             :   }
     115             : 
     116           1 :   void crop(String cropValue) {
     117             :     // checks if cropRatio is not null then takes height, width and cropRatio as prams
     118             :     // else it takes crop params and comas separated width & height
     119           2 :     query.append('crop', cropValue);
     120             :   }
     121             : 
     122             :   /// The crop function allows you to remove pixels from an image.
     123             :   /// You can crop an image by specifying the height and width in
     124             :   /// pixels or percentage value, or defining height and width in aspect ratio.
     125             :   /// You can also define a sub region (i.e., define the starting point for crop)
     126             :   /// before cropping the image, or you can offset the image on its
     127             :   /// X and Y axis (i.e., define the centre point of the crop) before cropping the image.
     128             : 
     129             :   /// You can set the X-axis and Y-axis position of the top left corner of the crop by
     130             :   /// using the query ?crop={width_value},{height_value},x{value},y{value}.
     131             :   /// This lets you define the starting point of the crop region.
     132             :   /// The x-axis value and y-axis value can be defined in pixels or percentage.
     133             :   ///
     134             :   /// An example of this would be ?crop=300,400,x150,y75 or ?crop=300,400,x0.50,y0.60.
     135             :   /// Compulsory parameters [width] and [height]
     136             :   /// Optional parameters: [region]
     137             :   /// Optional parameters: [offset]
     138             :   /// For more details, Read documentation:
     139             :   /// For more details read the doc: https://www.contentstack.com/docs/developers/apis/image-delivery-api/#crop-images
     140             :   ///
     141             :   /// Example: With Aspect Ratio:
     142             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     143             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     144             :   /// final response = await imageTransformation.cropBy(150, 100, cropRatio: '1:3').fetch();
     145             :   /// log.fine(response);
     146             :   ///
     147             :   /// Example: Without aspect Ratio:
     148             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     149             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     150             :   /// final response = await imageTransformation.cropBy(150, 100).fetch();
     151             :   /// log.fine(response);
     152           1 :   void cropBy(int width, int height, {String region, String offset}) {
     153             :     // checks if cropRatio is not null then takes height, width and cropRatio as prams
     154             :     // else it takes crop params and comas separated width & height
     155           1 :     final cropLRBL = [];
     156             :     if (width != null) {
     157           1 :       cropLRBL.add(width);
     158             :     }
     159             :     if (height != null) {
     160           1 :       cropLRBL.add(height);
     161             :     }
     162             :     if (region != null) {
     163           0 :       cropLRBL.add(region);
     164             :     }
     165             :     if (offset != null) {
     166           0 :       cropLRBL.add(offset);
     167             :     }
     168           1 :     final commaSeparated = cropLRBL.join(', ');
     169           2 :     query.append('crop', commaSeparated);
     170             :   }
     171             : 
     172             :   ///This parameter enables you to fit the given image properly within the specified height and width.
     173             :   ///You need to provide values for the height, width and fit parameters.
     174             :   ///The two available values for the fit parameter are bounds and crop.
     175             :   ///fit accepts optional parameters [width], [height]  of type [int] and fir of type [Fit]
     176             :   ///
     177             :   /// For more details, Read documentation:
     178             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#fit-mode
     179             :   ///
     180             :   ///  Example:
     181             :   ///   final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     182             :   ///   final imageTransformation = stack.imageTransform(imageUrl);
     183             :   ///   final response = await imageTransformation.fitBy(  250,  250, Fit.crop).fetch();
     184             :   ///
     185             : 
     186           1 :   void fit(double width, double height, Fit fit) {
     187             :     if (width != null) {
     188           3 :       query.append('width', width.toString());
     189             :     }
     190             :     if (height != null) {
     191           3 :       query.append('height', height.toString());
     192             :     }
     193             :     if (fit != null) {
     194             :       //enum Fit { bounds, crop }
     195           2 :       fit.when(bounds: (value) {
     196           2 :         query.append('fit', 'bounds');
     197           1 :       }, crop: (value) {
     198           2 :         query.append('fit', 'crop');
     199             :       });
     200             :     }
     201             :   }
     202             : 
     203             :   ///The trim parameter lets you trim an image from the edges.
     204             :   ///This is especially useful for removing border or white spaces
     205             :   ///that the downloaded images usually come with.
     206             :   ///The value for this parameter can be given in pixels or percentage.
     207             :   ///You can specify values for [top], [right], [bottom], and [left] edges of an image.
     208             :   ///For example, to trim the top edge by 25px, right edge by 50px, bottom edge by 75px and left edge by 100
     209             :   /// provide [trim] as comma separated value like, 25,50,75,100
     210             :   ///
     211             :   /// For more details, Read documentation:
     212             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#trim-images
     213             :   ///
     214             :   /// Example:
     215             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     216             :   ///   final imageTransformation = stack.imageTransform(imageUrl);
     217             :   ///   final response = await imageTransformation.trim(25).fetch();
     218           1 :   void trim([int top, int right, int bottom, int left]) {
     219           1 :     final trimLRBL = [];
     220             :     if (top != null) {
     221           1 :       trimLRBL.add(top);
     222             :     }
     223             :     if (right != null) {
     224           1 :       trimLRBL.add(top);
     225             :     }
     226             :     if (bottom != null) {
     227           1 :       trimLRBL.add(bottom);
     228             :     }
     229             :     if (left != null) {
     230           1 :       trimLRBL.add(left);
     231             :     }
     232           1 :     final joinedValue = trimLRBL.join(', ');
     233           2 :     query.append('trim', joinedValue);
     234             :   }
     235             : 
     236             :   ///The orient parameter lets you control the cardinal orientation of the given image.
     237             :   ///Using this parameter, you can orient the image right or left, flip horizontally
     238             :   ///or vertically or both, and do a lot more. It can automatically correct the
     239             :   ///orientation of the image if the source image contains orientation details
     240             :   ///within its EXIF data (Exchangeable Image File Format).
     241             :   ///
     242             :   ///[orient] parameter should be enum type of  [Orientation]
     243             :   ///
     244             :   /// For more details, Read documentation:
     245             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#reorient-images
     246             :   ///
     247             :   ///  Example:
     248             :   ///   final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     249             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
     250             :   ///  final response = await imageTransformation.orientation(Orientation.vertically).fetch();
     251           1 :   void orientation(Orientation orient) {
     252             :     //  toDefault = '1';
     253             :     //  horizontally = '2';
     254             :     //  horizontallyAndVertically = '3';
     255             :     //  vertically = '4';
     256             :     //  horizontallyAndRotate90DegreeLeft = '5';
     257             :     //  degrees90TowardsRight = '6';
     258             :     //  horizontallyAndRotate90DegreesRight = '7';
     259             :     //  rotate90DegreesLeft = '8';
     260             :     if (orient != null) {
     261           2 :       orient.when(toDefault: (orientation) {
     262           2 :         query.append('orient', 1);
     263           1 :       }, horizontally: (orientation) {
     264           2 :         query.append('orient', 2);
     265           1 :       }, horizontallyAndVertically: (orientation) {
     266           2 :         query.append('orient', 3);
     267           1 :       }, vertically: (orientation) {
     268           2 :         query.append('orient', 4);
     269           1 :       }, horizontallyAndRotate90DegreeLeft: (orientation) {
     270           2 :         query.append('orient', 5);
     271           1 :       }, degrees90TowardsRight: (orientation) {
     272           2 :         query.append('orient', 6);
     273           1 :       }, horizontallyAndRotate90DegreesRight: (orientation) {
     274           2 :         query.append('orient', 7);
     275           1 :       }, rotate90DegreesLeft: (orientation) {
     276           2 :         query.append('orient', 8);
     277             :       });
     278             :     }
     279             :   }
     280             : 
     281             :   /// The overlay parameter allows you to put one image on top of another.
     282             :   /// You need to specify the relative URL of the image as value for this parameter.
     283             :   /// By default, the cropping alignment will be middle, center. See overlay-align for more details.
     284             :   /// It accepts [overlayUrl]  as a parameter to put one [overlayUrl] on top of [_imageUrl]
     285             :   /// There are optional params also like [overlayAlign], [overlayRepeat], [overlayWidth], [overlayHeight]
     286             :   /// Fr more details read the documentation:
     287             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#overlay
     288             :   ///
     289             :   /// For more details, Read documentation:
     290             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#overlay-pad
     291             :   ///
     292             :   /// Example
     293             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     294             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
     295             :   ///  final response = await imageTransformation.overlay('overlayUrl', overlayWidth: '').fetch();
     296             :   ///
     297           1 :   void overlay(String overlayUrl,
     298             :       {String overlayAlign,
     299             :       String overlayRepeat,
     300             :       int overlayWidth,
     301             :       int overlayHeight}) {
     302           2 :     query.append('overlay', overlayUrl);
     303             : 
     304             :     if (overlayAlign != null) {
     305           2 :       query.append('overlay-align', overlayAlign);
     306             :     }
     307             :     if (overlayRepeat != null) {
     308           2 :       query.append('overlay-repeat', overlayRepeat);
     309             :     }
     310             :     if (overlayWidth != null) {
     311           2 :       query.append('overlay-width', overlayWidth);
     312             :     }
     313             :     if (overlayHeight != null) {
     314           2 :       query.append('overlay-height', overlayHeight);
     315             :     }
     316             :   }
     317             : 
     318             :   /// This function lets you add extra pixels to the edges of an image.
     319             :   /// This is useful if you want to add whitespace or border to an image.
     320             :   /// The value for this parameter can be given in pixels or percentage.
     321             :   ///
     322             :   /// You can specify values for top, right, bottom, and left padding for an image.
     323             :   /// For example, to add padding to the top edge by 25px, right edge by 50px, bottom edge by 75px and left edge by 100,
     324             :   ///
     325             :   /// For more details, Read documentation:
     326             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#pad
     327             :   ///
     328             :   ///Example:
     329             :   ///final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     330             :   ///final imageTransformation = stack.imageTransform(imageUrl);
     331             :   ///final response = await imageTransformation.padding("25,50,75,100").fetch();
     332             :   ///
     333           1 :   void padding(String padding) {
     334           2 :     query.append('pad', padding);
     335             :   }
     336             : 
     337             :   ///You can either specify all the four padding values (top, right, bottom, and left)
     338             :   ///or combine two or more values
     339             :   //top, right, bottom, left
     340             :   ///Example:
     341             :   ///final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     342             :   ///final imageTransformation = stack.imageTransform(imageUrl);
     343             :   ///final response = await imageTransformation.addPadding("25,50,75,100").fetch();
     344           1 :   void overlayPadding(String overlayPadding) {
     345           2 :     query.append('overlay-pad', overlayPadding);
     346             :   }
     347             : 
     348             :   ///The bg-color function lets you set a backgroud color for the given image.
     349             :   ///This is useful when applying padding or for replacing the transparent pixels of an image.
     350             :   ///There are three possible types of values for this [bgColor] is string .
     351             :   ///It can accept hexadecimal, combination of (Red, Blue, Green) and (Red, Blue, Green, Alpha).
     352             :   ///
     353             :   /// For more details, Read documentation:
     354             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#background-color
     355             :   ///  Example:
     356             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     357             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
     358             :   ///  final response = await imageTransformation.bgColor('cccccc').fetch();
     359           1 :   void bgColor(String bgColor) {
     360           2 :     query.append('bg-color', bgColor);
     361             :   }
     362             : 
     363             :   ///To implement the device pixel ratio functionality of the Image Delivery API, you require two parameters "dpr" and "height or width".
     364             :   ///For more details read the documentation:
     365             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#set-device-pixel-ratio
     366             :   ///
     367             :   /// For more details, Read documentation:
     368             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#dpr
     369             :   ///
     370             :   /// Example
     371             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     372             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
     373             :   ///  final response = await imageTransformation.dpr(30, 60, 12).fetch();
     374             :   ///
     375           1 :   void dpr(int dpr) {
     376           3 :     query.append('dpr', dpr.toString());
     377             :   }
     378             : 
     379             :   ///
     380             :   /// The blur parameter allows you to decrease the focus and clarity of a given image.
     381             :   /// To specify the extent to which you need to increase the blurriness of an image,
     382             :   /// use any decimal number (float) between 1 and 1000.
     383             :   ///
     384             :   /// For more details, Read documentation:
     385             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#blur
     386             :   ///
     387             :   /// Example:
     388             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     389             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
     390             :   ///  final response = await imageTransformation.blur(3).fetch();
     391             : 
     392           1 :   void blur(int blur) {
     393           3 :     query.append('blur', blur.toString());
     394             :   }
     395             : 
     396             :   ///The frame parameter fetches the first frame from an animated GIF
     397             :   ///(Graphics Interchange Format) file that comprises a sequence of moving images.
     398             :   /// For more details, Read documentation:
     399             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#frame
     400             :   ///
     401             :   /// Example:
     402             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     403             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
     404             :   ///  final response = await imageTransformation.frame(30).fetch();
     405             :   ///
     406           1 :   void frame(int frame) {
     407           3 :     query.append('frame', frame.toString());
     408             :   }
     409             : 
     410             :   ///The frame parameter fetches the first frame from an animated GIF
     411             :   ///(Graphics Interchange Format) file that comprises a sequence of moving images.
     412             :   ///Increase the sharpness of a given image by amount, radius and threshold
     413             :   /// For more details, Read documentation:
     414             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#sharpen
     415             :   ///
     416             :   ///  Example:
     417             :   ///  final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     418             :   ///  final imageTransformation = stack.imageTransform(imageUrl);
     419             :   ///  final response = await imageTransformation.sharpen(5, 1000, 2').fetch();
     420             :   ///
     421           1 :   void sharpen(int amount, int radius, int threshold) {
     422           3 :     query.append('sharpen', 'a$amount,r$radius,t$threshold');
     423             :   }
     424             : 
     425             :   ///The saturation parameter allows you to increase or decrease the intensity
     426             :   /// of the colors in a given image. To specify the saturation
     427             :   /// for an image, use a whole number (integer) between -100 and 100.
     428             :   /// You can also define saturation using any decimal number between -100.00 and 100.00
     429             :   /// For more details, Read documentation:
     430             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#saturation
     431             :   ///    Example:
     432             :   ///    final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     433             :   ///    final imageTransformation = stack.imageTransform(imageUrl);
     434             :   ///    final response = await imageTransformation.saturation(20);
     435             : 
     436           1 :   void saturation(int saturation) {
     437           3 :     query.append('saturation', saturation.toString());
     438             :   }
     439             : 
     440             :   /// The contrast parameter allows you to increase or decrease
     441             :   /// the difference between the darkest and lightest tones in a given image.
     442             :   /// To specify contrast for an image, use a whole number (integer)
     443             :   /// between -100 and 100. You can also define contrast using any decimal number between -100.00 and 100.00.
     444             :   ///
     445             :   /// To increase the value of the contrast parameter of an image, pass a positive value or negative value
     446             :   /// For more details, Read documentation:
     447             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#contrast
     448             :   ///
     449             :   /// Example
     450             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     451             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     452             :   /// final response = await imageTransformation.contrast(20);
     453             : 
     454           1 :   void contrast(int contrast) {
     455           3 :     query.append('contrast', contrast.toString());
     456             :   }
     457             : 
     458             :   /// The brightness parameter allows you to increase or decrease the intensity
     459             :   /// with which an image reflects or radiates perceived light.
     460             :   /// To specify brightness for an image, use a whole number (integer) between -100 and 100.
     461             :   /// You can also define brightness using any decimal number between -100.00 and 100.00
     462             :   ///
     463             :   /// To increase the value of the brightness parameter of an image, pass a positive value or negative value
     464             :   /// For more details, Read documentation:
     465             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#brightness
     466             :   ///
     467             :   /// Example:
     468             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     469             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     470             :   /// final response =  imageTransformation.brightness(20);
     471           1 :   void brightness(int brightness) {
     472           3 :     query.append('brightness', brightness.toString());
     473             :   }
     474             : 
     475             :   ///
     476             :   /// The resize-filter parameter allows you to use the resizing
     477             :   /// filter to increase or decrease the number of pixels in a given image.
     478             :   /// This parameter resizes the given image without adding or removing any data from it.
     479             :   ///
     480             :   /// The following values are acceptable for the resize-filter parameter
     481             :   /// For more details, Read documentation:
     482             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#resize-filter
     483             : 
     484             :   /// Example:
     485             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     486             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     487             :   /// final response =  imageTransformation.resizeFilter(width: 20, height: 40, filter: Filter.bilinear);
     488             :   ///
     489           1 :   void resizeFilter({int width, int height, Filter filter}) {
     490             :     if (width != null) {
     491           3 :       query.append('width', width.toString());
     492             :     }
     493             :     if (height != null) {
     494           3 :       query.append('height', height.toString());
     495             :     }
     496             :     if (filter != null) {
     497           2 :       filter.when(nearest: (filterType) {
     498           2 :         query.append('resize-filter', 'nearest');
     499           1 :       }, bilinear: (filterType) {
     500           2 :         query.append('resize-filter', 'bilinear');
     501           1 :       }, bicubic: (filterType) {
     502           2 :         query.append('resize-filter', 'bicubic');
     503           1 :       }, lanczos: (filterType) {
     504           2 :         query.append('resize-filter', 'lanczos3');
     505             :       });
     506             :     }
     507             :   }
     508             : 
     509             :   ///
     510             :   /// The canvas parameter allows you to increase the size of the canvas that surrounds an image.
     511             :   /// You can specify the height and width of the canvas area in pixels or percentage or define the
     512             :   /// height and width of the aspect ratio of the canvas. You can also define the starting point for
     513             :   /// the canvas area or offset the canvas on its X and Y axis.
     514             :   ///
     515             :   /// [canvasValue] could be in the type of  string, It could be in the format of
     516             :   /// dimension: [700,800],
     517             :   /// ratio: 2:3 , sub-region: [700,800,x0.50,y0.60],  or offset :[ 700,800,offset-x0.65,offset-y0.80]
     518             :   /// For more details, Read documentation:
     519             :   /// https://www.contentstack.com/docs/developers/apis/image-delivery-api/#canvas
     520             :   ///
     521             :   /// Example:  Canvas by width & Height:
     522             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     523             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     524             :   /// final response =  imageTransformation.canvas('700,800');
     525             :   ///
     526             :   /// Example:  Canvas by ratio:
     527             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     528             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     529             :   /// final response =  imageTransformation.canvas('2:3');
     530             :   ///
     531             :   /// Example:  Canvas  Sub-region:
     532             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     533             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     534             :   /// final response =  imageTransformation.canvas('700,800,x0.50,y0.60');
     535             :   ///
     536             :   /// Example:  Canvas and offset:
     537             :   /// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
     538             :   /// final imageTransformation = stack.imageTransform(imageUrl);
     539             :   /// final response =  imageTransformation.canvas('700,800,offset-x0.65,offset-y0.80');
     540             :   ///
     541           1 :   void canvas(String canvasValue) {
     542           2 :     query.append('canvas', canvasValue);
     543             :   }
     544             : 
     545             :   ///Makes API Request of respective function.
     546           1 :   Future<T> fetch<T, K>() async {
     547           3 :     final bool _validURL = Uri.parse(_imageUrl).isAbsolute;
     548             :     if (!_validURL) {
     549           0 :       throw Exception('Invalid url requested');
     550             :     }
     551           4 :     final response = await client.get(getUrl());
     552           2 :     if (response.statusCode == 200) {
     553           2 :       final Map bodyJson = jsonDecode(response.body);
     554           0 :       if (T == AssetModel && bodyJson.containsKey('asset')) {
     555           0 :         return AssetModel.fromJson(bodyJson['asset']) as T;
     556             :       }else{
     557           0 :         return json.decode(response.body);
     558             :       }
     559             :     } else {
     560           0 :       throw Exception('Failed to load post');
     561             :     }
     562             :   }
     563             : 
     564           1 :   String getUrl() {
     565           3 :     return query.toUrl(_imageUrl);
     566             :   }
     567             : }

Generated by: LCOV version 1.14