/
effect_convert2img.php
257 lines (210 loc) · 8.16 KB
/
effect_convert2img.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Benutzt den Konsolen convert oder ffmpeg Befehl.
*
* @author jan
*
* @package redaxo\media-manager
*/
class rex_effect_convert2img extends rex_effect_abstract
{
private const CONVERT_TYPES = [
'pdf',
'ps',
'psd',
'tif',
'tiff',
'bmp',
'eps',
'ico',
'svg',
];
private const VIDEO_TO_IMAGE_TYPES = [
'mp4',
'm4v',
'avi',
'mov',
];
private const CONVERT_TO = [
'jpg' => [
'ext' => 'jpg',
'content-type' => 'image/jpeg',
],
'png' => [
'ext' => 'png',
'content-type' => 'image/png',
],
];
private const DENSITIES = [100, 150, 200, 300, 600];
private const DENSITY_DEFAULT = 150;
private const CONVERT_TOS = ['jpg', 'png'];
private const CONVERT_TO_DEFAULT = 'jpg';
public function execute()
{
if (!isset(self::CONVERT_TO[(string) $this->params['convert_to']])) {
$convertTo = self::CONVERT_TO[self::CONVERT_TO_DEFAULT];
} else {
$convertTo = self::CONVERT_TO[(string) $this->params['convert_to']];
}
if ($this->isVideoToImageConversionSupported()) {
$inputFile = rex_type::notNull($this->media->getMediaPath());
// Try to get the duration of the video using ffprobe
$ffprobeCmd = 'ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 ' . escapeshellarg($inputFile);
$duration = exec($ffprobeCmd);
if ($duration) {
$timestamp = gmdate('H:i:s', (int) floor((float) $duration / 2));
} else {
$timestamp = '00:00:01';
}
$outputFile = rex_path::addonCache('media_manager', 'media_manager__convert2img_' . md5($inputFile) . '.' . $convertTo['ext']);
$cmd = 'ffmpeg -y -i ' . escapeshellarg($inputFile) . ' -ss ' . escapeshellarg($timestamp) . ' -vframes 1 ' . escapeshellarg($outputFile);
exec($cmd, $out, $ret);
if (0 !== $ret) {
throw new rex_exception('Unable to exec command ' . $cmd);
}
$this->media->setSourcePath($outputFile);
$this->media->refreshImageDimensions();
$this->media->setFormat($convertTo['ext']);
$this->media->setHeader('Content-Type', $convertTo['content-type']);
$filename = $this->media->getMediaFilename();
$this->media->setMediaFilename($filename);
register_shutdown_function(static function () use ($outputFile) {
rex_file::delete($outputFile);
});
return;
}
$density = (int) $this->params['density'];
$color = $this->params['color'] ?? '';
if (!in_array($density, self::DENSITIES)) {
$density = self::DENSITY_DEFAULT;
}
$fromPath = realpath($this->media->getMediaPath());
$ext = rex_file::extension($fromPath);
if (!$ext) {
return;
}
if (!in_array(strtolower($ext), self::CONVERT_TYPES)) {
return;
}
if (class_exists(Imagick::class)) {
$imagick = new Imagick();
$imagick->setResolution($density, $density);
$imagick->readImage($fromPath . '[0]');
if ('' != $color) {
$imagick->setImageBackgroundColor($color);
$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$imagick->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
}
$imagick->transformImageColorspace(Imagick::COLORSPACE_RGB);
$imagick->setImageFormat($convertTo['ext']);
$gd = imagecreatefromstring($imagick->getImageBlob());
$this->media->setImage($gd);
$this->media->setFormat($convertTo['ext']);
$this->media->setHeader('Content-Type', $convertTo['content-type']);
$this->media->refreshImageDimensions();
return;
}
$convertPath = self::getConvertPath();
if ('' == $convertPath) {
return;
}
$filename = $this->media->getMediaFilename();
$filenameWoExt = substr($filename, 0, strlen($filename) - strlen($ext));
$toPath = rex_path::addonCache('media_manager', 'media_manager__convert2img_' . md5($this->media->getMediaPath()) . '_' . $filenameWoExt . $convertTo['ext']);
$addColor = '' != $color ? ' -background ' . escapeshellarg($color) . ' -flatten' : '';
$cmd = $convertPath . ' -density ' . $density . ' ' . escapeshellarg($fromPath . '[0]') . ' ' . $addColor . ' -colorspace RGB ' . escapeshellarg($toPath);
exec($cmd, $out, $ret);
if (0 != $ret) {
if ($error = error_get_last()) {
throw new rex_exception('Unable to exec command ' . $cmd . ': ' . $error['message']);
}
throw new rex_exception('Unable to exec command ' . $cmd);
}
$this->media->setSourcePath($toPath);
$this->media->refreshImageDimensions();
$this->media->setFormat($convertTo['ext']);
$this->media->setMediaFilename($filename);
$this->media->setHeader('Content-Type', $convertTo['content-type']);
register_shutdown_function(static function () use ($toPath) {
rex_file::delete($toPath);
});
}
public function getName()
{
return rex_i18n::msg('media_manager_effect_convert2img');
}
public function getParams()
{
$notSupported = [];
if (!class_exists(Imagick::class) && '' == self::getConvertPath()) {
$notSupported[] = '<strong>' . rex_i18n::msg('media_manager_effect_convert2img_noimagemagick') . '</strong> ';
}
if (!$this->isFfmpegAvailable()) {
$notSupported[] = '<strong>' . rex_i18n::msg('media_manager_effect_convert2img_videoconverternotfound') . '</strong> ';
}
return [
[
'label' => rex_i18n::msg('media_manager_effect_convert2img_convertto'),
'name' => 'convert_to',
'type' => 'select',
'options' => self::CONVERT_TOS,
'default' => self::CONVERT_TO_DEFAULT,
'prefix' => implode('<br>', $notSupported),
'notice' => rex_i18n::msg('media_manager_effect_convert2img_convertto_notice'),
],
[
'label' => rex_i18n::msg('media_manager_effect_convert2img_density'),
'name' => 'density',
'type' => 'select',
'options' => self::DENSITIES,
'default' => self::DENSITY_DEFAULT,
'notice' => rex_i18n::msg('media_manager_effect_convert2img_density_notice'),
],
[
'label' => rex_i18n::msg('media_manager_effect_convert2img_color'),
'name' => 'color',
'type' => 'int',
'notice' => rex_i18n::msg('media_manager_effect_convert2img_color_notice'),
],
];
}
/**
* @return string
*/
private function getConvertPath()
{
$path = '';
if (function_exists('exec')) {
$out = [];
$cmd = 'command -v convert || which convert';
exec($cmd, $out, $ret);
if (0 === $ret) {
$path = (string) $out[0];
}
}
return $path;
}
private function isVideoToImageConversionSupported(): bool
{
$inputFile = $this->media->getMediaPath();
if (null === $inputFile) {
return false;
}
$inputExt = pathinfo($inputFile, PATHINFO_EXTENSION);
if ($this->isFfmpegAvailable()) {
return in_array($inputExt, self::VIDEO_TO_IMAGE_TYPES);
}
return false;
}
private function isFfmpegAvailable(): bool
{
if (!function_exists('exec')) {
return false;
}
$ffmpegPath = 'ffmpeg'; // change to full path if necessary
$output = [];
$returnVar = -1;
exec($ffmpegPath . ' -version', $output, $returnVar);
return 0 === $returnVar;
}
}