Skip to content

Commit 25ec43d

Browse files
committed
Imager will now automatically use native focal point for Assets, if no
position is set by the transform.
1 parent 49c99f6 commit 25ec43d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/services/ImagerService.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public function transformImage($image, $transforms, $transformDefaults = null, $
324324
$transforms = $this->mergeTransforms($transforms, $transformDefaults);
325325

326326
// Normalize transform parameters
327-
$transforms = $this->normalizeTransforms($transforms);
327+
$transforms = $this->normalizeTransforms($transforms, $image);
328328

329329
// Create transformer
330330
try {
@@ -593,15 +593,16 @@ private function mergeTransforms($transforms, $defaults): array
593593
* Merges default transform object into an array of transforms
594594
*
595595
* @param array $transforms
596+
* @param Asset|string $image
596597
*
597598
* @return array
598599
*/
599-
private function normalizeTransforms($transforms): array
600+
private function normalizeTransforms($transforms, $image): array
600601
{
601602
$r = [];
602603

603604
foreach ($transforms as $t) {
604-
$r[] = $this->normalizeTransform((array)$t);
605+
$r[] = $this->normalizeTransform((array)$t, $image);
605606
}
606607

607608
return $r;
@@ -611,10 +612,11 @@ private function normalizeTransforms($transforms): array
611612
* Normalize transform object and values
612613
*
613614
* @param array $transform
615+
* @param Asset|string $image
614616
*
615617
* @return array
616618
*/
617-
private function normalizeTransform($transform): array
619+
private function normalizeTransform($transform, $image): array
618620
{
619621
// if resize mode is not crop or croponly, remove position
620622
if (isset($transform['mode'], $transform['position']) && (($transform['mode'] !== 'crop') && ($transform['mode'] !== 'croponly'))) {
@@ -644,10 +646,14 @@ private function normalizeTransform($transform): array
644646
}
645647
}
646648

649+
// if no position is passed and a focal point exists, use it
650+
if ($image instanceof Asset && !isset($transform['position']) && $image->getHasFocalPoint()) {
651+
$transform['position'] = $image->getFocalPoint();
652+
}
653+
647654
// if transform is in Craft's named version, convert to percentage
648655
if (isset($transform['position'])) {
649-
650-
if (\is_array($transform['position']) && isset($transform['position']['x']) && isset($transform['position']['y'])) {
656+
if (\is_array($transform['position']) && isset($transform['position']['x'], $transform['position']['y'])) {
651657
$transform['position'] = ($transform['position']['x'] * 100).' '.($transform['position']['y'] * 100);
652658
}
653659

0 commit comments

Comments
 (0)