1 <?php
2
3 4 5 6 7 8 9
10
11 class rex_effect_convert2img extends rex_effect_abstract
12 {
13 private static $convert_types = [
14 'pdf',
15 'ps',
16 'psd',
17 'tif',
18 'tiff',
19 'bmp',
20 'eps',
21 'ico',
22
23 ];
24 private static $convert_to = [
25 'jpg' => [
26 'ext' => 'jpg',
27 'content-type' => 'image/jpeg',
28 ],
29 'png' => [
30 'ext' => 'png',
31 'content-type' => 'image/png',
32 ],
33 ];
34 private static $densities = [100, 150, 200, 300, 600];
35 private static $density_default = 150;
36 private static $convert_tos = ['jpg', 'png'];
37 private static $convert_to_default = 'jpg';
38
39 public function execute()
40 {
41 if (!isset(self::$convert_to[$this->params['convert_to']])) {
42 $convert_to = self::$convert_to[self::$convert_to_default];
43 } else {
44 $convert_to = self::$convert_to[$this->params['convert_to']];
45 }
46
47 $density = (int) $this->params['density'];
48
49 if (!in_array($density, self::$densities)) {
50 $density = self::$density_default;
51 }
52
53 $from_path = realpath($this->media->getMediaPath());
54 $ext = rex_file::extension($from_path);
55
56 if (!$ext) {
57 return;
58 }
59
60 if (!in_array(strtolower($ext), self::$convert_types)) {
61 return;
62 }
63
64 $convert_path = self::getConvertPath();
65
66 if ($convert_path == '') {
67 return;
68 }
69
70 $filename = $this->media->getMediaFilename();
71 $filename_wo_ext = substr($filename, 0, (strlen($filename) - strlen($ext)));
72
73 $to_path = rex_path::addonCache('media_manager', 'media_manager__convert2img_' . md5($this->media->getMediaPath()) . '_' . $filename_wo_ext . $convert_to['ext']);
74
75 $cmd = $convert_path . ' -density '.$density.' "' . $from_path . '[0]" -colorspace RGB "' . $to_path . '"';
76
77 exec($cmd, $out, $ret);
78
79 if ($ret != 0) {
80 return false;
81 }
82
83 $this->media->setSourcePath($to_path);
84 $this->media->refreshImageDimensions();
85 $this->media->setFormat($convert_to['ext']);
86 $this->media->setMediaFilename($filename);
87 $this->media->setHeader('Content-Type', $convert_to['content-type']);
88
89 register_shutdown_function(function () use ($to_path) {
90 rex_file::delete($to_path);
91 });
92 }
93
94 public function getName()
95 {
96 return rex_i18n::msg('media_manager_effect_convert2img');
97 }
98
99 public function getParams()
100 {
101 return [
102 [
103 'label' => rex_i18n::msg('media_manager_effect_convert2img_convertto'),
104 'name' => 'convert_to',
105 'type' => 'select',
106 'options' => self::$convert_tos,
107 'default' => self::$convert_to_default,
108 ],
109 [
110 'label' => rex_i18n::msg('media_manager_effect_convert2img_density'),
111 'name' => 'density',
112 'type' => 'select',
113 'options' => self::$densities,
114 'default' => self::$density_default,
115 ],
116 ];
117 }
118
119 private function getConvertPath()
120 {
121 $path = '';
122
123 if (function_exists('exec')) {
124 $out = [];
125 $cmd = 'command -v convert || which convert';
126 exec($cmd, $out, $ret);
127
128 if ($ret === 0) {
129 $path = $out[0];
130 }
131 }
132 return $path;
133 }
134 }
135