-
-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1192 from PiranhaCMS/features/image-block-aspect
Added support for setting preferred image aspect on image block. Fixe…
- Loading branch information
Showing
17 changed files
with
356 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) .NET Foundation and Contributors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
* | ||
* https://github.com/piranhacms/piranha.core | ||
* | ||
*/ | ||
|
||
using System; | ||
using Piranha.Extend.Fields; | ||
using Piranha.Extend.Blocks; | ||
using Piranha.Models; | ||
|
||
namespace Piranha.AspNetCore.Services | ||
{ | ||
public class MediaHelper : IMediaHelper | ||
{ | ||
private readonly IApi _api; | ||
|
||
/// <summary> | ||
/// Default internal constructur. | ||
/// </summary> | ||
internal MediaHelper(IApi api) | ||
{ | ||
_api = api; | ||
} | ||
|
||
/// <summary> | ||
/// Resizes the given image to the given dimensions. | ||
/// </summary> | ||
/// <param name="image">The image field</param> | ||
/// <param name="width">The width</param> | ||
/// <param name="height">The optional height</param> | ||
/// <returns>The public URL of the resized image</returns> | ||
public string ResizeImage(ImageField image, int width, int? height = null) | ||
{ | ||
if (image.Id.HasValue) | ||
{ | ||
return _api.Media.EnsureVersion(image.Id.Value, width, height); | ||
} | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Resizes the given image to the given dimensions. | ||
/// </summary> | ||
/// <param name="image">The image</param> | ||
/// <param name="width">The width</param> | ||
/// <param name="height">The optional width</param> | ||
/// <returns>The public URL of the resized image</returns> | ||
public string ResizeImage(Media image, int width, int? height = null) | ||
{ | ||
if (image.Id != Guid.Empty && image.Type == MediaType.Image) | ||
{ | ||
return _api.Media.EnsureVersion(image.Id, width, height); | ||
} | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Resizes the given image block according to the | ||
/// preferred aspect. | ||
/// </summary> | ||
/// <param name="block">The image block</param> | ||
/// <param name="width">The width</param> | ||
/// <returns>The public URL of the resized image</returns> | ||
public string ResizeImage(ImageBlock block, int width) | ||
{ | ||
if (block.Body.Media != null) | ||
{ | ||
int? height = null; | ||
|
||
if (block.Aspect.Value == ImageAspect.Landscape) | ||
{ | ||
height = Convert.ToInt32(width * 10 / 16); | ||
} | ||
else if (block.Aspect.Value == ImageAspect.Portrait) | ||
{ | ||
height = Convert.ToInt32(width * 16 / 10); | ||
} | ||
else if (block.Aspect.Value == ImageAspect.Square) | ||
{ | ||
height = width; | ||
} | ||
return _api.Media.EnsureVersion(block.Body.Media.Id, width, height); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) .NET Foundation and Contributors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
* | ||
* https://github.com/piranhacms/piranha.core | ||
* | ||
*/ | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Piranha.Models; | ||
|
||
namespace Piranha.AspNetCore.Services | ||
{ | ||
public class SiteHelper : ISiteHelper | ||
{ | ||
private readonly IApi _api; | ||
|
||
/// <summary> | ||
/// Gets the id of the currently requested site. | ||
/// </summary> | ||
public Guid Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets/sets the optional culture of the requested site. | ||
/// </summary> | ||
public string Culture { get; set; } | ||
|
||
/// <summary> | ||
/// Gets/set the optional hostname of the requested site. | ||
/// </summary> | ||
public string Host { get; set; } | ||
|
||
/// <summary> | ||
/// Gets/sets the optional site prefic of the requested site | ||
/// if it's routed with `host/prefix`. | ||
/// </summary> | ||
public string SitePrefix { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the sitemap of the currently requested site. | ||
/// </summary> | ||
public Sitemap Sitemap { get; set; } | ||
|
||
/// <summary> | ||
/// Default internal constructur. | ||
/// </summary> | ||
internal SiteHelper(IApi api) | ||
{ | ||
_api = api; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the site content for the current site. | ||
/// </summary> | ||
/// <typeparam name="T">The content type</typeparam> | ||
/// <returns>The site content model</returns> | ||
public Task<T> GetContentAsync<T>() where T : SiteContent<T> | ||
{ | ||
if (Id != Guid.Empty) | ||
{ | ||
return _api.Sites.GetContentByIdAsync<T>(Id); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
core/Piranha.Manager/assets/dist/img/icons/img-landscape.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions
13
core/Piranha.Manager/assets/dist/img/icons/img-original.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions
11
core/Piranha.Manager/assets/dist/img/icons/img-portrait.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.