1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Bluz PHP Team |
5
|
|
|
* @link https://github.com/bluzphp/skeleton |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @namespace |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Image; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Image resize and crop |
16
|
|
|
* |
17
|
|
|
* @category Application |
18
|
|
|
* @package Library |
19
|
|
|
*/ |
20
|
|
|
class Thumbnail |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Full path to file |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $path; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Filename |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $file; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Width of thumbnail, zero means leave original size |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $width = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Height of thumbnail, zero means leave original size |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $height = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor of Image Tool |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
* |
55
|
|
|
* @param string $file |
56
|
|
|
*/ |
57
|
|
|
public function __construct(string $file) |
58
|
|
|
{ |
59
|
|
|
$this->path = dirname($file); |
60
|
|
|
$this->file = substr($file, strlen($this->path) + 1); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Setup width |
65
|
|
|
* |
66
|
|
|
* @param int $width |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function setWidth(int $width): void |
71
|
|
|
{ |
72
|
|
|
$this->width = $width; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Setup height |
77
|
|
|
* |
78
|
|
|
* @param int $height |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function setHeight(int $height): void |
83
|
|
|
{ |
84
|
|
|
$this->height = $height; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Generate thumbnail |
89
|
|
|
* |
90
|
|
|
* @return string Path to new file |
91
|
|
|
* @throws Exception |
92
|
|
|
* @throws \ImagickException |
93
|
|
|
*/ |
94
|
|
|
public function generate(): string |
95
|
|
|
{ |
96
|
|
|
$dir = $this->path . '/.thumb/' . $this->width . 'x' . $this->height; |
97
|
|
|
|
98
|
|
|
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) { |
99
|
|
|
throw new Exception('Thumbnail image can\'t be save. Parent directory is not writable'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Thumbnail already exists |
103
|
|
|
// then remove it and regenerate |
104
|
|
|
if (file_exists($dir . '/' . $this->file)) { |
105
|
|
|
unlink($dir . '/' . $this->file); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (class_exists('\\Imagick')) { |
109
|
|
|
$image = new \Imagick($this->path . '/' . $this->file); |
110
|
|
|
} elseif (function_exists('gd_info')) { |
111
|
|
|
$image = new Gd($this->path . '/' . $this->file); |
112
|
|
|
} else { |
113
|
|
|
// return original file |
114
|
|
|
return $this->path . '/' . $this->file; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$image->cropThumbnailImage($this->width, $this->height); |
118
|
|
|
$image->writeimage($dir . '/' . $this->file); |
119
|
|
|
|
120
|
|
|
return $dir . '/' . $this->file; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|