ImageResizer::resize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CloudControl\Cms\images {
4
5
    use CloudControl\Cms\util\StringUtil;
6
7
    /**
8
     * Class ImageResizer
9
     * @package CloudControl\Cms\images
10
     */
11
    class ImageResizer
12
    {
13
        protected $imageSet;
14
15
        /**
16
         * ImageResizer constructor.
17
         *
18
         * @param $imageSet
19
         */
20
        public function __construct($imageSet)
21
        {
22
            $this->imageSet = $imageSet;
23
        }
24
25
        /**
26
         * @param $imagePath
27
         *
28
         * @return array
29
         * @throws \Exception
30
         */
31
        public function applyImageSetToImage($imagePath)
32
        {
33
            $returnFileNames = array();
34
            $filename = '';
35
            if (file_exists($imagePath)) {
36
                foreach ($this->imageSet as $set) {
37
                    if ($set->method == 'resize') {
38
                        $filename = $this->resize($imagePath, $set->width, $set->height);
39
                    } elseif ($set->method == 'smartcrop') {
40
                        $filename = $this->smartcrop($imagePath, $set->width, $set->height);
41
                    } elseif ($set->method == 'boxcrop') {
42
                        $filename = $this->boxcrop($imagePath, $set->width, $set->height);
43
                    }
44
                    $returnFileNames[$set->slug] = $filename;
45
                }
46
                return $returnFileNames;
47
            } else {
48
                throw new \Exception('Image doesnt exist: ' . $imagePath);
49
            }
50
        }
51
52
        /**
53
         * @param string $imagePath
54
         * @param string $width
55
         * @param string $height
56
         * @return string
57
         * @throws \Exception
58
         */
59
        public function resize($imagePath = '', $width = '', $height = '')
60
        {
61
            $modifier = '-r' . $width . 'x' . $height;
62
            return $this->applyMethod('Resize', $imagePath, $width, $height, $modifier);
63
        }
64
65
        /**
66
         * @param string $imagePath
67
         * @param string $width
68
         * @param string $height
69
         * @return string
70
         * @throws \Exception
71
         */
72
        public function smartcrop($imagePath = '', $width = '', $height = '')
73
        {
74
            $modifier = '-s' . $width . 'x' . $height;
75
            return $this->applyMethod('SmartCrop', $imagePath, $width, $height, $modifier);
76
        }
77
78
        /**
79
         * @param string $imagePath
80
         * @param string $width
81
         * @param string $height
82
         * @return string
83
         * @throws \Exception
84
         */
85
        public function boxcrop($imagePath = '', $width = '', $height = '')
86
        {
87
            $modifier = '-b' . $width . 'x' . $height;
88
            return $this->applyMethod('BoxCrop', $imagePath, $width, $height, $modifier);
89
        }
90
91
        /**
92
         * @param        $imagePath
93
         * @param string $modifier
94
         *
95
         * @return string
96
         */
97
        private function modifyName($imagePath, $modifier = '')
98
        {
99
            $filename = basename($imagePath);
100
            $path = dirname($imagePath);
101
            $fileParts = explode('.', $filename);
102
            if (count($fileParts) > 1) {
103
                $extension = end($fileParts);
104
                array_pop($fileParts);
105
                $fileNameWithoutExtension = implode('-', $fileParts);
106
                $fileNameWithoutExtension = StringUtil::slugify($fileNameWithoutExtension);
107
                $filename = $fileNameWithoutExtension . $modifier . '.' . $extension;
108
            } else {
109
                $filename = StringUtil::slugify($filename);
110
            }
111
112
            if (file_exists($path . '/' . $filename)) {
113
                $fileParts = explode('.', $filename);
114
                if (count($fileParts) > 1) {
115
                    $extension = end($fileParts);
116
                    array_pop($fileParts);
117
                    $fileNameWithoutExtension = implode('-', $fileParts);
118
                    $fileNameWithoutExtension .= '-copy';
119
                    $filename = $fileNameWithoutExtension . '.' . $extension;
120
                } else {
121
                    $filename .= '-copy';
122
                }
123
                return $this->modifyName($path . '/' . $filename);
124
            }
125
            return $path . '/' . $filename;
126
        }
127
128
        private function applyMethod($method, $imagePath, $width, $height, $modifier)
129
        {
130
            $method = 'CloudControl\\Cms\\images\\methods\\' . $method;
131
            $destination = $this->modifyName($imagePath, $modifier);
132
            if (file_exists($imagePath)) {
133
                $image = new Image();
134
                $image->loadImage($imagePath);
135
                $resize = new $method();
136
                $resize->SetWidth($width);
137
                $resize->SetHeight($height);
138
                $resizedImageResource = $resize->Execute($image->getImageResource());
139
                $resizedImage = new Image();
140
                $resizedImage->loadImage($resizedImageResource);
141
                $resizedImage->saveImage($destination, $resizedImage->getImageMimeType($imagePath), 80);
142
                return basename($destination);
143
            } else {
144
                throw new \Exception('Image doesnt exist: ' . $imagePath);
145
            }
146
        }
147
    }
148
}