Contrast   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A modify() 0 17 4
A set() 0 22 5
A get() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jonasdamher\Simplifyimage\Utils;
6
7
use Jonasdamher\Simplifyimage\Core\ResponseHandler;
8
9
/**
10
 * Handle image contrast.
11
 */
12
class Contrast
13
{
14
15
	private int $contrast = 0;
16
17
	public function get(): int
18
	{
19
		return $this->contrast;
20
	}
21
22
	/**
23
	 * Image constrast.
24
	 * Options: low, medium, hight, default.
25
	 * 
26
	 * @default By "default".
27
	 */
28
	public function set(string $contrast)
29
	{
30
		try {
31
			switch ($contrast) {
32
				case 'low':
33
					$contrastNumber = -10;
34
					break;
35
				case 'medium':
36
					$contrastNumber = -50;
37
					break;
38
				case 'hight':
39
					$contrastNumber = -80;
40
					break;
41
				default:
42
					$contrastNumber = 0;
43
					throw new \Exception("Don't exist option ($contrast) for contrast.");
44
					break;
45
			}
46
		} catch (\Exception $e) {
47
			ResponseHandler::fail($e->getMessage());
48
		} finally {
49
			$this->contrast = $contrastNumber;
50
		}
51
	}
52
53
	public function modify($image)
54
	{
55
		try {
56
			if ($this->get() != 0) {
57
58
				$imageWithContrast = $image;
59
60
				if (!imagefilter($imageWithContrast, IMG_FILTER_CONTRAST, $this->get())) {
61
					throw new \Exception('Fail to apply contrast in image.');
62
				}
63
64
				$image = $imageWithContrast;
65
			}
66
		} catch (\Exception $e) {
67
			ResponseHandler::fail($e->getMessage());
68
		} finally {
69
			return $image;
70
		}
71
	}
72
}
73