1 <?php
  2 
  3 /**
  4  * @package redaxo\media-manager
  5  */
  6 class rex_effect_workspace extends rex_effect_abstract
  7 {
  8     private $options;
  9     private $script;
 10 
 11     public function __construct()
 12     {
 13         $this->options = [
 14             'top',
 15             'topleft',
 16             'left',
 17             'bottomleft',
 18             'bottom',
 19             'bottomright',
 20             'right',
 21             'topright',
 22             'center',
 23         ];
 24 
 25         $this->script = '
 26 <script type="text/javascript">
 27 <!--
 28 
 29 $(function() {
 30     var $fx_workspace_select_trans = $("#media-manager-rex-effect-workspace-set-transparent-select");
 31     var $fx_workspace_bg_r = $("#media-manager-rex-effect-workspace-bg-r-text").parent().parent();
 32     var $fx_workspace_bg_g = $("#media-manager-rex-effect-workspace-bg-g-text").parent().parent();
 33     var $fx_workspace_bg_b = $("#media-manager-rex-effect-workspace-bg-b-text").parent().parent();
 34 
 35     $fx_workspace_select_trans.change(function(){
 36         if(jQuery(this).val() != "colored")
 37         {
 38             $fx_workspace_bg_r.hide();
 39             $fx_workspace_bg_g.hide();
 40             $fx_workspace_bg_b.hide();
 41         }else
 42         {
 43             $fx_workspace_bg_r.show();
 44             $fx_workspace_bg_g.show();
 45             $fx_workspace_bg_b.show();
 46         }
 47     }).change();
 48 });
 49 
 50 //--></script>';
 51     }
 52 
 53     public function execute()
 54     {
 55         $this->media->asImage();
 56 
 57         $gdimage = $this->media->getImage();
 58         $w = $this->media->getWidth();
 59         $h = $this->media->getHeight();
 60 
 61         $this->params['width'] = (int) $this->params['width'];
 62         if ($this->params['width'] <= 0) {
 63             $this->params['width'] = $w;
 64         }
 65 
 66         $this->params['height'] = (int) $this->params['height'];
 67         if ($this->params['height'] <= 0) {
 68             $this->params['height'] = $h;
 69         }
 70 
 71         $this->params['bg_r'] = (int) $this->params['bg_r'];
 72         if (!isset($this->params['bg_r']) || $this->params['bg_r'] > 255 || $this->params['bg_r'] < 0) {
 73             $this->params['bg_r'] = 255;
 74         }
 75 
 76         $this->params['bg_g'] = (int) $this->params['bg_g'];
 77         if (!isset($this->params['bg_g']) || $this->params['bg_g'] > 255 || $this->params['bg_g'] < 0) {
 78             $this->params['bg_g'] = 255;
 79         }
 80 
 81         $this->params['bg_b'] = (int) $this->params['bg_b'];
 82         if (!isset($this->params['bg_b']) || $this->params['bg_b'] > 255 || $this->params['bg_b'] < 0) {
 83             $this->params['bg_b'] = 255;
 84         }
 85 
 86         $trans = false;
 87         if ($this->params['set_transparent'] != 'colored') {
 88             if ($this->media->getFormat() != 'gif' && $this->media->getFormat() != 'png' && $this->media->getFormat() != 'webp') {
 89                 $this->media->setFormat('png');
 90             }
 91             $trans = true;
 92         }
 93 
 94         $workspace = imagecreatetruecolor($this->params['width'], $this->params['height']);
 95         if ($trans) {
 96             $transparent = imagecolorallocatealpha($workspace, 0, 0, 0, 127);
 97             imagefill($workspace, 0, 0, $transparent);
 98             $this->keepTransparent($workspace);
 99         } else {
100             imagefill($workspace, 0, 0, imagecolorallocate($workspace, $this->params['bg_r'], $this->params['bg_g'], $this->params['bg_b']));
101         }
102 
103         $src_w = $w;
104         $src_h = $h;
105         $dst_x = 0;
106         $dst_y = 0;
107         $src_x = 0;
108         $src_y = 0;
109 
110         switch ($this->params['vpos']) {
111             case 'top':
112                 break;
113             case 'bottom':
114                 $dst_y = (int) $this->params['height'] - $h;
115                 break;
116             case 'middle':
117             default: // center
118                 $dst_y = (int) ($this->params['height'] / 2) - ($h / 2);
119                 break;
120         }
121 
122         switch ($this->params['hpos']) {
123             case 'left':
124                 break;
125             case 'right':
126                 $dst_x = (int) $this->params['width'] - $w;
127                 break;
128             case 'center':
129             default: // center
130                 $dst_x = (int) ($this->params['width'] / 2) - ($w / 2);
131                 break;
132         }
133 
134         imagecopy($workspace, $gdimage, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
135         $this->media->setImage($workspace);
136         $this->media->refreshImageDimensions();
137 
138         // Transparenz erhalten
139         /*
140         $this->keepTransparent($des);
141         imagecopyresampled($des, $gdimage, 0, 0, 0, 0, $this->params['width'], $this->params['height'], $w, $h);
142 
143         $gdimage = $des;
144         $this->image->refreshDimensions();
145         */
146     }
147 
148     public function getName()
149     {
150         return rex_i18n::msg('media_manager_effect_workspace');
151     }
152 
153     public function getParams()
154     {
155         return [
156             [
157                 'label' => rex_i18n::msg('media_manager_effect_resize_width'),
158                 'name' => 'width',
159                 'type' => 'int',
160             ],
161             [
162                 'label' => rex_i18n::msg('media_manager_effect_resize_height'),
163                 'name' => 'height',
164                 'type' => 'int',
165             ],
166             [
167                 'label' => rex_i18n::msg('media_manager_effect_brand_hpos'),
168                 'name' => 'hpos',
169                 'type' => 'select',
170                 'options' => ['left', 'center', 'right'],
171                 'default' => 'left',
172             ],
173             [
174                 'label' => rex_i18n::msg('media_manager_effect_brand_vpos'),
175                 'name' => 'vpos',
176                 'type' => 'select',
177                 'options' => ['top', 'middle', 'bottom'],
178                 'default' => 'top',
179             ],
180             [
181                 'label' => rex_i18n::msg('media_manager_effect_mirror_background_color'),
182                 'name' => 'set_transparent',
183                 'type' => 'select',
184                 'options' => ['colored', 'transparent'],
185                 'default' => 'colored',
186                 'suffix' => $this->script,
187             ],
188             [
189                 'label' => rex_i18n::msg('media_manager_effect_mirror_background_r'),
190                 'name' => 'bg_r',
191                 'type' => 'int',
192             ],
193             [
194                 'label' => rex_i18n::msg('media_manager_effect_mirror_background_g'),
195                 'name' => 'bg_g',
196                 'type' => 'int',
197             ],
198             [
199                 'label' => rex_i18n::msg('media_manager_effect_mirror_background_b'),
200                 'name' => 'bg_b',
201                 'type' => 'int',
202             ],
203         ];
204     }
205 }
206