|
@@ -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();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|