private static string GetSizeConstraintQueryString(SizeConstraint constraint) { var sizeConstraintParameters = new NameValueCollection(); sizeConstraintParameters.AddSizeConstraint(constraint); return(sizeConstraintParameters.ToQueryString()); }
/// <summary> /// Adds size constraint's values to the collection. /// </summary> /// <param name="collection">The name value collection.</param> /// <param name="constraint">Size constraint.</param> public static void AddSizeConstraint(this NameValueCollection collection, SizeConstraint constraint) { if (constraint.WidthComponent > 0) { collection.Add("width", constraint.WidthComponent); } if (constraint.HeightComponent > 0) { collection.Add("height", constraint.HeightComponent); } if (constraint.MaxWidthOrHeightComponent > 0) { collection.Add("maxsidesize", constraint.MaxWidthOrHeightComponent); } // Prevent Kentico from using site settings if (!constraint.IsEmpty) { collection.Add("resizemode", "force"); } }
/// <summary> /// Generates a fully qualified URL to the specified image path. /// </summary> /// <param name="instance">The object that provides methods to build URLs to Kentico content.</param> /// <param name="path">The virtual path of the image.</param> /// <param name="constraint">The size constraint that is enforced on image when resizing.</param> /// <returns>The fully qualified URL to the image.</returns> /// <exception cref="ArgumentNullException"><paramref name="instance"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="path"/> is null, empty or contains query string parameters.</exception> public static string ImageUrl(this ExtensionPoint <UrlHelper> instance, string path, SizeConstraint constraint) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (string.IsNullOrEmpty(path)) { throw new ArgumentException("The image path needs to be provided."); } var absolutePath = instance.Target.Content(path); var queryString = GetSizeConstraintQueryString(constraint); return(BuildUrl(absolutePath, queryString)); }