PerfectCmsImageValidator::isImageCorrectWidth()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 10
rs 10
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