Browse Source

abstract filter classes; black and white and grayscale filters

develop
Robin Thoni 8 years ago
parent
commit
3c777671f4

+ 71
- 0
app/Http/Business/EffectsBusiness.php View File

@@ -0,0 +1,71 @@
1
+<?php
2
+
3
+namespace App\Http\Business;
4
+
5
+use App\Http\Business\Filters\AbstractFilter;
6
+use App\Http\DBO\EffectApplyDbo;
7
+use Exception;
8
+use ImagickPixel;
9
+use Imagine\Image\Point;
10
+use Luticate\Utils\LuBusiness;
11
+use App\Http\DataAccess\EffectsDataAccess;
12
+
13
+class EffectsBusiness extends LuBusiness {
14
+    protected static function getDataAccess()
15
+    {
16
+        return new EffectsDataAccess();
17
+    }
18
+
19
+    public static function apply(EffectApplyDbo $data)
20
+    {
21
+        if (!preg_match('/^[a-zA-Z]+$/', $data->getEffect())) {
22
+            self::badInput("Invalid effect");
23
+        }
24
+        $className = 'App\Http\Business\Filters\\' . $data->getEffect() . 'Filter';
25
+
26
+        if (!class_exists($className)) {
27
+            self::notFound("Effect does not exist");
28
+        }
29
+
30
+        /**
31
+         * @var $filter AbstractFilter
32
+         */
33
+        $filter = new $className();
34
+        $filter->apply($data);
35
+
36
+        return $data;
37
+    }
38
+
39
+    public static function effectBlackAndWhite(EffectApplyDbo $data) {
40
+        $image = $data->getImage();
41
+        $iterator = $image->getPixelIterator();
42
+        foreach ($iterator as $row => $pixels) {
43
+            /**
44
+             * @var $pixel ImagickPixel
45
+             */
46
+            foreach ( $pixels as $col => $pixel ) {
47
+                $color = $pixel->getColor();
48
+                $g = 0.299 * $color["r"] + 0.587 * $color["g"] + 0.114 * $color["b"];
49
+                $g = ($g < 127 ? 0 : 255);
50
+                $pixel->setColor("rgb($g, $g, $g)");
51
+            }
52
+            $iterator->syncIterator();
53
+        }
54
+    }
55
+
56
+    public static function effectGrayScale(EffectApplyDbo $data) {
57
+        $image = $data->getImage();
58
+        $iterator = $image->getPixelIterator();
59
+        foreach ($iterator as $row => $pixels) {
60
+            /**
61
+             * @var $pixel ImagickPixel
62
+             */
63
+            foreach ( $pixels as $col => $pixel ) {
64
+                $color = $pixel->getColor();
65
+                $g = 0.299 * $color["r"] + 0.587 * $color["g"] + 0.114 * $color["b"];
66
+                $pixel->setColor("rgb($g, $g, $g)");
67
+            }
68
+            $iterator->syncIterator();
69
+        }
70
+    }
71
+}

+ 19
- 0
app/Http/Business/Filters/AbstractFilter.php View File

@@ -0,0 +1,19 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 4/22/16
6
+ * Time: 7:25 PM
7
+ */
8
+
9
+namespace App\Http\Business\Filters;
10
+
11
+use App\Http\DBO\EffectApplyDbo;
12
+
13
+abstract class AbstractFilter
14
+{
15
+    /**
16
+     * @param $data EffectApplyDbo
17
+     */
18
+    public abstract function apply($data);
19
+}

+ 38
- 0
app/Http/Business/Filters/AbstractSimpleFilter.php View File

@@ -0,0 +1,38 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 4/22/16
6
+ * Time: 7:40 PM
7
+ */
8
+
9
+namespace App\Http\Business\Filters;
10
+
11
+use App\Http\DBO\EffectApplyDbo;
12
+use ImagickPixel;
13
+
14
+abstract class AbstractSimpleFilter extends AbstractFilter
15
+{
16
+    /**
17
+     * @param EffectApplyDbo $data
18
+     */
19
+    public function apply($data)
20
+    {
21
+        $image = $data->getImage();
22
+        $iterator = $image->getPixelIterator();
23
+        foreach ($iterator as $row => $pixels) {
24
+            /**
25
+             * @var $pixel ImagickPixel
26
+             */
27
+            foreach ($pixels as $col => $pixel) {
28
+                $this->applyPixel($pixel);
29
+            }
30
+            $iterator->syncIterator();
31
+        }
32
+    }
33
+
34
+    /**
35
+     * @param $pixel ImagickPixel
36
+     */
37
+    protected abstract function applyPixel($pixel);
38
+}

