From a52bda223294ff8981f5e271df533e940972af50 Mon Sep 17 00:00:00 2001 From: Maxim <48408304+123jjck@users.noreply.github.com> Date: Tue, 13 Aug 2024 14:18:31 +0500 Subject: [PATCH] Add $file parameter, format code --- renderer.php | 137 +++++++++++++++++++++++++++++---------------------- 1 file changed, 79 insertions(+), 58 deletions(-) diff --git a/renderer.php b/renderer.php index ef69303..d35edac 100644 --- a/renderer.php +++ b/renderer.php @@ -1,63 +1,84 @@ startX = $x; - $this->startY = $y; - } - - protected function hexColorAllocate($im,$hex){ - $hex = ltrim($hex,'#'); - $r = hexdec(substr($hex,0,2)); - $g = hexdec(substr($hex,2,2)); - $b = hexdec(substr($hex,4,2)); - return imagecolorallocate($im, $r, $g, $b); - } - - public function render($ava, $type = 'jpeg') { - if(empty($ava)) throw new Exception('avatar cannot be empty'); - - $ava = str_replace("", "", $ava); - $ava = str_replace("", "", $ava); - - $canvas = $this->processRender($ava); - ob_start(); - - if($type == 'jpeg') imagejpeg ($canvas, null, 100); - if($type == 'png' || $type = 'base64') imagepng ($canvas, null, 0); - $image_data = ob_get_contents(); - ob_end_clean(); - imagedestroy($canvas); - - if($type == 'jpeg' || $type == 'png') { - $image = $image_data; - } else if($type == 'base64') { - $image = base64_encode($image_data); - } else { - throw new Exception('unknown image type'); +/* Render, please PHP by DevJjck, 2024 + Original Render, please by Lunnaholy */ +class Renderer +{ + public function __construct($x = 0, $y = 0) + { + $this->startX = $x; + $this->startY = $y; } - return $image; - } - - protected function processRender($ava) { - $canvas = imagecreatetruecolor(189,219); - $x = $this->startX; - $y = $this->startY; - - for($index = 4; $index < strlen($ava); $index += 6){ - $hexcolor = substr($ava, $index + 4, 2) . substr($ava, $index + 2, 2) . substr($ava, $index, 2); - $color = $this->hexColorAllocate($canvas, $hexcolor); - //imagefilledrectangle($canvas, $x, $y, 1, 1, $color); - imagesetpixel($canvas, $x, $y, $color); - $x++; - if($x == 189){ // go to new line - $x = 0; - $y++; - $index = $index + 2; - } + + protected function hexColorAllocate($im, $hex) + { + $hex = ltrim($hex, '#'); + $r = hexdec(substr($hex, 0, 2)); + $g = hexdec(substr($hex, 2, 2)); + $b = hexdec(substr($hex, 4, 2)); + return imagecolorallocate($im, $r, $g, $b); + } + + public function render($ava, $type = 'jpeg', $file = null) + { + if (empty($ava)) + throw new Exception('Avatar cannot be empty'); + + if ($type == 'base64' && !empty($saveTo)) + throw new Exception('Cannot save base64 image to file'); + + $ava = str_replace("", "", $ava); + $ava = str_replace("", "", $ava); + + $canvas = $this->processRender($ava); + + if (empty($file)) + ob_start(); + + switch ($type) { + case 'jpeg': + case 'jpg': + $result = imagejpeg($canvas, $file, 100); + break; + case 'png': + $result = imagepng($canvas, $file, 0); + break; + case 'base64': + $result = imagepng($canvas, $file, 0); + break; + default: + throw new Exception('unknown image type'); + } + + if (empty($file)) { + $image = ob_get_contents(); + ob_end_clean(); + imagedestroy($canvas); + return $type !== 'base64' ? $image : base64_encode($image); + } else { + return $result; + } + } + + protected function processRender($ava) + { + $canvas = imagecreatetruecolor(189, 219); + + $x = $this->startX; + $y = $this->startY; + + for ($index = 4; $index < strlen($ava); $index += 6) { + $hexcolor = substr($ava, $index + 4, 2) . substr($ava, $index + 2, 2) . substr($ava, $index, 2); + $color = $this->hexColorAllocate($canvas, $hexcolor); + imagesetpixel($canvas, $x, $y, $color); + $x++; + if ($x == 189) { + // go to new line + $x = 0; + $y++; + $index = $index + 2; + } + } + return $canvas; } - return $canvas; - } } ?>