Browse Source

NEW: added new class GDSupport to Engine
UPD: bump to Engine version 1.10.00

Giancarlo 7 years ago
parent
commit
059b4a73da
4 changed files with 144 additions and 35 deletions
  1. 1 1
      cfg/config.ini
  2. 1 1
      package.json
  3. 50 33
      src/Modules/restful/restful.php
  4. 92 0
      src/OpenMind/System/GDSupport.php

+ 1 - 1
cfg/config.ini

@@ -1,5 +1,5 @@
 [globals]
 [globals]
-VERSION=1.5.1
+VERSION=1.5.2
 AUTOLOAD="src/"
 AUTOLOAD="src/"
 CACHE = TRUE
 CACHE = TRUE
 UI=ui/frontend/
 UI=ui/frontend/

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
 {
 	"name": "omlink",
 	"name": "omlink",
-	"version": "1.9.39",
+	"version": "1.10.00",
 	"codename": "Barricade",
 	"codename": "Barricade",
 	"private": true,
 	"private": true,
 	"description": "OM-Link Engine",
 	"description": "OM-Link Engine",

+ 50 - 33
src/Modules/restful/restful.php

@@ -11,6 +11,7 @@ namespace Modules\restful;
 use Base;
 use Base;
 use OpenMind\FindBox\FindBoxCoreSystem;
 use OpenMind\FindBox\FindBoxCoreSystem;
 use OpenMind\FindBox\QueueCommand;
 use OpenMind\FindBox\QueueCommand;
+use OpenMind\System\GDSupport;
 use OpenMind\System\PluginStrategy;
 use OpenMind\System\PluginStrategy;
 use OpenMind\System\QueueSystem;
 use OpenMind\System\QueueSystem;
 use Template;
 use Template;
@@ -48,39 +49,55 @@ class restful extends PluginStrategy
 	{
 	{
 		$varCondizione = 0;
 		$varCondizione = 0;
 
 
-		$grafica = [
-			'BODY' => ['w' => 100, 'h' => 100],
-			'TEXT1' => [
-				'x' => '0',
-				'y' => '18',
-				'size' => 18,
-				'color' => $varCondizione == 1 ? '#FFFFFF' : '#00FF00',
-				'value' => 'ciao'
-			],
-			'TEXT2' => [
-				'x' => '0',
-				'y' => '37',
-				'size' => 12,
-				'color' => '#FF0000',
-				'value' => 'booohh'
-			]
-		];
-
-		$image = null;
-		foreach($grafica as $element => $gdElement) {
-			switch (substr($element, 0, 4)) {
-				case 'BODY':
-					$image = imagecreatetruecolor($gdElement['w'], $gdElement['h']);
-					break;
-				case 'TEXT':
-					list($r, $g, $b) = sscanf($gdElement['color'], "#%02x%02x%02x");
-					$color = imagecolorallocate($image, $r, $g, $b);
-					imagettftext($image, $gdElement['size'], 0, $gdElement['x'], $gdElement['y'], $color, 'C:\Windows\Fonts\arial.ttf', $gdElement['value']);
-			}
-		}
-
-		imagepng($image, 'c:\ciao.png');
-		imagedestroy($image);
+		$grafica[] = ['BODY' => ['w' => 400, 'h' => 400]];
+		$grafica[] = ['FILL' => ['w' => 10, 'h' => 10, 'color' => '#0000FF']];
+		$grafica[] = ['TEXT' => [
+			'x' => '0',
+			'y' => '18',
+			'size' => 18,
+			'color' => $varCondizione == 1 ? '#FFFFFF' : '#00FF00',
+			'value' => 'ciao'
+		]];
+		$grafica[] = ['TEXT' => [
+			'x' => '0',
+			'y' => '37',
+			'size' => 12,
+			'color' => '#FF0000',
+			'value' => 'booohh'
+		]];
+
+		$grafica[] = ['RECTFILL' => [
+			'x1' => '20',
+			'y1' => '100',
+			'x2' => '120',
+			'y2' => '120',
+			'color' => '#FF0000',
+		]];
+
+		$grafica[] = ['LINE' => [
+			'x1' => '10',
+			'y1' => '10',
+			'x2' => '300',
+			'y2' => '300',
+			'color' => '#7A6E9C',
+		]];
+
+		$grafica[] = ['PIXEL' => [
+			'x' => '250',
+			'y' => '200',
+			'color' => '#0A9C05',
+		]];
+
+		$grafica[] = ['ELLIPSE' => [
+			'x' => '210',
+			'y' => '230',
+			'w' => '50',
+			'h' => '90',
+			'color' => '#9C5225',
+		]];
+
+		$gd = new GDSupport();
+		$gd->Render($grafica, 'ciao.png');
 	}
 	}
 
 
 	public function RESTful_ListAPIS($f3)
 	public function RESTful_ListAPIS($f3)

+ 92 - 0
src/OpenMind/System/GDSupport.php

@@ -0,0 +1,92 @@
+<?php
+/**
+ * @author Open Mind
+ * @date 21/09/2019
+ * @time 22:32
+ */
+
+namespace OpenMind\System;
+
+/**
+ * Class GDSupport
+ * Facility for draw images using GD php library
+ * Accept an array of elements:
+ * ['gd_function_name' => ['property_function_1' => value_function_1, 'property_function_2' => value_function_2, etc]];
+ *
+ * @package OpenMind\System
+ */
+class GDSupport
+{
+	private $image;
+
+	public function __construct()
+	{
+		$this->image = null;
+	}
+
+	/**
+	 * @param string $colorStr
+	 * @return false|int
+	 */
+	private function getColor(string $colorStr)
+	{
+		list($r, $g, $b) = sscanf($colorStr, "#%02x%02x%02x");
+		return imagecolorallocate($this->image, $r, $g, $b);
+	}
+
+	/**
+	 * @param array $drawings
+	 * @param string|null $outputPathFile
+	 * @return string if outputPathFile is null, return the image instead to write to file
+	 */
+	public function Render(array $drawings, $outputPathFile = NULL): string
+	{
+		foreach($drawings as $gdElement) {
+			$name = key($gdElement);
+			$prop = $gdElement[$name];
+			$color = isset($prop['color']) ? $this->getColor($prop['color']) : NULL;
+			switch ($name) {
+				case 'BODY':
+					$this->image = imagecreatetruecolor($prop['w'], $prop['h']);
+					break;
+				case 'TEXT':
+					imagettftext($this->image, $prop['size'], 0, $prop['x'], $prop['y'], $color, 'C:\Windows\Fonts\arial.ttf', $prop['value']);
+					break;
+				case 'FILL':
+					imagefill($this->image, $prop['x'], $prop['y'], $color);
+					break;
+				case 'RECTFILL':
+					imagefilledrectangle($this->image, $prop['x1'], $prop['y1'], $prop['x2'], $prop['y2'], $color);
+					break;
+				case 'LINE':
+					imageline($this->image, $prop['x1'], $prop['y1'], $prop['x2'], $prop['y2'], $color);
+					break;
+				case 'PIXEL':
+					imagesetpixel($this->image, $prop['x'], $prop['y'], $color);
+					break;
+				case 'ELLIPSE':
+					imagefilledellipse($this->image, $prop['x'], $prop['y'], $prop['w'], $prop['h'], $color);
+					break;
+			}
+		}
+
+		ob_start();
+		imagepng($this->image, $outputPathFile);
+		$data = ob_get_contents();
+		ob_end_clean();
+		return $data;
+	}
+
+	public function Destroy()
+	{
+		if ($this->image) {
+			imagedestroy($this->image);
+			$this->image = NULL;
+		}
+	}
+
+	public function __destruct()
+	{
+		$this->Destroy();
+	}
+}