|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class PerfectCMSImage_Validator extends Upload_Validator |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
protected $fieldName = ''; |
|
6
|
|
|
|
|
7
|
|
|
public function setFieldName($fieldName) |
|
8
|
|
|
{ |
|
9
|
|
|
$this->fieldName = $fieldName; |
|
10
|
|
|
} |
|
11
|
|
|
/** |
|
12
|
|
|
* Looser check validation that doesn't do is_upload_file() |
|
13
|
|
|
* checks as we're faking a POST request that PHP didn't generate |
|
14
|
|
|
* itself. |
|
15
|
|
|
* |
|
16
|
|
|
* @return boolean |
|
17
|
|
|
*/ |
|
18
|
|
|
public function validate() |
|
19
|
|
|
{ |
|
20
|
|
|
$hasError = false; |
|
21
|
|
|
if(PerfectCMSImageDataExtension::get_enforce_size($this->fieldName)) { |
|
22
|
|
|
$widthRecommendation = (PerfectCMSImageDataExtension::get_width($this->fieldName) * 2); |
|
23
|
|
|
$heightRecommendation = (PerfectCMSImageDataExtension::get_height($this->fieldName) * 2); |
|
24
|
|
|
if ($widthRecommendation) { |
|
25
|
|
|
if (! $this->isImageCorrectWidth(true, $widthRecommendation)) { |
|
26
|
|
|
$this->errors[] = "Expected width: " . $widthRecommendation . "px;"; |
|
27
|
|
|
$hasError = true; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if ($heightRecommendation) { |
|
32
|
|
|
if (! $this->isImageCorrectWidth(false, $heightRecommendation)) { |
|
33
|
|
|
$this->errors[] = "Expected height: " . $heightRecommendation . "px;"; |
|
34
|
|
|
$hasError = true; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
$parentResult = parent::validate(); |
|
39
|
|
|
if ($hasError) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
return $parentResult; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function isImageCorrectWidth($isWidth, $recommendedWidthOrHeight) |
|
46
|
|
|
{ |
|
47
|
|
|
$actualWidthOrHeight = $this->getWidthOrHeight($isWidth); |
|
48
|
|
|
if ($actualWidthOrHeight) { |
|
49
|
|
|
if ($actualWidthOrHeight != $recommendedWidthOrHeight) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
protected function getWidthOrHeight($isWidth) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$imageSize = false; |
|
60
|
|
|
if (isset($this->tmpFile["tmp_name"])) { |
|
61
|
|
|
$imageSize = getimagesize($this->tmpFile["tmp_name"]); |
|
62
|
|
|
} else { |
|
|
|
|
|
|
63
|
|
|
// $imagefile = $this->getFullPath(); |
|
|
|
|
|
|
64
|
|
|
// if($this->exists() && file_exists($imageFile)) { |
|
|
|
|
|
|
65
|
|
|
// $imageSize = getimagesize($imagefile); |
|
|
|
|
|
|
66
|
|
|
// } |
|
67
|
|
|
} |
|
68
|
|
|
if ($imageSize === false) { |
|
69
|
|
|
return false; |
|
70
|
|
|
} else { |
|
71
|
|
|
if ($isWidth) { |
|
72
|
|
|
return $imageSize[0]; |
|
73
|
|
|
} else { |
|
74
|
|
|
return $imageSize[1]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.