1 <?php
 2 
 3 class rex_effect_filter_colorize extends rex_effect_abstract
 4 {
 5     public function execute()
 6     {
 7         $this->params['filter_r'] = (int) $this->params['filter_r'];
 8         if ($this->params['filter_r'] < 0) {
 9             return;
10         }
11         $this->params['filter_g'] = (int) $this->params['filter_g'];
12         if ($this->params['filter_g'] < 0) {
13             return;
14         }
15         $this->params['filter_b'] = (int) $this->params['filter_b'];
16         if ($this->params['filter_b'] < 0) {
17             return;
18         }
19 
20         $this->media->asImage();
21         $img = $this->media->getImage();
22 
23         if (!($t = imagecolorstotal($img))) {
24             $t = 256;
25             imagetruecolortopalette($img, true, $t);
26         }
27         $imagex = imagesx($img);
28         $imagey = imagesy($img);
29 
30         $gdimage = $this->media->getImage();
31         $w = $this->media->getWidth();
32         $h = $this->media->getHeight();
33 
34         $src_x = ceil($w);
35         $src_y = ceil($h);
36         $dst_x = $src_x;
37         $dst_y = $src_y;
38         $dst_im = imagecreatetruecolor($dst_x, $dst_y);
39 
40         imagecopyresampled($dst_im, $gdimage, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y);
41         for ($y = 0; $y < $src_y; ++$y) {
42             for ($x = 0; $x < $src_x; ++$x) {
43                 $rgb = imagecolorat($dst_im, $x, $y);
44                 $TabColors = imagecolorsforindex($dst_im, $rgb);
45                 $color_r = floor($TabColors['red'] * $this->params['filter_r'] / 255);
46                 $color_g = floor($TabColors['green'] * $this->params['filter_g'] / 255);
47                 $color_b = floor($TabColors['blue'] * $this->params['filter_b'] / 255);
48                 $newcol = imagecolorallocate($dst_im, $color_r, $color_g, $color_b);
49                 imagesetpixel($dst_im, $x, $y, $newcol);
50             }
51         }
52         $this->media->setImage($dst_im);
53     }
54 
55     public function getName()
56     {
57         return rex_i18n::msg('media_manager_effect_colorize');
58     }
59 
60     public function getParams()
61     {
62         return [
63             [
64                 'label' => rex_i18n::msg('media_manager_effect_colorize_r'),
65                 'name' => 'filter_r',
66                 'type' => 'int',
67                 'default' => '',
68             ],
69             [
70                 'label' => rex_i18n::msg('media_manager_effect_colorize_g'),
71                 'name' => 'filter_g',
72                 'type' => 'int',
73                 'default' => '',
74             ],
75             [
76                 'label' => rex_i18n::msg('media_manager_effect_colorize_b'),
77                 'name' => 'filter_b',
78                 'type' => 'int',
79                 'default' => '',
80             ],
81         ];
82     }
83 }
84