1 <?php
  2 
  3 /**
  4  * Branded ein Bild mit einem Wasserzeichen.
  5  *
  6  * @package redaxo\media-manager
  7  */
  8 class rex_effect_insert_image extends rex_effect_abstract
  9 {
 10     public function execute()
 11     {
 12         $this->media->asImage();
 13 
 14         // -------------------------------------- CONFIG
 15         $brandimage = rex_path::media($this->params['brandimage']);
 16         if (!file_exists($brandimage) || !is_file($brandimage)) {
 17             return;
 18         }
 19 
 20         // Abstand vom Rand
 21         $padding_x = -10;
 22         if (isset($this->params['padding_x'])) {
 23             $padding_x = (int) $this->params['padding_x'];
 24         }
 25 
 26         $padding_y = -10;
 27         if (isset($this->params['padding_y'])) {
 28             $padding_y = (int) $this->params['padding_y'];
 29         }
 30 
 31         // horizontale ausrichtung: left/center/right
 32         $hpos = 'right';
 33         if (isset($this->params['hpos'])) {
 34             $hpos = (string) $this->params['hpos'];
 35         }
 36 
 37         // vertikale ausrichtung:   top/center/bottom
 38         $vpos = 'bottom';
 39         if (isset($this->params['vpos'])) {
 40             $vpos = (string) $this->params['vpos'];
 41         }
 42 
 43         // -------------------------------------- /CONFIG
 44         $brand = new rex_managed_media($brandimage);
 45         $brand->asImage();
 46         $gdbrand = $brand->getImage();
 47         $gdimage = $this->media->getImage();
 48 
 49         $image_width = $this->media->getWidth();
 50         $image_height = $this->media->getHeight();
 51         $brand_width = $brand->getWidth();
 52         $brand_height = $brand->getHeight();
 53 
 54         switch ($hpos) {
 55             case 'left':
 56                 $dstX = 0;
 57                 break;
 58             case 'center':
 59                 $dstX = (int) (($image_width - $brand_width) / 2);
 60                 break;
 61             case 'right':
 62             default:
 63                 $dstX = $image_width - $brand_width;
 64         }
 65 
 66         switch ($vpos) {
 67             case 'top':
 68                 $dstY = 0;
 69                 break;
 70             case 'middle':
 71                 $dstY = (int) (($image_height - $brand_height) / 2);
 72                 break;
 73             case 'bottom':
 74             default:
 75                 $dstY = $image_height - $brand_height;
 76         }
 77 
 78         imagealphablending($gdimage, true);
 79         imagecopy($gdimage, $gdbrand, $dstX + $padding_x, $dstY + $padding_y, 0, 0, $brand_width, $brand_height);
 80 
 81         $this->media->setImage($gdimage);
 82     }
 83 
 84     public function getName()
 85     {
 86         return rex_i18n::msg('media_manager_effect_insert_image');
 87     }
 88 
 89     public function getParams()
 90     {
 91         return [
 92             [
 93                 'label' => rex_i18n::msg('media_manager_effect_brand_image'),
 94                 'name' => 'brandimage',
 95                 'type' => 'media',
 96                 'default' => '',
 97             ],
 98             [
 99                 'label' => rex_i18n::msg('media_manager_effect_brand_hpos'),
100                 'name' => 'hpos',
101                 'type' => 'select',
102                 'options' => ['left', 'center', 'right'],
103                 'default' => 'left',
104             ],
105             [
106                 'label' => rex_i18n::msg('media_manager_effect_brand_vpos'),
107                 'name' => 'vpos',
108                 'type' => 'select',
109                 'options' => ['top', 'middle', 'bottom'],
110                 'default' => 'top',
111             ],
112             [
113                 'label' => rex_i18n::msg('media_manager_effect_brand_padding_x'),
114                 'name' => 'padding_x',
115                 'type' => 'int',
116                 'default' => '-10',
117             ],
118             [
119                 'label' => rex_i18n::msg('media_manager_effect_brand_padding_y'),
120                 'name' => 'padding_y',
121                 'type' => 'int',
122                 'default' => '-10',
123             ],
124         ];
125     }
126 }
127