|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\PerfectCmsImages\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Upload_Validator; |
|
6
|
|
|
use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages; |
|
7
|
|
|
|
|
8
|
|
|
class PerfectCmsImageValidator extends Upload_Validator |
|
9
|
|
|
{ |
|
10
|
|
|
protected $fieldName = ''; |
|
11
|
|
|
|
|
12
|
|
|
public function setFieldName($fieldName) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->fieldName = $fieldName; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Looser check validation that doesn't do is_upload_file() |
|
19
|
|
|
* checks as we're faking a POST request that PHP didn't generate |
|
20
|
|
|
* itself. |
|
21
|
|
|
* |
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
|
|
public function validate() |
|
25
|
|
|
{ |
|
26
|
|
|
$hasError = false; |
|
27
|
|
|
$name = $this->fieldName; |
|
28
|
|
|
if (PerfectCMSImages::get_enforce_size($name)) { |
|
29
|
|
|
$useRetina = PerfectCMSImages::use_retina($name); |
|
30
|
|
|
$multiplier = PerfectCMSImages::get_multiplier($useRetina); |
|
31
|
|
|
$widthRecommendation = ((int) PerfectCMSImages::get_width($name, true)) * $multiplier; |
|
32
|
|
|
$heightRecommendation = ((int)PerfectCMSImages::get_height($name, true)) * $multiplier; |
|
33
|
|
|
if (0 !== $widthRecommendation) { |
|
34
|
|
|
if (!$this->isImageCorrectWidth(true, $widthRecommendation)) { |
|
35
|
|
|
$this->errors[] = 'Expected width: ' . $widthRecommendation . 'px;'; |
|
36
|
|
|
$hasError = true; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($heightRecommendation) { |
|
41
|
|
|
if (!$this->isImageCorrectWidth(false, $heightRecommendation)) { |
|
42
|
|
|
$this->errors[] = 'Expected height: ' . $heightRecommendation . 'px;'; |
|
43
|
|
|
$hasError = true; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$parentResult = parent::validate(); |
|
49
|
|
|
if ($hasError) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $parentResult; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function isImageCorrectWidth($isWidth, $recommendedWidthOrHeight) |
|
57
|
|
|
{ |
|
58
|
|
|
$actualWidthOrHeight = $this->getWidthOrHeight($isWidth); |
|
59
|
|
|
if ($actualWidthOrHeight) { |
|
60
|
|
|
if ($actualWidthOrHeight !== $recommendedWidthOrHeight) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getWidthOrHeight($isWidth) |
|
69
|
|
|
{ |
|
70
|
|
|
$imageSize = false; |
|
71
|
|
|
if (isset($this->tmpFile['tmp_name'])) { |
|
72
|
|
|
$imageSize = getimagesize($this->tmpFile['tmp_name']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// $imagefile = $this->getFullPath(); |
|
76
|
|
|
// if($this->exists() && file_exists($imageFile)) { |
|
77
|
|
|
// $imageSize = getimagesize($imagefile); |
|
78
|
|
|
// } |
|
79
|
|
|
|
|
80
|
|
|
if (false === $imageSize) { |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($isWidth) { |
|
85
|
|
|
return $imageSize[0]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $imageSize[1]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|