Source of file FocusPointImageExtension.php
Size: 1,660 Bytes - Last Modified: 2021-12-24T05:20:16+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/FocusPointImageExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 | <?php namespace JonoM\FocusPoint\Extensions; use JonoM\FocusPoint\Dev\FocusPointMigrationTask; use JonoM\FocusPoint\FieldType\DBFocusPoint; use SilverStripe\Assets\Image; use SilverStripe\Assets\Storage\DBFile; /** * FocusPoint Image extension. * Extends Image to allow automatic cropping from a selected focus point. * * @extends DataExtension * @property DBFocusPoint $FocusPoint * @property Image|DBFile|FocusPointImageExtension $owner */ class FocusPointImageExtension extends FocusPointExtension { /** * Describes the focus point coordinates on the image. */ private static $db = [ 'FocusPoint' => DBFocusPoint::class, ]; public function requireDefaultRecords() { $autoMigrate = FocusPointMigrationTask::create(); $autoMigrate->up(); } public function onBeforeWrite() { // Ensure that we saved the cached image width / height whenever we change the file hash // This code ignores forceChange, since we only need to know if the underlying file is modified $changes = $this->owner->getChangedFields(['FileHash']); $fileHashChanged = isset($changes['FileHash']) && $changes['FileHash']['before'] !== $changes['FileHash']['after']; if ( ( $fileHashChanged || empty($this->owner->FocusPoint->getField('Width')) || empty($this->owner->FocusPoint->getField('Height')) ) && $this->owner->exists() ) { $this->owner->FocusPoint->Width = $this->owner->getWidth(); $this->owner->FocusPoint->Height = $this->owner->getHeight(); } } } |