/// <summary> /// Crops the image around a focal point. /// Will try and center on the focal point if possible, but if the crop falls outside the area of the image it will adjust. /// Keeps the aspect of the crop. /// </summary> /// <param name="image">The image to run the macro on</param> /// <param name="x">The horizontal position of the focal point</param> /// <param name="y">The y position of the focal point</param> /// <param name="width">The width of the area to crop</param> /// <param name="height">The height of the area to crop</param> /// <param name="sourceWidth">The original image width</param> /// <param name="sourceHeight">The original image height</param> /// <returns></returns> public static ImgixImage FocalCrop(this ImgixImage image, int x, int y, int width, int height, int sourceWidth, int sourceHeight) { var cropRectangle = new Rectangle(width, height); var imageRectangle = new Rectangle(sourceWidth, sourceHeight); if (!imageRectangle.CanHold(cropRectangle)) { cropRectangle = cropRectangle.EnsureFit(imageRectangle); } var cropTopLeftCorner = new Point(x - cropRectangle.Width / 2, y - cropRectangle.Height / 2); var cropBottomRightCorner = new Point(cropTopLeftCorner.X + cropRectangle.Width, cropTopLeftCorner.Y + cropRectangle.Height); //Adjust x position if (cropTopLeftCorner.X < 0) { cropTopLeftCorner = cropTopLeftCorner.SetX(0); } else if (cropBottomRightCorner.X > sourceWidth) { cropTopLeftCorner = cropTopLeftCorner.SetX(sourceWidth - cropRectangle.Width); } //Adjust y position if (cropTopLeftCorner.Y < 0) { cropTopLeftCorner = cropTopLeftCorner.SetY(0); } else if (cropBottomRightCorner.Y > sourceHeight) { cropTopLeftCorner = cropTopLeftCorner.SetY(sourceHeight - cropRectangle.Height); } return(image.Rect(cropTopLeftCorner.X, cropTopLeftCorner.Y, cropRectangle.Width, cropRectangle.Height)); }
/// <summary> /// Resizes the image around a focal point /// Crops the image to the aspect ratio with the focal point as close to center as possible. /// Will not stretch the image to the width and height since this will just be a bigger picture with the same details. /// </summary> /// <param name="image">The image to run the macro on</param> /// <param name="x">The horizontal position of the focal point</param> /// <param name="y">The y position of the focal point</param> /// <param name="width">The width of the resulting image</param> /// <param name="height">The height of the resulting image</param> /// <param name="sourceWidth">The original image width</param> /// <param name="sourceHeight">The original image height</param> /// <returns></returns> public static ImgixImage FocalResize(this ImgixImage image, int x, int y, int width, int height, int sourceWidth, int sourceHeight) { var resizeRectangle = new Rectangle(width, height); var imageRectangle = new Rectangle(sourceWidth, sourceHeight); var cropRectangle = resizeRectangle.Match(imageRectangle); var croppedImage = image.FocalCrop(x, y, cropRectangle.Width, cropRectangle.Height, sourceWidth, sourceHeight); return(!imageRectangle.CanHold(resizeRectangle) ? croppedImage : croppedImage.Width(width).Height(height)); }
/// <summary> /// Crops the image around a focal point. /// Will try and center on the focal point if possible, but if the crop falls outside the area of the image it will adjust. /// Keeps the aspect of the crop. /// </summary> /// <param name="image">The image to run the macro on</param> /// <param name="x"> /// The horizontal position of the focal point. /// Interpreted as a percentage of the image width from the left. /// Values 0.0 to 1.0 /// </param> /// <param name="y"> /// The y position of the focal point. /// Interpreted as a percentage of the image height from the top side. /// Values 0.0 to 1.0 /// </param> /// <param name="width">The width of the area to crop</param> /// <param name="height">The height of the area to crop</param> /// <param name="sourceWidth">The original image width</param> /// <param name="sourceHeight">The original image height</param> /// <exception cref="ArgumentOutOfRangeException">If either x or y is outside the 0.0 to 1.0 range</exception> /// <returns></returns> public static ImgixImage FocalCrop(this ImgixImage image, double x, double y, int width, int height, int sourceWidth, int sourceHeight) { if (x < 0.0 && x > 1.0) { throw new ArgumentOutOfRangeException(nameof(x)); } if (y < 0.0 && y > 1.0) { throw new ArgumentOutOfRangeException(nameof(y)); } var focalPoint = new Point(Convert.ToInt32(sourceWidth * x), Convert.ToInt32(sourceHeight * y)); return(image.FocalCrop(focalPoint.X, focalPoint.Y, width, height, sourceWidth, sourceHeight)); }
/// <summary> /// Sets the width of the transformed image. /// </summary> /// <param name="image">The image to transform</param> /// <param name="width">The width value as a string</param> /// <returns></returns> public static ImgixImage Width(this ImgixImage image, string width) => image.AddParameter("w", width);
/// <summary> /// Sets the output width of the transformed image. /// </summary> /// <param name="image">The image to transform</param> /// <param name="width">The pixel width</param> /// <returns></returns> public static ImgixImage Width(this ImgixImage image, int width) => image.Width(width.ToString());
/// <summary> /// Sets the output height of the transformed image as the ratio of the original size. /// </summary> /// <param name="image">The image to transform</param> /// <param name="ratio">The ratio compared to the original image. Should be between 0,0 and 1.0</param> /// <returns></returns> public static ImgixImage Height(this ImgixImage image, double ratio) => image.Height(ratio.ToString(CultureInfo.InvariantCulture));
/// <summary> /// Selects a sub-reqion of the image to use for processing /// </summary> /// <param name="image">The image to transform</param> /// <param name="x">The x position of the top left corner</param> /// <param name="y">The x position of the top left corner</param> /// <param name="width">The width of the rectangle</param> /// <param name="height">The height of the rectangle</param> /// <returns></returns> public static ImgixImage Rect(this ImgixImage image, int x, int y, int width, int height) => image.Rect($"{x},{y},{width},{height}");
/// <summary> /// How is the output image fit into the target dimensions. /// </summary> /// <param name="image">The image to transform</param> /// <param name="fit"> /// The fit mode /// Values: clamp, clip, crop, facearea, fill, max, min, scale /// </param> /// <returns></returns> public static ImgixImage Fit(this ImgixImage image, string fit) => image.AddParameter("fit", fit);
/// <summary> /// Should the returned image be lossless /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// Values: 0, 1, true, false /// </param> /// <returns></returns> public static ImgixImage Lossless(this ImgixImage image, string value) => image.AddParameter("lossless", value);
/// <summary> /// Sets the DPI value in the Exif header /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// The DPI value /// </param> /// <returns></returns> public static ImgixImage DPI(this ImgixImage image, int value) => image.AddParameter("dpi", value);
/// <summary> /// Defines the standard deviation among pixels in a border. /// Only valid when trim is set to auto /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// The standard deviation /// Default: 10 /// </param> /// <returns></returns> public static ImgixImage TrimStandardDeviation(this ImgixImage image, int value) => image.AddParameter("trimsd", value);
/// <summary> /// The difference threshhold used when trimming a border. /// Only valid when trim is set to auto /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// A lower value makes the trimming more aggressive /// Default: 11 /// </param> /// <returns></returns> public static ImgixImage TrimMeanDifference(this ImgixImage image, int value) => image.AddParameter("trimmd", value);
/// <summary> /// Adds the timecolor parameter to the image url /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// A three or six char hex color string /// </param> /// <returns></returns> public static ImgixImage TrimColor(this ImgixImage image, string value) => image.AddParameter("trimcolor", value);
/// <summary> /// Will add an auto enhancement parameter to the url /// </summary> /// <param name="image">The image to transform</param> /// <param name="values"> /// One or more AutoOperations /// </param> /// <returns></returns> public static ImgixImage Auto(this ImgixImage image, params AutoOperation[] values) => image.AddParameter("auto", string.Join(",", values.Select(at => at.ToString().ToLower()).ToArray()));
/// <summary> /// Will add an auto enhancement parameter to the url /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// The value of the auto parameter /// Values: enhance, format, redeye /// </param> /// <returns></returns> public static ImgixImage Auto(this ImgixImage image, string value) => image.AddParameter("auto", value);
/// <summary> /// DEfines the tolerance when trim has been set to color. /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// The tolerance /// Default: 0 /// </param> /// <returns></returns> public static ImgixImage TrimTolerance(this ImgixImage image, int value) => image.AddParameter("trimtol", value);
/// <summary> /// When used in an a tag forces download with the specified name /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// The download name of the image /// </param> /// <returns></returns> public static ImgixImage Download(this ImgixImage image, string value) => image.AddParameter("dl", value);
public static void HasQueryParameter(ImgixImage subject, string name, string value) { Assert.True(new Url(subject).QueryParams.Contains(new KeyValuePair<string, object>(name, value)), $"Expected image url[{subject}] to have query parameter {name}={value} but it was not found."); }
/// <summary> /// Sets the output format of the image. /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// The format name /// Values: gif, jp2, jpg, json, jxr, pjpg, mp4, png, png8, png32, webp /// </param> /// <returns></returns> public static ImgixImage OutputFormat(this ImgixImage image, string value) => image.AddParameter("fm", value);
/// <summary> /// Adds a background color to the image if there is transparency in it /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// 3, 4, 6 and 8 character color values /// </param> /// <returns></returns> public static ImgixImage BackgroundColor(this ImgixImage image, string value) => image.AddParameter("bg", value);
/// <summary> /// SEts the oputput quality of the image /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// Values: 0-100 /// Default: 75 /// </param> /// <returns></returns> public static ImgixImage Quality(this ImgixImage image, int value) => image.AddParameter("q", value);
/// <summary> /// Adds padding to the image. /// If the image does not have any size parameters the padding will extend the picture. /// If there are size parameters, the image will be shrunk to fit /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// The thickness of the padding. /// </param> /// <returns></returns> public static ImgixImage Padding(this ImgixImage image, int value) => image.AddParameter("pad", value);
/// <summary> /// Selects a sub-reqion of the image to use for processing /// </summary> /// <param name="image">The image to transform</param> /// <param name="dimensions"> /// A comma seperated string of integers of the format "x,y,width,height" /// </param> /// <returns></returns> public static ImgixImage Rect(this ImgixImage image, string dimensions) => image.AddParameter("rect", dimensions);
/// <summary> /// Sets the border around the image. /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// A String describing the border. /// Value: size,color /// Size: An integer value /// Color: An RGB or ARGB value /// </param> /// <returns></returns> public static ImgixImage Border(this ImgixImage image, string value) => image.AddParameter("border", value);
/// <summary> /// Sets the output height of the transformed image. /// </summary> /// <param name="image">The image to transform</param> /// <param name="height">The pixel height</param> /// <returns></returns> public static ImgixImage Height(this ImgixImage image, int height) => image.Height(height.ToString());
/// <summary> /// Adds ch parameter opting in to using client hints. /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// A comma seperated list of hints /// Values: Width and DPR are currently supported /// </param> /// <returns></returns> public static ImgixImage ClientHints(this ImgixImage image, string value) => image.AddParameter("ch", value);
/// <summary> /// Sets the height of the transformed image. /// </summary> /// <param name="image">The image to transform</param> /// <param name="height">The height value as a string</param> /// <returns></returns> public static ImgixImage Height(this ImgixImage image, string height) => image.AddParameter("h", height);
/// <summary> /// Chroma subsampling for JPEG and Progressive JPEG /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// Values: 420, 422, and 444 /// Default: 420 /// </param> /// <returns></returns> public static ImgixImage ChromaSubsampling(this ImgixImage image, int value) => image.AddParameter("chromasub", value);
/// <summary> /// Sets the output width of the transformed image as the ratio of the original size. /// </summary> /// <param name="image">The image to transform</param> /// <param name="ratio">The ratio compared to the original image. Should be between 0,0 and 1.0</param> /// <returns></returns> public static ImgixImage Width(this ImgixImage image, double ratio) => image.Width(ratio.ToString(CultureInfo.InvariantCulture));
/// <summary> /// Adds color quantization to the image, limiting the amount of colors in the image. /// </summary> /// <param name="image">The image to transform</param> /// <param name="value"> /// Values: 2-256 /// </param> /// <returns></returns> public static ImgixImage ColorQuantization(this ImgixImage image, int value) => image.AddParameter("colorquant", value);
/// <summary> /// Controls how the image is aligned when fit has been set to crop /// </summary> /// <param name="image">The image to transform</param> /// <param name="cropMode"> /// The crop mode value. Can be a comma seperated list. /// Values: top, bottom, left, right, faces, entropy /// </param> /// <returns></returns> public static ImgixImage Crop(this ImgixImage image, string cropMode) => image.AddParameter("crop", cropMode);
public void MainFixtureInit() { Image = Imgix.CreateImage(new ImgixOptions(new ImgixSource("sourceName", "sourceName")), "some/path/to/some/image.jpg"); }