Source of file PerfectCmsImageValidator.php
Size: 2,636 Bytes - Last Modified: 2021-12-23T10:45:59+00:00
/var/www/docs.ssmods.com/process/src/src/Filesystem/PerfectCmsImageValidator.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 | <?php namespace Sunnysideup\PerfectCmsImages\Filesystem; use SilverStripe\Assets\Upload_Validator; use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages; class PerfectCmsImageValidator extends Upload_Validator { protected $fieldName = ''; public function setFieldName($fieldName) { $this->fieldName = $fieldName; } /** * Looser check validation that doesn't do is_upload_file() * checks as we're faking a POST request that PHP didn't generate * itself. * * @return bool */ public function validate() { $hasError = false; $name = $this->fieldName; if (PerfectCMSImages::get_enforce_size($name)) { $useRetina = PerfectCMSImages::use_retina($name); $multiplier = PerfectCMSImages::get_multiplier($useRetina); $widthRecommendation = PerfectCMSImages::get_width($name, true) * $multiplier; $heightRecommendation = PerfectCMSImages::get_height($name, true) * $multiplier; if (0 !== $widthRecommendation) { if (! $this->isImageCorrectWidth(true, $widthRecommendation)) { $this->errors[] = 'Expected width: ' . $widthRecommendation . 'px;'; $hasError = true; } } if ($heightRecommendation) { if (! $this->isImageCorrectWidth(false, $heightRecommendation)) { $this->errors[] = 'Expected height: ' . $heightRecommendation . 'px;'; $hasError = true; } } } $parentResult = parent::validate(); if ($hasError) { return false; } return $parentResult; } protected function isImageCorrectWidth($isWidth, $recommendedWidthOrHeight) { $actualWidthOrHeight = $this->getWidthOrHeight($isWidth); if ($actualWidthOrHeight) { if ($actualWidthOrHeight !== $recommendedWidthOrHeight) { return false; } } return true; } protected function getWidthOrHeight($isWidth) { $imageSize = false; if (isset($this->tmpFile['tmp_name'])) { $imageSize = getimagesize($this->tmpFile['tmp_name']); } // $imagefile = $this->getFullPath(); // if($this->exists() && file_exists($imageFile)) { // $imageSize = getimagesize($imagefile); // } if (false === $imageSize) { return false; } if ($isWidth) { return $imageSize[0]; } return $imageSize[1]; } } |