Source of file PerfectCmsImageDataExtension.php
Size: 8,199 Bytes - Last Modified: 2021-12-23T10:45:59+00:00
/var/www/docs.ssmods.com/process/src/src/Model/File/PerfectCmsImageDataExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 | <?php namespace Sunnysideup\PerfectCmsImages\Model\File; use SilverStripe\Assets\Image; use SilverStripe\Control\Director; use SilverStripe\Core\Convert; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\View\ArrayData; use Sunnysideup\PerfectCmsImages\Api\ImageManipulations; use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages; /** * defines the image sizes * and default upload folder. */ class PerfectCmsImageDataExtension extends DataExtension { private static $perfect_cms_images_background_padding_color = '#ffffff'; private static $perfect_cms_images_image_definitions = []; private static $casting = [ 'PerfectCMSImageTag' => 'HTMLText', ]; /** * @param string $name PerfectCMSImages name * @param bool $inline for use within existing image tag - optional * @param string $alt alt tag for image -optional * @param string $attributes additional attributes * * @return string (HTML) */ public function getPerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') { return $this->PerfectCMSImageTag($name, $inline, $alt, $attributes); } /** * @param string $name PerfectCMSImages name * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... * @param string $alt alt tag for image -optional * @param string $attributes additional attributes * * @return string (HTML) */ public function PerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') { $arrayData = $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes); $template = 'Includes/PerfectCMSImageTag'; if (true === $inline || 1 === (int) $inline || 'true' === strtolower($inline)) { $template .= 'Inline'; } return DBField::create_field('HTMLText', $arrayData->renderWith($template)); } /** * @param string $name PerfectCMSImages name * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... * @param string $alt alt tag for image -optional * @param string $attributes additional attributes * * @return ArrayData */ public function PerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') { return $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes); } /** * @param string $name PerfectCMSImages name * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... * @param string $alt alt tag for image -optional * @param string $attributes additional attributes * * @return ArrayData */ public function getPerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') { $retinaLink = $this->PerfectCMSImageLinkRetina($name); $nonRetinaLink = $this->PerfectCMSImageLinkNonRetina($name); $retinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebP($name); $nonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebP($name); $mobileRetinaLink = $this->PerfectCMSImageLinkRetinaForMobile($name); $mobileNonRetinaLink = $this->PerfectCMSImageLinkNonRetinaForMobile($name); $mobileRetinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebPForMobile($name); $mobileNonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebPForMobile($name); $width = PerfectCMSImages::get_width($name, true); $height = PerfectCMSImages::get_height($name, true); $mobileMediaWidth = PerfectCMSImages::get_mobile_media_width($name); if (! $alt) { $alt = $this->getOwner()->Title; } return ArrayData::create( [ 'MobileMediaWidth' => $mobileMediaWidth, 'Width' => $width, 'Height' => $height, 'Alt' => Convert::raw2att($alt), 'MobileRetinaLink' => $mobileRetinaLink, 'MobileNonRetinaLink' => $mobileNonRetinaLink, 'MobileRetinaLinkWebP' => $mobileRetinaLinkWebP, 'MobileNonRetinaLinkWebP' => $mobileNonRetinaLinkWebP, 'RetinaLink' => $retinaLink, 'NonRetinaLink' => $nonRetinaLink, 'RetinaLinkWebP' => $retinaLinkWebP, 'NonRetinaLinkWebP' => $nonRetinaLinkWebP, 'Attributes' => DBField::create_field('HTMLText', $attributes), ] ); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkNonRetina(string $name): string { return $this->PerfectCMSImageLink($name); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkRetina(string $name): string { return $this->PerfectCMSImageLink($name, true); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkNonRetinaWebP(string $name): string { return $this->PerfectCMSImageLink($name, false, true); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkRetinaWebP(string $name): string { return $this->PerfectCMSImageLink($name, true, true); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkNonRetinaForMobile(string $name): string { return $this->PerfectCMSImageLink($name, false, false, true); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkRetinaForMobile(string $name): string { return $this->PerfectCMSImageLink($name, true, false, true); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkNonRetinaWebPForMobile(string $name): string { return $this->PerfectCMSImageLink($name, false, true, true); } /** * @var string name of Image Field template * * @return string (link) */ public function PerfectCMSImageLinkRetinaWebPForMobile(string $name): string { return $this->PerfectCMSImageLink($name, true, true, true); } /** * @var string name of Image Field template * * @return string (link) */ public function getPerfectCMSImageAbsoluteLink(string $link): string { return Director::absoluteURL($link); } /** * returns image link (if any). */ public function PerfectCMSImageLink(string $name, ?bool $useRetina = false, ?bool $isWebP = false, ?bool $forMobile = false): string { /** @var null|Image $image */ $image = $this->owner; if ($image && $image->exists() && $image instanceof Image) { //we are all good ... } else { $image = ImageManipulations::get_backup_image($name); } if ($image && $image->exists() && $image instanceof Image) { // $backEndString = Image::get_backend(); // $backend = Injector::inst()->get($backEndString); $link = ImageManipulations::get_image_link($image, $name, $useRetina, $forMobile); if ($isWebP) { $link = ImageManipulations::web_p_link($link); } return '' !== $link ? ImageManipulations::add_fake_parts($image, $link) : ''; } // no image -> provide placeholder if in DEV MODE only!!! if (Director::isDev()) { return ImageManipulations::get_placeholder_image_tag($name); } return ''; } } |