-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vipsthumbnail.php
164 lines (132 loc) · 3.83 KB
/
Vipsthumbnail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Floriankarsten;
use Exception;
/**
* TBA
*
*/
class Vipsthumbnail
{
public $options = [];
public $src;
public $dst;
public function __construct(string $src, string $dst, array $options = [])
{
$this->src = $src;
$this->dst = $dst;
$thumbOptions = option('thumbs') ?? [];
// option merging: plugin defaults > config options > options from image
$this->options = array_merge($this->defaults(), $thumbOptions, array_filter($options));
if ($this->options['log'] === true) {
// $this->logMessage(json_encode($this->defaults()));
// $this->logMessage(json_encode(option('thumbs')));
// $this->logMessage(json_encode(array_filter($options)));
$this->logMessage(json_encode($this->options));
}
}
protected function defaults(): array
{
return [
'bin' => 'vipsthumbnail',
'interlace' => false,
'autoOrient' => false,
'crop' => false,
'height' => null,
'strip' => true,
'quality' => 90,
'width' => null,
'log' => false
];
}
function logMessage($log_msg)
{
// dumblog from stack overflow please no judging
if (!empty($this->options['logdir'])) {
$log_filename = $this->options['logdir'];
} else {
$log_filename = __DIR__ . "/logs";
}
if (!file_exists($log_filename)) {
// create directory/folder uploads.
mkdir($log_filename, 0777, true);
}
$log_file_data = $log_filename . '/vipthumbnaillog_' . date('d-M-Y') . '.log';
file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND);
}
protected function autoOrient()
{
if ($this->options['autoOrient'] === true) {
return '--rotate';
}
}
protected function convert(): string
{
return sprintf($this->options['bin'] . ' %s', $this->src);
}
protected function interlace()
{
if ($this->options['interlace']) {
return 'interlace';
}
}
public function process(): string
{
$command = [];
$outputOptions = [];
$outputOptions[] = $this->strip();
$outputOptions[] = $this->interlace();
$outputOptions[] = $this->quality();
$outputOptions = implode(',', array_filter($outputOptions));
$command[] = $this->convert();
$command[] = $this->autoOrient();
$command[] = $this->resize();
$command[] = $this->save($outputOptions);
// remove all null values and join the parts
$command = implode(' ', array_filter($command));
if ($this->options['log'] === true) {
$this->logMessage($command);
}
// try to execute the command
exec($command, $output, $return);
// log broken commands
if ($return !== 0) {
throw new Exception('The Vips convert command could not be executed: ' . $command);
}
return $this->dst;
}
protected function save($outputOptions): string
{
return sprintf('-o %s[%s]', $this->dst, $outputOptions);
}
protected function quality(): string
{
return 'Q=' . $this->options['quality'];
}
protected function resize(): string
{
// normalize crop
// here it should be possible to take $this->options['crop'] "center" etc to make crops by direction
// dirty weak check $this->options['crop'] == true if its either true or string "center" etc
if (is_string($this->options['crop'])) {
$this->options['crop'] = true;
}
// simple resize
if ($this->options['crop'] === false) {
return sprintf('--size %sx%s', $this->options['width'], $this->options['height']);
}
if ($this->options['crop'] === true && $this->options['height'] === 0) {
// assume crop to square like ->crop(100)
return sprintf('--size %sx%s --smartcrop attention', $this->options['width'], $this->options['width']);
}
if ($this->options['crop'] === true) {
// assume crop with exact sizes
return sprintf('--size %sx%s --smartcrop attention', $this->options['width'], $this->options['width']);
}
}
protected function strip()
{
if ($this->options['strip']) {
return 'strip';
}
}
}