Passed
Push — master ( e3a5c6...5f245b )
by Wanderson
01:25
created

Thumb   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 73
dl 0
loc 187
rs 10
c 0
b 0
f 0
wmc 29

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setWidth() 0 2 2
A padding() 0 2 1
A validate() 0 5 2
A setMode() 0 3 2
A setSize() 0 5 1
A showDefault() 0 6 1
A center() 0 2 1
A setCache() 0 2 1
A setImageUrl() 0 7 2
A saveCache() 0 8 2
A getWidth() 0 2 1
A applyOpacity() 0 4 1
A setDefault() 0 3 1
A getHeight() 0 2 1
A config() 0 8 2
A normal() 0 2 1
A show() 0 9 2
A setHeight() 0 2 2
A setPadding() 0 2 1
A getPadding() 0 2 1
1
<?php
2
3
namespace Win\File\Image;
4
5
use PHPThumb\GD;
0 ignored issues
show
Bug introduced by
The type PHPThumb\GD was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Win\File\Directory;
7
use Win\File\Image;
8
9
/**
10
 * Gera thumbs utilizando alguma biblioteca de terceiros
11
 */
12
class Thumb {
13
14
	private $url;
15
	private $cache = null;
16
	private $default = null;
17
	private $opacity = 100;
18
19
	/** @var AdapterInterface Biblioteca responsável pela thumb */
0 ignored issues
show
Bug introduced by
The type Win\File\Image\AdapterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
	public $adapter;
21
22
	/** @var GD */
23
	private $image;
24
	private $imageUrl;
25
	private $width;
26
	private $height;
27
	private $padding;
28
	private $quality;
29
	private $mode;
30
	static private $modes = ['normal', 'center', 'padding'];
31
32
	/**
33
	 * Construtor
34
	 */
35
	public function __construct() {
36
		$this->width = Image::MAX_WIDTH;
37
		$this->height = Image::MAX_WIDTH;
38
		$this->quality = Image::QUALITY;
39
		$this->mode = 'normal';
40
	}
41
42
	public function getWidth() {
43
		return $this->width;
44
	}
45
46
	public function getHeight() {
47
		return $this->height;
48
	}
49
50
	public function getPadding() {
51
		return $this->padding;
52
	}
53
54
	public function setWidth($width) {
55
		$this->width = (int) $width > 0 ? $width : Image::MAX_WIDTH;
56
	}
57
58
	public function setHeight($height) {
59
		$this->height = (int) $height > 0 ? $height : Image::MAX_HEIGHT;
60
	}
61
62
	public function setPadding($padding) {
63
		$this->padding = (int) $padding;
64
	}
65
66
	public function setCache($cache) {
67
		$this->cache = $cache;
68
	}
69
70
	/**
71
	 * Define a imagem padrão caso não encontre a imagem
72
	 * @param string $default
73
	 * @param int $opacity
74
	 */
75
	public function setDefault($default, $opacity) {
76
		$this->default = $default;
77
		$this->opacity = $opacity;
78
	}
79
80
	/**
81
	 * Inicia configurações com base na url
82
	 * @param string $url
83
	 */
84
	public function config($url) {
85
		$config = explode('/', str_replace('cache/thumb/', '', $url));
86
87
		if (count($config) >= 4) {
88
			$this->setMode(array_shift($config));
89
			$this->url = implode('/', $config);
90
			$this->setSize($config);
91
			$this->setImageUrl($config);
92
		}
93
	}
94
95
	/**
96
	 * Define o nome da imagem
97
	 * é possivel utililizar dois modos de apelido para a imagem
98
	 * diretorio/real/[APELIDO]_nome-imagem.jpg
99
	 * diretorio/real/nome-imagem/[APELIDO].jpg
100
	 */
101
	private function setImageUrl($config) {
102
		$imageName = explode('_', array_pop($config));
103
		$this->imageUrl = BASE_PATH . '/' . implode('/', $config) . '/' . end($imageName);
104
105
		if (!file_exists($this->imageUrl)) {
106
			$imageExt = explode('.', end($imageName));
107
			$this->imageUrl = BASE_PATH . '/' . implode('/', $config) . '.' . $imageExt[1];
108
		}
109
	}
110
111
	/** @param string $mode */
112
	private function setMode($mode) {
113
		if (in_array($mode, static::$modes)) {
0 ignored issues
show
Bug introduced by
Since $modes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $modes to at least protected.
Loading history...
114
			$this->mode = $mode;
115
		}
116
	}
117
118
	/**
119
	 * Define as dimensões
120
	 * @param mixed[] $config
121
	 */
122
	private function setSize(&$config) {
123
		$size = explode('x', array_shift($config), 2);
124
		$this->setWidth($size[0]);
125
		$this->setHeight($size[1]);
126
		$this->setPadding($size[2]);
127
	}
128
129
	/** Exibe a thumb final */
130
	public function show() {
131
		if ($this->validate()) {
132
			$this->image = new GD($this->imageUrl);
133
			$this->image->setOptions(['jpegQuality' => 75]);
134
			$thumb = $this->{$this->mode}();
135
			$this->saveCache($thumb);
136
			$thumb->show();
137
		} else {
138
			$this->showDefault();
139
		}
140
	}
141
142
	/** @return boolean */
143
	private function validate() {
144
		if (!file_exists($this->imageUrl)) {
145
			return false;
146
		}
147
		return true;
148
	}
149
150
	/**
151
	 * Salva a imagem no diretorio de cache se necessário
152
	 * @param GD $thumb
153
	 */
154
	private function saveCache($thumb) {
155
		if (!is_null($this->cache)) {
156
			$cacheDir = BASE_PATH . '/' . $this->cache . '/thumb';
157
			$url = explode('/', $cacheDir . '/' . $this->mode . '/' . $this->url);
158
			$newFile = array_pop($url);
159
			$dir = new Directory(implode('/', $url));
160
			$dir->create();
161
			$thumb->save($dir->getPath() . $newFile);
162
		}
163
	}
164
165
	/** Exibe a imagem default */
166
	private function showDefault() {
167
		$this->image = new GD(BASE_PATH . '/' . $this->default);
168
		$this->padding = $this->width * 0.2;
169
		$thumb = $this->padding();
170
		$this->applyOpacity($thumb);
171
		$thumb->show();
172
	}
173
174
	/**
175
	 * Aplica opacidade na imagem
176
	 * @param GD $thumb
177
	 */
178
	private function applyOpacity($thumb) {
179
		$img = imagecreatetruecolor($this->width, $this->height);
180
		imagefilledrectangle($img, 0, 0, $this->width, $this->height, imagecolorallocate($img, 255, 255, 255));
181
		imagecopymerge($thumb->getOldImage(), $img, 0, 0, 0, 0, $this->width, $this->height, 100 - $this->opacity);
182
	}
183
184
	/* Modos */
185
186
	/** @return GD */
187
	protected function normal() {
188
		return $this->image->resize($this->width, $this->height);
189
	}
190
191
	/** @return GD */
192
	protected function center() {
193
		return $this->image->adaptiveResize($this->width, $this->height);
194
	}
195
196
	/** @return GD */
197
	protected function padding() {
198
		return $this->image->resize($this->width, $this->height)->pad($this->width + $this->padding, $this->height + $this->padding);
199
	}
200
201
}
202