From 9e195a1a55af3ed7ac1af996f184f8db704f1008 Mon Sep 17 00:00:00 2001 From: Erika Heidi Date: Fri, 25 Jun 2021 18:58:45 +0200 Subject: [PATCH] Refactoring to use erikaheidi/gdaisy --- app/Command/Generate/TwitterController.php | 19 +++- app/ImageSource/GhSponsorImageSource.php | 3 +- .../TwitterFollowerImageSource.php | 3 +- app/Resources/templates/cover_basic.json | 68 +++++++---- app/Resources/templates/cover_colorful.json | 68 +++++++---- app/Resources/templates/cover_neon.json | 68 +++++++---- app/Resources/templates/cover_sponsors.json | 90 +++++++++------ app/Template.php | 107 +----------------- composer.json | 3 +- composer.lock | 46 +++++++- 10 files changed, 259 insertions(+), 216 deletions(-) diff --git a/app/Command/Generate/TwitterController.php b/app/Command/Generate/TwitterController.php index b8a24c4..3267db2 100644 --- a/app/Command/Generate/TwitterController.php +++ b/app/Command/Generate/TwitterController.php @@ -3,9 +3,10 @@ namespace App\Command\Generate; use App\ImageSource; -use App\Service\TwitterServiceProvider; use App\Storage; use App\Template; +use GDaisy\ImagePlaceholder; +use GDaisy\PlaceholderInterface; use Minicli\Command\CommandController; class TwitterController extends CommandController @@ -27,13 +28,27 @@ public function handle(): int $template = Template::create(Storage::root() . $template_file); $featured = []; + //build sources foreach ($template->sources as $key => $params) { /** @var ImageSource $source */ $source = new $params['class']; $featured = array_merge($featured, $source->getImageList($this->getApp(), $params['count'])); } - $template->build($featured); + //apply + /** + * @var string $key + * @var PlaceholderInterface $placeholder + */ + foreach ($template->placeholders as $key => $placeholder) { + if ($placeholder instanceof ImagePlaceholder and $placeholder->image) { + $placeholder->apply($template->getResource(), ['image_file' => $placeholder->image]); + continue; + } + + $placeholder->apply($template->getResource(), $featured[$key]); + } + $template->write($save_path); $this->getPrinter()->info("Finished generating cover at $save_path."); diff --git a/app/ImageSource/GhSponsorImageSource.php b/app/ImageSource/GhSponsorImageSource.php index 73e2781..84e4b34 100644 --- a/app/ImageSource/GhSponsorImageSource.php +++ b/app/ImageSource/GhSponsorImageSource.php @@ -33,7 +33,8 @@ public function getImageList(App $app, $limit = 5): array $avatar = Storage::downloadImage($sponsor->avatarUrl); $featured[self::$prefix . "$count"] = [ 'screen_name' => $sponsor->login, - 'avatar' => $avatar + 'avatar' => $avatar, + 'image_file' => $avatar ]; $count++; diff --git a/app/ImageSource/TwitterFollowerImageSource.php b/app/ImageSource/TwitterFollowerImageSource.php index 7f64836..a075b66 100644 --- a/app/ImageSource/TwitterFollowerImageSource.php +++ b/app/ImageSource/TwitterFollowerImageSource.php @@ -34,7 +34,8 @@ public function getImageList(App $app, $limit = 5): array $avatar = Storage::downloadImage($avatar_path); $featured[self::$prefix . "$count"] = [ 'screen_name' => $follower->screen_name, - 'avatar' => $avatar + 'avatar' => $avatar, + 'image_file' => $avatar ]; $count++; diff --git a/app/Resources/templates/cover_basic.json b/app/Resources/templates/cover_basic.json index fe5a5a8..6b13498 100644 --- a/app/Resources/templates/cover_basic.json +++ b/app/Resources/templates/cover_basic.json @@ -9,41 +9,59 @@ }, "elements": { "tw1": { - "pos_x": 486, - "pos_y": 272, - "width": 130, - "height": 130 + "type": "image", + "properties": { + "pos_x": 486, + "pos_y": 272, + "width": 130, + "height": 130 + } }, "tw2": { - "pos_x": 670, - "pos_y": 270, - "width": 132, - "height": 132 + "type": "image", + "properties": { + "pos_x": 670, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw3": { - "pos_x": 859, - "pos_y": 270, - "width": 132, - "height": 132 + "type": "image", + "properties": { + "pos_x": 859, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw4": { - "pos_x": 1049, - "pos_y": 270, - "width": 132, - "height": 132 + "type": "image", + "properties": { + "pos_x": 1049, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw5": { - "pos_x": 1236, - "pos_y": 270, - "width": 130, - "height": 130 + "type": "image", + "properties": { + "pos_x": 1236, + "pos_y": 270, + "width": 130, + "height": 130 + } }, "cover": { - "width": 1500, - "height": 500, - "pos_x": 0, - "pos_y": 0, - "image": "app/Resources/images/cover_basic.png" + "type": "image", + "properties": { + "width": 1500, + "height": 500, + "pos_x": 0, + "pos_y": 0, + "image": "app/Resources/images/cover_basic.png" + } } } } \ No newline at end of file diff --git a/app/Resources/templates/cover_colorful.json b/app/Resources/templates/cover_colorful.json index ee45f99..14ca1e6 100644 --- a/app/Resources/templates/cover_colorful.json +++ b/app/Resources/templates/cover_colorful.json @@ -9,41 +9,59 @@ }, "elements": { "tw1": { - "pos_x": 486, - "pos_y": 272, - "width": 130, - "height": 130 + "type": "image", + "properties": { + "pos_x": 486, + "pos_y": 272, + "width": 130, + "height": 130 + } }, "tw2": { - "pos_x": 670, - "pos_y": 270, - "width": 132, - "height": 132 + "type": "image", + "properties": { + "pos_x": 670, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw3": { - "pos_x": 859, - "pos_y": 270, - "width": 132, - "height": 132 + "type": "image", + "properties": { + "pos_x": 859, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw4": { - "pos_x": 1049, - "pos_y": 270, - "width": 132, - "height": 132 + "type": "image", + "properties": { + "pos_x": 1049, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw5": { - "pos_x": 1236, - "pos_y": 270, - "width": 130, - "height": 130 + "type": "image", + "properties": { + "pos_x": 1236, + "pos_y": 270, + "width": 130, + "height": 130 + } }, "cover": { - "width": 1500, - "height": 500, - "pos_x": 0, - "pos_y": 0, - "image": "app/Resources/images/cover_colorful.png" + "type": "image", + "properties": { + "width": 1500, + "height": 500, + "pos_x": 0, + "pos_y": 0, + "image": "app/Resources/images/cover_colorful.png" + } } } } \ No newline at end of file diff --git a/app/Resources/templates/cover_neon.json b/app/Resources/templates/cover_neon.json index d86b72f..eccb86e 100644 --- a/app/Resources/templates/cover_neon.json +++ b/app/Resources/templates/cover_neon.json @@ -9,41 +9,59 @@ }, "elements": { "tw1": { - "pos_x": 838, - "pos_y": 275, - "width": 98, - "height": 98 + "type": "image", + "properties": { + "pos_x": 486, + "pos_y": 272, + "width": 130, + "height": 130 + } }, "tw2": { - "pos_x": 962, - "pos_y": 275, - "width": 98, - "height": 98 + "type": "image", + "properties": { + "pos_x": 670, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw3": { - "pos_x": 1087, - "pos_y": 275, - "width": 98, - "height": 98 + "type": "image", + "properties": { + "pos_x": 859, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw4": { - "pos_x": 1212, - "pos_y": 275, - "width": 98, - "height": 98 + "type": "image", + "properties": { + "pos_x": 1049, + "pos_y": 270, + "width": 132, + "height": 132 + } }, "tw5": { - "pos_x": 1338, - "pos_y": 275, - "width": 98, - "height": 98 + "type": "image", + "properties": { + "pos_x": 1236, + "pos_y": 270, + "width": 130, + "height": 130 + } }, "cover": { - "width": 1500, - "height": 500, - "pos_x": 0, - "pos_y": 0, - "image": "app/Resources/images/cover_neon.png" + "type": "image", + "properties": { + "width": 1500, + "height": 500, + "pos_x": 0, + "pos_y": 0, + "image": "app/Resources/images/cover_neon.png" + } } } } \ No newline at end of file diff --git a/app/Resources/templates/cover_sponsors.json b/app/Resources/templates/cover_sponsors.json index 09b12ad..86c011a 100644 --- a/app/Resources/templates/cover_sponsors.json +++ b/app/Resources/templates/cover_sponsors.json @@ -13,53 +13,77 @@ }, "elements": { "sp1": { - "width": 140, - "height": 140, - "pos_x": 898, - "pos_y": 326 + "type": "image", + "properties": { + "width": 140, + "height": 140, + "pos_x": 898, + "pos_y": 326 + } }, "sp2": { - "width": 140, - "height": 140, - "pos_x": 1101, - "pos_y": 326 + "type": "image", + "properties": { + "width": 140, + "height": 140, + "pos_x": 1101, + "pos_y": 326 + } }, "sp3": { - "width": 140, - "height": 140, - "pos_x": 1311, - "pos_y": 326 + "type": "image", + "properties": { + "width": 140, + "height": 140, + "pos_x": 1311, + "pos_y": 326 + } }, "tw1": { - "pos_x": 461, - "pos_y": 393, - "width": 70, - "height": 70 + "type": "image", + "properties": { + "pos_x": 461, + "pos_y": 393, + "width": 70, + "height": 70 + } }, "tw2": { - "pos_x": 548, - "pos_y": 393, - "width": 70, - "height": 70 + "type": "image", + "properties": { + "pos_x": 548, + "pos_y": 393, + "width": 70, + "height": 70 + } }, "tw3": { - "pos_x": 636, - "pos_y": 393, - "width": 70, - "height": 70 + "type": "image", + "properties": { + "pos_x": 636, + "pos_y": 393, + "width": 70, + "height": 70 + } }, "tw4": { - "pos_x": 727, - "pos_y": 393, - "width": 70, - "height": 70 + "type": "image", + "properties": { + "pos_x": 727, + "pos_y": 393, + "width": 70, + "height": 70 + } }, "cover": { - "width": 1500, - "height": 500, - "pos_x": 0, - "pos_y": 0, - "image": "app/Resources/images/cover_sponsors.png" + "type": "image", + "properties": { + "width": 1500, + "height": 500, + "pos_x": 0, + "pos_y": 0, + "image": "app/Resources/images/cover_sponsors.png" + } } } } diff --git a/app/Template.php b/app/Template.php index 8b57a94..4e692f8 100644 --- a/app/Template.php +++ b/app/Template.php @@ -2,21 +2,13 @@ namespace App; -class Template +use GDaisy\Template as GDaisy; + +class Template extends GDaisy { - public string $name; - public int $width; - public int $height; public array $sources; - public array $placeholders = []; - protected $resource; - public function __construct(string $name) - { - $this->name = $name; - } - - static function create(string $filename): Template + static function create(string $filename): GDaisy { $template = new Template(basename($filename)); $template->loadJson($filename); @@ -26,97 +18,8 @@ static function create(string $filename): Template public function loadJson(string $json_file) { + parent::loadJson($json_file); $json_content = json_decode(file_get_contents($json_file), true); - - $this->width = $json_content['width']; - $this->height = $json_content['height']; $this->sources = $json_content['sources']; - - foreach ($json_content['elements'] as $key => $element) { - $this->addPlaceholder($key, $element['width'], $element['height'], $element['pos_x'], $element['pos_y'], $element['image'] ?? null); - } - } - - public function addPlaceholder(string $key, int $width, int $height, int $pos_x = 0, int $pos_y = 0, string $image = null) - { - $this->placeholders[$key] = [ - 'width' => $width, - 'height' => $height, - 'pos_x' => $pos_x, - 'pos_y' => $pos_y, - 'image' => $image - ]; - } - - public function getPlaceholder(string $key) - { - return $this->placeholders[$key] ?? null; - } - - public function build(array $images) - { - foreach ($this->placeholders as $key => $placeholder) { - - if ($placeholder['image']) { - $this->apply($key, Storage::root() . $placeholder['image']); - continue; - } - - $this->apply($key, $images[$key]['avatar']); - } - } - - public function write(string $path) - { - imagepng($this->getResource(), $path); - } - - public function apply(string $key, string $image_file) - { - $placeholder = $this->getPlaceholder($key); - - if ($placeholder && is_file($image_file)) { - $resource = $this->getResource(); - $stamp = $this->getStampResource($image_file); - $info = getimagesize($image_file); - - imagecopyresized( - $resource, - $stamp, - $placeholder['pos_x'], - $placeholder['pos_y'], - 0, - 0, - $placeholder['width'], - $placeholder['height'], - $info[0], - $info[1] - ); - } - } - - public function getResource() - { - if (!$this->resource) { - $this->resource = imagecreatetruecolor($this->width, $this->height); - } - - return $this->resource; - } - - public function getStampResource(string $image_file) - { - $info = getimagesize($image_file); - $extension = image_type_to_extension($info[2]); - - if (strtolower($extension) == '.png') { - return imagecreatefrompng($image_file); - } - - if (strtolower($extension) == '.jpeg' OR strtolower($extension) == '.jpg') { - return imagecreatefromjpeg($image_file); - } - - return null; } } \ No newline at end of file diff --git a/composer.json b/composer.json index f135adc..ceaada4 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "abraham/twitteroauth": "^2.0", "ext-gd": "*", "ext-json": "*", - "minicli/curly": "^0.1.2" + "minicli/curly": "^0.1.2", + "erikaheidi/gdaisy": "^0.1.5" }, "scripts": { "post-install-cmd": [ diff --git a/composer.lock b/composer.lock index 79aaaa9..46b86e1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6bbec74ed39f5b8350ca530d2c7788fb", + "content-hash": "ff2d15be925595100cccb7157e46e9c4", "packages": [ { "name": "abraham/twitteroauth", @@ -143,6 +143,50 @@ ], "time": "2021-01-12T12:10:35+00:00" }, + { + "name": "erikaheidi/gdaisy", + "version": "0.1.5", + "source": { + "type": "git", + "url": "https://github.com/erikaheidi/gdaisy.git", + "reference": "7296916e1d9c3c331767eb7fe44f686e1287f408" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/erikaheidi/gdaisy/zipball/7296916e1d9c3c331767eb7fe44f686e1287f408", + "reference": "7296916e1d9c3c331767eb7fe44f686e1287f408", + "shasum": "" + }, + "require": { + "ext-gd": "*", + "ext-json": "*", + "minicli/minicli": "^2.2", + "php": "^7.4|^8.0" + }, + "require-dev": { + "pestphp/pest": "^1.0" + }, + "bin": [ + "bin/gdaisy" + ], + "type": "library", + "autoload": { + "psr-4": { + "GDaisy\\": "src/", + "GDaisy\\Command\\": "bin/command" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "php-gd templating system", + "support": { + "issues": "https://github.com/erikaheidi/gdaisy/issues", + "source": "https://github.com/erikaheidi/gdaisy/tree/0.1.5" + }, + "time": "2021-06-21T10:07:35+00:00" + }, { "name": "minicli/curly", "version": "0.1.2",