+ 20
- 0
app/Http/Business/Filters/BlackAndWhiteFilter.php View File

@@ -0,0 +1,20 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 4/22/16
6
+ * Time: 7:57 PM
7
+ */
8
+
9
+namespace App\Http\Business\Filters;
10
+
11
+
12
+class BlackAndWhiteFilter extends AbstractSimpleFilter
13
+{
14
+    protected function applyPixel($pixel)
15
+    {
16
+        $g = GrayScaleFilter::toGrayScale($pixel);
17
+        $g = ($g < 127 ? 0 : 255);
18
+        $pixel->setColor("rgb($g, $g, $g)");
19
+    }
20
+}

+ 33
- 0
app/Http/Business/Filters/GrayScaleFilter.php View File

@@ -0,0 +1,33 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 4/22/16
6
+ * Time: 7:58 PM
7
+ */
8
+
9
+namespace App\Http\Business\Filters;
10
+
11
+use ImagickPixel;
12
+
13
+class GrayScaleFilter extends AbstractSimpleFilter
14
+{
15
+    /**
16
+     * @param $pixel ImagickPixel
17
+     * @return int
18
+     */
19
+    public static function toGrayScale($pixel)
20
+    {
21
+        $color = $pixel->getColor();
22
+        return 0.299 * $color["r"] + 0.587 * $color["g"] + 0.114 * $color["b"];
23
+    }
24
+
25
+    /**
26
+     * @param ImagickPixel $pixel
27
+     */
28
+    protected function applyPixel($pixel)
29
+    {
30
+        $g = self::toGrayScale($pixel);
31
+        $pixel->setColor("rgb($g, $g, $g)");
32
+    }
33
+}

+ 14
- 0
app/Http/Business/ImagesBusiness.php View File

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\Business;
4
+
5
+use Luticate\Utils\LuBusiness;
6
+use App\Http\DataAccess\ImagesDataAccess;
7
+use App\Http\DBO\ImagesDbo;
8
+
9
+class ImagesBusiness extends LuBusiness {
10
+    protected static function getDataAccess()
11
+    {
12
+        return new ImagesDataAccess();
13
+    }
14
+}

+ 20
- 0
app/Http/Controller/EffectsController.php View File

@@ -0,0 +1,20 @@
1
+<?php
2
+
3
+namespace App\Http\Controller;
4
+
5
+use App\Http\DBO\EffectApplyDbo;
6
+use Luticate\Utils\LuController;
7
+use App\Http\Business\EffectsBusiness;
8
+
9
+class EffectsController extends LuController {
10
+    protected function getBusiness()
11
+    {
12
+        return new EffectsBusiness();
13
+    }
14
+
15
+    public function apply($effect, EffectApplyDbo $data)
16
+    {
17
+        $data->setEffect($effect);
18
+        return EffectsBusiness::apply($data);
19
+    }
20
+}

+ 14
- 0
app/Http/Controller/ImagesController.php View File

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\Controller;
4
+
5
+use Luticate\Utils\LuController;
6
+use App\Http\Business\ImagesBusiness;
7
+use App\Http\DBO\ImagesDbo;
8
+
9
+class ImagesController extends LuController {
10
+    protected function getBusiness()
11
+    {
12
+        return new ImagesBusiness();
13
+    }
14
+}

+ 104
- 0
app/Http/DBO/EffectApplyDbo.php View File

