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

feat: add image focus resolver implementation #669

Merged
merged 9 commits into from
Sep 20, 2024
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
15 changes: 0 additions & 15 deletions source/php/Integrations/Component/Image.php

This file was deleted.

31 changes: 31 additions & 0 deletions source/php/Integrations/Component/ImageFocusResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Modularity\Integrations\Component;

use \ComponentLibrary\Integrations\Image\ImageFocusResolverInterface;

class ImageFocusResolver implements ImageFocusResolverInterface {

/**
* Constructor
*
* @param array $data The data array to resolve from
*/
public function __construct(private $data){}

/**
* Get focus point
*
* @return array
*/
public function getFocusPoint(): array {
$data = $this->data;
if($data && isset($data['left'], $data['top'])) {
return [
'left' => $data['left'] ?? 50,
'top' => $data['top'] ?? 50
];
}
return ['left' => 50, 'top' => 50];
}
}
37 changes: 37 additions & 0 deletions source/php/Integrations/Component/ImageResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Modularity\Integrations\Component;

use \ComponentLibrary\Integrations\Image\ImageResolverInterface;

class ImageResolver implements ImageResolverInterface {

/**
* Get image url
*
* @param int $id
* @param array $size
* @return string|null
*/
public function getImageUrl(int $id, array $size): ?string {
$image = wp_get_attachment_image_src($id, $size);
if($image !== false && isset($image[0]) && filter_var($image[0], FILTER_VALIDATE_URL)) {
return $image[0];
}
return null;
}

/**
* Get image alt
*
* @param int $id
* @return string|null
*/
public function getImageAltText(int $id): ?string {
$alt = get_post_meta($id, '_wp_attachment_image_alt', true);
if($alt) {
return $alt;
}
return null;
}
}
18 changes: 10 additions & 8 deletions source/php/Module/Hero/Hero.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Modularity\Module\Hero;

use Modularity\Integrations\Component\ImageResolver;
use Modularity\Integrations\Component\ImageFocusResolver;
use ComponentLibrary\Integrations\Image\Image as ImageComponentContract;

class Hero extends \Modularity\Module
{
public $slug = 'hero';
Expand All @@ -28,14 +32,12 @@ public function data() : array
//Grab image
if ('image' == $type) {
$data = [
'image' => wp_get_attachment_image_src(
$fields['mod_hero_background_image']['id'],
[1728, false] //90% of 1920 (max screen width)
)[0] ?? false,
'imageFocus' => [
'top' => $fields['mod_hero_background_image']['top'] ?? '50',
'left' => $fields['mod_hero_background_image']['left'] ?? '50'
]
'image' => ImageComponentContract::factory(
(int) $fields['mod_hero_background_image']['id'],
[1920, false],
new ImageResolver(),
new ImageFocusResolver($fields['mod_hero_background_image'])
)
];
}

Expand Down
1 change: 0 additions & 1 deletion source/php/Module/Hero/views/image.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@hero([
"classList" => $stretch ? [$class] : [],
"image" => $image,
"imageFocus" => $imageFocus,
"size" => $size,
"title" => !$hideTitle && !empty($postTitle) ? $postTitle : false,
"byline" => $byline,
Expand Down
6 changes: 4 additions & 2 deletions source/php/Module/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Modularity\Module\Image;

use Municipio\Helper\Image as ImageHelper;
use Modularity\Integrations\Component\Image as ImageResolverInterface;
use Modularity\Integrations\Component\ImageResolver;
use Modularity\Integrations\Component\ImageFocusResolver;
use ComponentLibrary\Integrations\Image\Image as ImageComponentContract;

class Image extends \Modularity\Module
Expand Down Expand Up @@ -59,7 +60,8 @@ public function data() : array
$resolvedImage = ImageComponentContract::factory(
$imageId,
$imageSize,
new ImageResolverInterface()
new ImageResolver(),
new ImageFocusResolver('test')
);

$data['image']['src'] = $resolvedImage;
Expand Down
Loading