Shape   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 83
c 2
b 2
f 0
dl 0
loc 159
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 11 3
A horizontalRentangle() 0 7 1
A square() 0 6 1
A verticalRectangle() 0 7 1
A minBetweenDimensions() 0 3 1
A get() 0 3 1
A modify() 0 18 5
A circle() 0 77 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jonasdamher\Simplifyimage\Utils;
6
7
use Jonasdamher\Simplifyimage\Core\ResponseHandler;
8
9
/**
10
 * Image shape crop.
11
 * 
12
 * default,
13
 * circle, square, 
14
 * verticalRectangle, horizontalRentangle.
15
 */
16
class Shape
17
{
18
19
	private array $AllowsShapesTypes = ['horizontalRentangle', 'verticalRectangle', 'circle', 'square', 'default'];
20
	private string $type = 'default';
21
22
	public function get(): string
23
	{
24
		return $this->type;
25
	}
26
27
	/**
28
	 * Has options default, horizontalRentangle, verticalRectangle, 
29
	 * circle, square.
30
	 * @param string $type
31
	 */
32
	public function set(string $type)
33
	{
34
		try {
35
			if (!in_array($type, $this->AllowsShapesTypes, true)) {
36
				throw new \Exception("Don't exist shape (" . $type . ')');
37
			}
38
		} catch (\Exception $e) {
39
			$type = 'default';
40
			ResponseHandler::fail($e->getMessage());
41
		} finally {
42
			$this->type = $type;
43
		}
44
	}
45
46
	private function minBetweenDimensions(array $dimensions): int
47
	{
48
		return min($dimensions['x'], $dimensions['y']);
49
	}
50
51
	private function square(array $dimensions): array
52
	{
53
		$min = $this->minBetweenDimensions($dimensions);
54
		$dimensions['x'] = $min;
55
		$dimensions['y'] = $min;
56
		return $dimensions;
57
	}
58
59
	private function horizontalRentangle(array $dimensions, array $position): array
60
	{
61
		$heightRedimension = ceil(($dimensions['x'] / 161) * 100);
62
		$position['x'] += ($dimensions['x'] - $heightRedimension) / 2;
63
		$dimensions['y'] = $heightRedimension;
64
65
		return $dimensions;
66
	}
67
68
	private function verticalRectangle(array $dimensions, array $position): array
69
	{
70
		$widthRedimension = ceil(($dimensions['y'] / 161) * 100);
71
		$position['y'] += ($dimensions['y'] - $widthRedimension) / 2;
72
		$dimensions['x'] = $widthRedimension;
73
74
		return $dimensions;
75
	}
76
77
	private function circle($image, array $dimensions, array $position)
78
	{
79
		try {
80
			$newDimensions = $this->square($dimensions);
81
			$min = $this->minBetweenDimensions($dimensions);
82
83
			$croppedImage = imagecrop($image, [
84
				'x' => $position['x'],
85
				'y' => $position['y'],
86
				'width' => $newDimensions['x'],
87
				'height' => $newDimensions['y']
88
			]);
89
90
			// Create mask circle
91
			$circleMask = imagecreatetruecolor($min, $min);
92
93
			if (!is_resource($croppedImage) || !is_resource($circleMask)) {
94
				throw new \Exception('Could not apply circular shape.');
95
			}
96
97
			imagealphablending($circleMask, false);
98
99
			// Create colors
100
			$magentaColor = imagecolorallocatealpha($circleMask, 255, 0, 255, 0);
101
			$transparent = imagecolorallocatealpha($circleMask, 255, 255, 255, 127);
102
103
			// Add color mask
104
			imagefill($circleMask, 0, 0, $magentaColor);
105
106
			// Draw circle border line circleMask
107
			imagearc(
108
				$circleMask,
109
				$min / 2,
110
				$min / 2,
111
				$min,
112
				$min,
113
				0,
114
				360,
115
				$transparent
116
			);
117
118
			// Fill circle
119
			imagefilltoborder(
120
				$circleMask,
121
				$min / 2,
122
				$min / 2,
123
				$transparent,
124
				$transparent
125
			);
126
			// Mask circle final
127
128
			// Add alpha channel to image
129
			imagealphablending($croppedImage, true);
130
131
			// Add mask to image
132
			imagecopyresampled(
133
				$croppedImage,
134
				$circleMask,
135
				0,
136
				0,
137
				0,
138
				0,
139
				$min,
140
				$min,
141
				$min,
142
				$min
143
			);
144
145
			// remove mask color to image
146
			imagecolortransparent($croppedImage, $magentaColor);
147
148
			imagedestroy($image);
149
		} catch (\Exception $e) {
150
			$croppedImage = $image;
151
			ResponseHandler::fail($e->getMessage());
152
		} finally {
153
			return $croppedImage;
154
		}
155
	}
156
157
	public function modify($image, array $position, array $dimensions)
158
	{
159
		$shapeType = $this->get();
160
		$shape = false;
161
162
		switch ($shapeType) {
163
			case 'horizontalRentangle':
164
			case 'verticalRectangle':
165
				$shape = $this->$shapeType($dimensions, $position);
166
				break;
167
			case 'square':
168
				$shape = $this->$shapeType($dimensions);
169
				break;
170
			case 'circle':
171
				$shape = $this->$shapeType($image, $dimensions, $position);
172
				break;
173
		}
174
		return $shape;
175
	}
176
}
177