@@ -0,0 +1,104 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 4/22/16
6
+ * Time: 4:57 PM
7
+ */
8
+
9
+namespace App\Http\DBO;
10
+
11
+use Imagick;
12
+use Luticate\Utils\LuDbo;
13
+
14
+class EffectApplyDbo extends LuDbo
15
+{
16
+    /**
17
+     * @var Imagick
18
+     */
19
+    private $image;
20
+
21
+    /**
22
+     * @var string
23
+     */
24
+    private $effect;
25
+
26
+    /**
27
+     * @var array
28
+     */
29
+    private $data;
30
+
31
+    public static function jsonDeserialize($json)
32
+    {
33
+        $dbo = new EffectApplyDbo();
34
+        
35
+        $imagine = new Imagick();
36
+        $imagine->readImageBlob(base64_decode($json["image"]));
37
+        $dbo->setImage($imagine);
38
+        $dbo->setEffect($json["effect"]);
39
+        $dbo->setData($json["data"]);
40
+        
41
+        return $dbo;
42
+    }
43
+
44
+    function jsonSerialize()
45
+    {
46
+        $imageData = null;
47
+        if (!is_null($this->image)) {
48
+            $imageData = base64_encode($this->image->getImageBlob());
49
+        }
50
+        return [
51
+            "image" => $imageData,
52
+            "effect" => $this->effect,
53
+            "data" => $this->data
54
+        ];
55
+    }
56
+    
57
+    /**
58
+     * @return Imagick
59
+     */
60
+    public function getImage()
61
+    {
62
+        return $this->image;
63
+    }
64
+
65
+    /**
66
+     * @param Imagick $image
67
+     */
68
+    public function setImage($image)
69
+    {
70
+        $this->image = $image;
71
+    }
72
+
73
+    /**
74
+     * @return string
75
+     */
76
+    public function getEffect()
77
+    {
78
+        return $this->effect;
79
+    }
80
+
81
+    /**
82
+     * @param string $effect
83
+     */
84
+    public function setEffect($effect)
85
+    {
86
+        $this->effect = $effect;
87
+    }
88
+
89
+    /**
90
+     * @return array
91
+     */
92
+    public function getData()
93
+    {
94
+        return $this->data;
95
+    }
96
+
97
+    /**
98
+     * @param array $data
99
+     */
100
+    public function setData($data)
101
+    {
102
+        $this->data = $data;
103
+    }
104
+}

+ 85
- 0
app/Http/DBO/ImagesDbo.php View File

@@ -0,0 +1,85 @@
1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ */
7
+
8
+namespace App\Http\DBO;
9
+
10
+use Luticate\Utils\LuDbo;
11
+
12
+class ImagesDbo extends LuDbo {
13
+
14
+    public function jsonSerialize()
15
+    {
16
+        return array(
17
+            "Id" => $this->_id,
18
+            "UserId" => $this->_userId,
19
+            "Path" => $this->_path
20
+        );
21
+    }
22
+
23
+    public static function jsonDeserialize($json)
24
+    {
25
+        $dbo = new ImagesDbo();
26
+        if (isset($json["Id"])) {
27
+            $dbo->setId($json["Id"]);
28
+        }
29
+        if (isset($json["UserId"])) {
30
+            $dbo->setUserId($json["UserId"]);
31
+        }
32
+        if (isset($json["Path"])) {
33
+            $dbo->setPath($json["Path"]);
34
+        }
35
+        return $dbo;
36
+    }
37
+
38
+    public static function generateSample()
39
+    {
40
+        $dbo = new ImagesDbo();
41
+        $dbo->setId(42);
42
+        $dbo->setUserId(42);
43
+        $dbo->setPath("sample string");
44
+        return $dbo;
45
+    }
46
+
47
+    /**
48
+     * @var integer
49
+     */
50
+    protected $_id;
51
+    public function getId()
52
+    {
53
+        return $this->_id;
54
+    }
55
+    public function setId($value)
56
+    {
57
+        $this->_id = $value;
58
+    }
59
+
60
+    /**
61
+     * @var integer
62
+     */
63
+    protected $_userId;
64
+    public function getUserId()
65
+    {
66
+        return $this->_userId;
67
+    }
68
+    public function setUserId($value)
69
+    {
70
+        $this->_userId = $value;
71
+    }
72
+
73
+    /**
74
+     * @var text
75
+     */
76
+    protected $_path;
77
+    public function getPath()
78
+    {
79
+        return $this->_path;
80
+    }
81
+    public function setPath($value)
82
+    {
83
+        $this->_path = $value;
84
+    }
85
+}

+ 50
- 0
app/Http/DBO/ImagesDboArray.php View File

