Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide constants for the Image Quality settings #6

Merged
merged 3 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ to render something like this:
The generated URLs have a burned-in quality setting applied — for 1x images
it's `70` and for 2x it's `40`. These are values we've found works pretty great
for most of the scenarios we cover, by not hurting the image quality while
keeping the download sizes down. I'd like to have those values configurable
but haven't found a great way yet — if you have a good suggestion, by all means
[let me know.][ISSUE1]
keeping the download sizes down. They're defined as constants so they're at
least configurable if you need to change them.


## FAQ
Expand Down
27 changes: 18 additions & 9 deletions src/Vokseverk.MediaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

namespace Vokseverk {

internal static partial class Constants {
internal static partial class Media {
internal static partial class Quality {
public const int Retina = 40;
public const int Standard = 70;
}
}
}

public class PictureSource {
public string Media { get; set; }
public string Crop { get; set; }
Expand Down Expand Up @@ -50,8 +59,8 @@ public static HtmlString RenderPicture(IPublishedContent mediaItem, List<Picture

try {
foreach (var source in sources) {
var mediaURL1x = mediaItem.GetCropUrl(cropAlias: source.Crop, width: source.Width, quality: 70);
var mediaURL2x = mediaItem.GetCropUrl(cropAlias: source.Crop, width: source.Width * 2, quality: 40);
var mediaURL1x = mediaItem.GetCropUrl(cropAlias: source.Crop, width: source.Width, quality: Constants.Media.Quality.Standard);
var mediaURL2x = mediaItem.GetCropUrl(cropAlias: source.Crop, width: source.Width * 2, quality: Constants.Media.Quality.Retina);

if (source.Media == "2x") {
// Special case for rendering a single image for 1x and 2x using a `<picture<` tag
Expand Down Expand Up @@ -142,8 +151,8 @@ public static HtmlString RenderMedia(IPublishedContent image, string crop, int w
if (image != null) {
var dimensions = GetCropSize(image, crop, width);

var crop1x = image.GetCropUrl(cropAlias: crop, width: width, quality: 70);
var crop2x = image.GetCropUrl(cropAlias: crop, width: width * 2, quality: 40);
var crop1x = image.GetCropUrl(cropAlias: crop, width: width, quality: Constants.Media.Quality.Standard);
var crop2x = image.GetCropUrl(cropAlias: crop, width: width * 2, quality: Constants.Media.Quality.Retina);

imageTag = GetOutputTag(crop1x, crop2x, image.Name, dimensions);
}
Expand All @@ -166,8 +175,8 @@ public static HtmlString RenderMedia(IPublishedContent image, int width) {
if (image != null) {
var url = image.Url;
var dimensions = GetMediaSize(image, width);
var size1x = image.GetCropUrl(width: width, quality: 70);
var size2x = image.GetCropUrl(width: width * 2, quality: 40);
var size1x = image.GetCropUrl(width: width, quality: Constants.Media.Quality.Standard);
var size2x = image.GetCropUrl(width: width * 2, quality: Constants.Media.Quality.Retina);

var extension = image.Value<string>("UmbracoExtension");

Expand Down Expand Up @@ -205,8 +214,8 @@ public static HtmlString RenderMedia(IPublishedContent image, string size) {
var dimensions = size.Split('x');

if (Int32.TryParse(dimensions[0], out w) && Int32.TryParse(dimensions[1], out h)) {
var size1x = string.Format("{0}?width={1}&height={2}&quality=70", mediaUrl, w, h);
var size2x = string.Format("{0}?width={1}&height={2}&quality=40", mediaUrl, w * 2, h * 2);
var size1x = string.Format("{0}?width={1}&height={2}&quality={3}", mediaUrl, w, h, Constants.Media.Quality.Standard);
var size2x = string.Format("{0}?width={1}&height={2}&quality={3}", mediaUrl, w * 2, h * 2, Constants.Media.Quality.Retina);

imageTag = GetOutputTag(size1x, size2x, image.Name);
}
Expand Down Expand Up @@ -235,7 +244,7 @@ public static string GetMediaUrl(IPublishedContent media) {
/// <summary>
/// Single point of getting a URL for a mediaitem
/// </summary>
public static string GetCropUrl(IPublishedContent mediaItem, string crop, int width, int quality = 70) {
public static string GetCropUrl(IPublishedContent mediaItem, string crop, int width, int quality = Constants.Media.Quality.Standard) {
string outputUrl = "";

try {
Expand Down