Passed
Push — develop ( bfc038...8eb1da )
by Jens
03:00
created

ImageResizer::modifyName()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 24

Duplication

Lines 22
Ratio 73.33 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 4
eloc 24
c 5
b 0
f 2
nc 6
nop 2
dl 22
loc 30
rs 8.5806
1
<?php
2
namespace library\images
3
{
4
5
	/**
6
	 * Class ImageResizer
7
	 * @package library\images
8
	 */
9
	class ImageResizer
10
	{
11
		protected $imageSet;
12
13
		/**
14
		 * ImageResizer constructor.
15
		 *
16
		 * @param $imageSet
17
		 */
18
		public function __construct($imageSet)
19
		{
20
			$this->imageSet = $imageSet;
21
		}
22
23
		/**
24
		 * @param $imagePath
25
		 *
26
		 * @return array
27
		 * @throws \Exception
28
		 */
29
		public function applyImageSetToImage($imagePath)
30
		{
31
			$returnFileNames = array();
32
			$filename = '';
33
			if (file_exists($imagePath)) {
34
				foreach ($this->imageSet as $set) {
35
					if ($set->method == 'resize') {
36
						$filename = $this->resize($imagePath, $set->width, $set->height);
37
					} elseif ($set->method == 'smartcrop') {
38
						$filename = $this->smartcrop($imagePath, $set->width, $set->height);
39
					} elseif ($set->method == 'boxcrop') {
40
						$filename = $this->boxcrop($imagePath, $set->width, $set->height);
41
					}
42
					$returnFileNames[$set->slug] = $filename;
43
				}
44
				return $returnFileNames;
45
			} else {
46
				throw new \Exception('Image doesnt exist: ' . $imagePath);
47
			}
48
		}
49
50
		/**
51
		 * @param string $imagePath
52
		 * @param string $width
53
		 * @param string $height
54
		 * @return string
55
		 * @throws \Exception
56
		 */
57
		public function resize($imagePath='', $width='',$height='')
58
		{
59
			$modifier = '-r' . $width . 'x' . $height;
60
			return $this->applyMethod('Resize', $imagePath, $width,$height, $modifier);
61
		}
62
63
		/**
64
		 * @param string $imagePath
65
		 * @param string $width
66
		 * @param string $height
67
		 * @return string
68
		 * @throws \Exception
69
		 */
70
		public function smartcrop($imagePath='', $width='',$height='')
71
		{
72
			$modifier = '-s' . $width . 'x' . $height;
73
			return $this->applyMethod('SmartCrop', $imagePath, $width,$height, $modifier);
74
		}
75
76
		/**
77
		 * @param string $imagePath
78
		 * @param string $width
79
		 * @param string $height
80
		 * @return string
81
		 * @throws \Exception
82
		 */
83
		public function boxcrop($imagePath='', $width='',$height='')
84
		{
85
			$modifier = '-b' . $width . 'x' . $height;
86
			return $this->applyMethod('BoxCrop', $imagePath, $width,$height, $modifier);
87
		}
88
89
		/**
90
		 * @param        $imagePath
91
		 * @param string $modifier
92
		 *
93
		 * @return string
94
		 */
95
		private function modifyName($imagePath, $modifier='')
96
		{
97
			$filename = basename($imagePath);
98
			$path = dirname($imagePath);
99
			$fileParts = explode('.', $filename);
100 View Code Duplication
			if (count($fileParts) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
				$extension = end($fileParts);
102
				array_pop($fileParts);
103
				$fileNameWithoutExtension = implode('-', $fileParts);
104
				$fileNameWithoutExtension = slugify($fileNameWithoutExtension);
105
				$filename = $fileNameWithoutExtension . $modifier  . '.' . $extension;
106
			} else {
107
				$filename = slugify($filename);
108
			}
109
110 View Code Duplication
			if (file_exists($path . '/' . $filename)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
				$fileParts = explode('.', $filename);
112
				if (count($fileParts) > 1) {
113
					$extension = end($fileParts);
114
					array_pop($fileParts);
115
					$fileNameWithoutExtension = implode('-', $fileParts);
116
					$fileNameWithoutExtension .= '-copy';
117
					$filename = $fileNameWithoutExtension . '.' . $extension;
118
				} else {
119
					$filename .= '-copy';
120
				}
121
				return $this->modifyName($path . '/' . $filename);
122
			}
123
			return $path . '/' . $filename;
124
		}
125
126
		private function applyMethod($method, $imagePath, $width, $height, $modifier)
127
		{
128
			$method = 'library\\images\\methods\\' . $method;
129
			$destination = $this->modifyName($imagePath, $modifier);
130
			if (file_exists($imagePath)) {
131
				$image = new Image();
132
				$image->LoadImage($imagePath);
133
				$resize = new $method();
134
				$resize->SetWidth($width);
135
				$resize->SetHeight($height);
136
				$resizedImageResource = $resize->Execute($image->GetImageResource());
137
				$resizedImage = new Image();
138
				$resizedImage->LoadImage($resizedImageResource);
139
				$resizedImage->SaveImage($destination, $resizedImage->GetImageMimeType($imagePath), 80);
140
				return basename($destination);
141
			} else {
142
				throw new \Exception('Image doesnt exist: ' . $imagePath);
143
			}
144
		}
145
	}
146
}