Skip to content

Commit

Permalink
Merge pull request #1192 from PiranhaCMS/features/image-block-aspect
Browse files Browse the repository at this point in the history
Added support for setting preferred image aspect on image block. Fixe…
  • Loading branch information
tidyui authored May 24, 2020
2 parents 6b5587e + 273551f commit 90d60ec
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 124 deletions.
99 changes: 0 additions & 99 deletions core/Piranha.AspNetCore/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,111 +14,12 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Piranha.Extend.Fields;
using Piranha.Models;

namespace Piranha.AspNetCore.Services
{
public class ApplicationService : IApplicationService
{
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;
}
}

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"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></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"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></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>
/// Gets the current api.
/// </summary>
Expand Down
43 changes: 26 additions & 17 deletions core/Piranha.AspNetCore/Services/IMediaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,39 @@
*
*/

using System;
using Piranha.Extend.Fields;
using Piranha.Extend.Blocks;
using Piranha.Models;

namespace Piranha.AspNetCore.Services
{
public interface IMediaHelper
{
/// <summary>
/// Resizes the given image to the given dimensions.
/// </summary>
/// <param name="image"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
string ResizeImage(ImageField image, int width, int? height = null);
/// <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>
string ResizeImage(ImageField image, int width, int? height = null);

/// <summary>
/// Resizes the given image to the given dimensions.
/// </summary>
/// <param name="image"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
string ResizeImage(Media image, int width, int? height = 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>
string ResizeImage(Media image, int width, int? height = 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>
string ResizeImage(ImageBlock block, int width);
}
}
92 changes: 92 additions & 0 deletions core/Piranha.AspNetCore/Services/MediaHelper.cs
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;
}
}
}
69 changes: 69 additions & 0 deletions core/Piranha.AspNetCore/Services/SiteHelper.cs
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;
}
}
}
2 changes: 1 addition & 1 deletion core/Piranha.Manager/assets/dist/css/full.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/Piranha.Manager/assets/dist/css/slim.min.css

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions 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 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 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.
11 changes: 11 additions & 0 deletions core/Piranha.Manager/assets/dist/img/icons/img-square.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 90d60ec

Please sign in to comment.