1 <?php
  2 
  3 /**
  4  * Schneidet einen Ausschnitt aus einem Bild heraus. Es wird dabei nicht skaliert.
  5  *
  6  * @author staabm
  7  *
  8  * @package redaxo\media-manager
  9  */
 10 
 11 class rex_effect_crop extends rex_effect_abstract
 12 {
 13     public function execute()
 14     {
 15         $this->media->asImage();
 16 
 17         $gdimage = $this->media->getImage();
 18         $w = $this->media->getWidth();
 19         $h = $this->media->getHeight();
 20 
 21         if (empty($this->params['width']) || $this->params['width'] < 0 ||
 22             empty($this->params['height']) || $this->params['height'] < 0
 23         ) {
 24             return;
 25         }
 26 
 27         // das original-bild ist kleiner als das zu croppende format
 28         if ($this->params['width'] > $w && $this->params['height'] > $h) {
 29             return;
 30         }
 31 
 32         $offset_width = 0;
 33         $offset_height = 0;
 34         if (empty($this->params['offset_width'])) {
 35             $this->params['offset_width'] = 0;
 36         }
 37         if (empty($this->params['offset_height'])) {
 38             $this->params['offset_height'] = 0;
 39         }
 40 
 41         $cropW = min($this->params['width'], $w);
 42         $cropH = min($this->params['height'], $h);
 43 
 44         switch ($this->params['vpos']) {
 45             case 'top':
 46                 $offset_height += $this->params['offset_height'];
 47                 break;
 48             case 'bottom':
 49                 $offset_height = (int) (($h - $cropH)) + $this->params['offset_height'];
 50                 break;
 51             case 'middle':
 52             default: // center
 53                 $offset_height = (int) (($h - $cropH) / 2) + $this->params['offset_height'];
 54                 break;
 55         }
 56 
 57         switch ($this->params['hpos']) {
 58             case 'left':
 59                 $offset_width += $this->params['offset_width'];
 60                 break;
 61             case 'right':
 62                 $offset_width = (int) ($w - $cropW) + $this->params['offset_width'];
 63                 break;
 64             case 'center':
 65             default: // center
 66                 $offset_width = (int) (($w - $cropW) / 2) + $this->params['offset_width'];
 67                 break;
 68         }
 69 
 70         // create cropped image
 71         if (function_exists('ImageCreateTrueColor')) {
 72             $des = @imagecreatetruecolor($cropW, $cropH);
 73         } else {
 74             $des = @imagecreate($cropW, $cropH);
 75         }
 76 
 77         if (!$des) {
 78             return;
 79         }
 80 
 81         // Transparenz erhalten
 82         $this->keepTransparent($des);
 83         imagecopyresampled($des, $gdimage, 0, 0, $offset_width, $offset_height, $cropW, $cropH, $cropW, $cropH);
 84 
 85         $this->media->setImage($des);
 86         $this->media->refreshImageDimensions();
 87     }
 88 
 89     public function getName()
 90     {
 91         return rex_i18n::msg('media_manager_effect_crop');
 92     }
 93 
 94     public function getParams()
 95     {
 96         return [
 97             [
 98                 'label' => rex_i18n::msg('media_manager_effect_crop_width'),
 99                 'name' => 'width',
100                 'type' => 'int',
101             ],
102             [
103                 'label' => rex_i18n::msg('media_manager_effect_crop_height'),
104                 'name' => 'height',
105                 'type' => 'int',
106             ],
107             [
108                 'label' => rex_i18n::msg('media_manager_effect_crop_offset_width'),
109                 'name' => 'offset_width',
110                 'type' => 'int',
111             ],
112             [
113                 'label' => rex_i18n::msg('media_manager_effect_crop_offset_height'),
114                 'name' => 'offset_height',
115                 'type' => 'int',
116             ],
117             [
118                 'label' => rex_i18n::msg('media_manager_effect_brand_hpos'),
119                 'name' => 'hpos',
120                 'type' => 'select',
121                 'options' => ['left', 'center', 'right'],
122                 'default' => 'center',
123             ],
124             [
125                 'label' => rex_i18n::msg('media_manager_effect_brand_vpos'),
126                 'name' => 'vpos',
127                 'type' => 'select',
128                 'options' => ['top', 'middle', 'bottom'],
129                 'default' => 'middle',
130             ],
131         ];
132     }
133 }
134