@@ -0,0 +1,50 @@
1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ */
7
+
8
+namespace App\Http\DBO;
9
+
10
+use Luticate\Utils\LuDbo;
11
+
12
+class ImagesDboArray extends LuDbo {
13
+
14
+    /**
15
+    * @var ImagesDbo[]
16
+    */
17
+    protected $_array;
18
+    public function getArray()
19
+    {
20
+        return $this->_array;
21
+    }
22
+    public function setArray($value)
23
+    {
24
+        $this->_array = $value;
25
+    }
26
+
27
+    public function jsonSerialize()
28
+    {
29
+        return $this->_array;
30
+    }
31
+
32
+    public static function jsonDeserialize($json)
33
+    {
34
+        $dbo = new ImagesDboArray();
35
+        $array = [];
36
+        foreach ($json as $data) {
37
+            $array[] = ImagesDbo::jsonDeserialize($data);
38
+        }
39
+        $dbo->setArray($array);
40
+        return $dbo;
41
+    }
42
+
43
+    public static function generateSample()
44
+    {
45
+        return [
46
+            ImagesDbo::generateSample(),
47
+            ImagesDbo::generateSample()
48
+        ];
49
+    }
50
+}

+ 14
- 0
app/Http/DataAccess/EffectsDataAccess.php View File

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess;
4
+
5
+use Luticate\Utils\LuDataAccess;
6
+use App\Http\DataAccess\Models\Effects;
7
+use App\Http\DBO\EffectsDbo;
8
+
9
+class EffectsDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new Effects();
13
+    }
14
+}

+ 14
- 0
app/Http/DataAccess/ImagesDataAccess.php View File

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess;
4
+
5
+use Luticate\Utils\LuDataAccess;
6
+use App\Http\DataAccess\Models\Images;
7
+use App\Http\DBO\ImagesDbo;
8
+
9
+class ImagesDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new Images();
13
+    }
14
+}

+ 9
- 0
app/Http/DataAccess/Models/Images.php View File

@@ -0,0 +1,9 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess\Models;
4
+
5
+use App\Http\DBO\ImagesDbo;
6
+
7
+class Images extends ImagesModel
8
+{
9
+}

+ 51
- 0
app/Http/DataAccess/Models/ImagesModel.php View File

@@ -0,0 +1,51 @@
1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ * DO NOT DIRECTLY USE THIS FILE
7
+ * USE Images.php
8
+ * TO MAKE YOUR CHANGES AND DATABASE ACCESS
9
+*/
10
+
11
+namespace App\Http\DataAccess\Models;
12
+
13
+use Luticate\Utils\LuModel;
14
+use App\Http\DBO\ImagesDbo;
15
+
16
+class ImagesModel extends LuModel
17
+{
18
+    function __construct()
19
+    {
20
+        parent::__construct();
21
+        $this->timestamps = false;
22
+    }
23
+
24
+    public function toDbo()
25
+    {
26
+        $dbo = new ImagesDbo();
27
+
28
+        $dbo->setId($this->id);
29
+        $dbo->setUserId($this->user_id);
30
+        $dbo->setPath($this->path);
31
+
32
+        return $dbo;
33
+    }
34
+
35
+    /**
36
+     * @param $dbo ImagesDbo
37
+     * @param $model LuModel|null
38
+     * @return Images
39
+     */
40
+    public function fromDbo($dbo, $model = null)
41
+    {
42
+        if (is_null($model))
43
+            $model = new Images();
44
+
45
+        $model->id = $dbo->getId();
46
+        $model->user_id = $dbo->getUserId();
47
+        $model->path = $dbo->getPath();
48
+
49
+        return $model;
50
+    }
51
+}

+ 1
- 1
app/Http/routes.php View File

@@ -12,4 +12,4 @@ LuticateBusiness::setupAuth();
12 12
 LuticateBusiness::setupRoutes("/api/luticate");
13 13
 LuDocBusiness::setupRoutes("/api/luticate");
14 14
 
15
-//$route->get("/data", "Data", "getAll", ProjectPermissions::HOST_GET);
15
+$route->post("/api/effects/{effect}/apply", "Effects", "apply");

+ 2
- 1
composer.json View File

@@ -21,7 +21,8 @@
21 21
         "php": ">=5.5.9",
22 22
         "laravel/lumen-framework": "5.1.*",
23 23
         "vlucas/phpdotenv": "~1.0",
24
-        "luticate/auth": "0.1.x"
24
+        "luticate/auth": "0.1.x",
25
+        "imagine/imagine": "^0.6.3"
25 26
     },
26 27
     "require-dev": {
27 28
         "phpunit/phpunit": "~4.0",

Loading…
Cancel
Save