Skip to content

Commit

Permalink
Merge pull request #7 from mattiabasone/refactor_render
Browse files Browse the repository at this point in the history
Refactor skin/avatar rendering
  • Loading branch information
mattiabasone committed Jan 25, 2021
2 parents b8aa491 + 3a60020 commit f9ea95b
Show file tree
Hide file tree
Showing 56 changed files with 2,836 additions and 1,693 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lumen-mysql.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GitHub Action for Lumen with MySQL and Redis
name: Testing App (Lumen with MySQL)
on: [push, pull_request]
on: [push]
jobs:
lumen:
name: Lumen (PHP ${{ matrix.php-versions }})
Expand Down Expand Up @@ -81,6 +81,6 @@ jobs:
shell: bash
run: git fetch && git checkout ${{ steps.extract_branch.outputs.branch }}
- name: "[Coveralls] Send report"
run: vendor/bin/php-coveralls -v -x clover.xml -o coveralls-upload.json
run: ./vendor/bin/php-coveralls -v -x clover.xml -o coveralls-upload.json
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27 changes: 23 additions & 4 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,48 @@ $finder = PhpCsFixer\Finder::create()
__DIR__.'/tests'
]);

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setFinder($finder)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => [
'syntax' => 'short'
],
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
'blank_line_before_return' => true,
'blank_line_before_statement' => true,
'date_time_immutable' => true,
'declare_strict_types' => true,
'fopen_flags' => [
'b_mode' => true
],
'fully_qualified_strict_types' => true,
'line_ending' => true,
'linebreak_after_opening_tag' => true,
'mb_str_functions' => true,
'native_function_invocation' => true,
'no_closing_tag' => true,
'no_empty_phpdoc' => false,
'no_trailing_whitespace' => true,
'no_useless_else' => true,
'no_extra_blank_lines' => true,
'no_spaces_after_function_name' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_around_offset' => true,
'no_superfluous_phpdoc_tags' => false,
'no_unused_imports' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'ordered_imports' => true,
'ordered_class_elements' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_align' => false,
'phpdoc_separation' => false,
'phpdoc_types_order' => true,
'phpdoc_summary' => false,
'phpdoc_order' => true,
'protected_to_private' => false,
'pre_increment' => false,
'single_trait_insert_per_statement' => false,
'strict_param' => true,
'yoda_style' => [
Expand Down
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:7.4.10-cli-alpine
FROM php:7.4.14-cli-alpine

LABEL maintainer="Mattia Basone mattia.basone@gmail.com"

Expand All @@ -12,7 +12,7 @@ RUN apk update \

RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
&& docker-php-ext-configure pdo_dblib --with-libdir=lib \
&& pecl install redis swoole \
&& pecl install redis swoole xdebug \
&& docker-php-ext-install \
bcmath \
dom \
Expand All @@ -34,6 +34,7 @@ RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/i
redis \
opcache \
swoole
# xdebug

# Imagick Setup
RUN set -ex \
Expand All @@ -50,6 +51,10 @@ RUN adduser -D ${APP_USER} -u ${DEFAULT_USER_UID} -s /bin/bash
RUN mkdir ${APP_PATH} && \
chown -R ${APP_USER}:${APP_USER} ${APP_PATH}

#RUN cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini \
# && echo "[XDEBUG]" >> /usr/local/etc/php/php.ini \
# && echo "xdebug.mode=coverage" >> /usr/local/etc/php/php.ini

WORKDIR ${APP_PATH}

USER ${APP_USER}
Expand Down
4 changes: 2 additions & 2 deletions app/Cache/UserNotFoundCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public static function has(string $usernameOrUuid): bool
}

/**
* @param $usernameOrUuid
* @param string $usernameOrUuid
*
* @return bool
*/
public static function add($usernameOrUuid): bool
public static function add(string $usernameOrUuid): bool
{
return Cache::add(self::PREFIX.\md5($usernameOrUuid), 1, self::TTL);
}
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/CheckUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function handle(MojangClient $mojangClient): int
/** @var \Minepic\Models\Account $account */
$account = Account::find($result->id);
$this->info("Checking {$account->username} [{$account->uuid}]...");

try {
$this->updateAccount($account);
$this->updateAccountSkin($account);
Expand All @@ -73,7 +74,7 @@ public function handle(MojangClient $mojangClient): int
}

/**
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|Account[]
* @return Account[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
*/
private function getAccountsIds()
{
Expand Down
4 changes: 2 additions & 2 deletions app/Events/Account/AccountCreatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
class AccountCreatedEvent extends Event
{
/**
* @var Account|null
* @var null|Account
*/
private ?Account $account;

/**
* @param Account|null $account
* @param null|Account $account
*/
public function __construct(?Account $account)
{
Expand Down
12 changes: 7 additions & 5 deletions app/Helpers/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function exists(string $uuid): bool
public static function save(string $uuid, $rawData): bool
{
$fp = \fopen(static::getPath($uuid), 'wb');
if ($fp) {
if (\is_resource($fp)) {
\fwrite($fp, $rawData);
\fclose($fp);

Expand All @@ -69,14 +69,16 @@ public static function save(string $uuid, $rawData): bool
/**
* Use Steve file for given uuid.
*
* @param mixed
* @param string $name
* @throws \Exception
* @return bool
*/
public static function copyAsSteve(string $string): bool
public static function copyAsSteve(string $name): bool
{
if ($string !== '') {
if ($name !== '') {
return \copy(
static::getPath(MinecraftDefaults::getRandomDefaultSkin()),
static::getPath($string)
static::getPath($name)
);
}

Expand Down
6 changes: 3 additions & 3 deletions app/Helpers/UserDataValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class UserDataValidator
/**
* Checks if the given string is a valid username.
*
* @param $username
* @param string $username
*
* @return bool
*/
public static function isValidUsername(string $username): bool
{
return !(\preg_match('#[\W]+#', $username) === 1);
return \preg_match('#[\W]+#', $username) !== 1;
}

/**
Expand All @@ -36,7 +36,7 @@ public static function isValidUuid(string $uuid): bool
/**
* Check if is an email address has invalid characters.
*
* @param string
* @param string $email
*
* @return bool
*/
Expand Down
30 changes: 30 additions & 0 deletions app/Helpers/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* @see https://www.php.net/manual/en/function.imagecopymerge.php#92787
* @param \GdImage $dst_im
* @param \GdImage $src_im
* @param int $dst_x
* @param int $dst_y
* @param int $src_x
* @param int $src_y
* @param int $src_w
* @param int $src_h
* @param int $pct
*/
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
{
// creating a cut resource
$cut = \imagecreatetruecolor($src_w, $src_h);

// copying relevant section from background to the cut resource
\imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);

// copying relevant section from watermark to the cut resource
\imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

// insert cut resource to destination image
\imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
8 changes: 2 additions & 6 deletions app/Http/Controllers/Api/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AvatarController extends BaseApiController
/**
* Serve Avatar.
*
* @param \Illuminate\Http\Request
* @param \Illuminate\Http\Request $request
* @param string $uuid
* @param int $size
*
Expand All @@ -34,11 +34,7 @@ public function serveUuid(Request $request, string $uuid, $size = 0): Response
}

/**
* @param int $size
*
* @throws \Minepic\Image\Exceptions\ImageCreateFromPngFailedException
* @throws \Minepic\Image\Exceptions\ImageTrueColorCreationFailedException
* @throws \Minepic\Image\Exceptions\InvalidSectionSpecifiedException
* @param int|string $size
*
* @return Response
*/
Expand Down
11 changes: 4 additions & 7 deletions app/Http/Controllers/Api/DownloadTextureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
use Laravel\Lumen\Http\ResponseFactory;
use Laravel\Lumen\Routing\Controller as BaseController;
use Minepic\Helpers\Storage\Files\SkinsStorage;
use Minepic\Image\Sections\Skin;
use Minepic\Image\Sections\Raw;
use Minepic\Minecraft\MinecraftDefaults;
use Minepic\Resolvers\UuidResolver;

/**
* Class BaseApiController.
*/
class DownloadTextureController extends BaseController
{
/**
Expand Down Expand Up @@ -60,9 +57,9 @@ public function serve(string $uuid = ''): Response
SkinsStorage::getPath($this->uuidResolver->getUuid()) :
SkinsStorage::getPath(MinecraftDefaults::STEVE_DEFAULT_SKIN_NAME);

$userSkin = new Skin($skinPath);
$userSkin->prepareTextureDownload();
$rawSkin = new Raw($skinPath);
$rawSkin->render();

return $this->responseFactory->make($userSkin, 200, $headers);
return $this->responseFactory->make((string) $rawSkin, 200, $headers);
}
}
7 changes: 3 additions & 4 deletions app/Http/Controllers/Api/IsometricAvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class IsometricAvatarController extends BaseApiController
/**
* Serve isometric avatar.
*
* @param \Illuminate\Http\Request
* @param \Illuminate\Http\Request $request
* @param string $uuid User UUID
* @param int $size
*
* @throws \Throwable
*
* @return \Illuminate\Http\Response
*/
public function serveUuid(Request $request, $uuid, $size = 0): Response
public function serveUuid(Request $request, string $uuid, $size = 0): Response
{
$this->uuidResolver->resolve($uuid);
$this->dispatchAccountImageServedEvent();
Expand All @@ -31,9 +31,8 @@ public function serveUuid(Request $request, $uuid, $size = 0): Response
}

/**
* @param int $size
* @param int|string $size
*
* @throws \Minepic\Image\Exceptions\SkinNotFountException
* @throws \Throwable
*
* @return Response
Expand Down
12 changes: 5 additions & 7 deletions app/Http/Controllers/Api/SkinBackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Minepic\Image\Components\Side;

class SkinBackController extends BaseApiController
{
/**
* Serve Avatar.
*
* @param \Illuminate\Http\Request
* @param \Illuminate\Http\Request $request
* @param string $uuid User UUID
* @param int $size
*
Expand All @@ -27,12 +26,12 @@ public function serveUuid(Request $request, $uuid, $size = 0): Response
$this->dispatchAccountImageServedEvent();

return $this->pngResponse(
(string) $this->rendering->skin($this->uuidResolver->getUuid(), (int) $size, Side::BACK)
(string) $this->rendering->skinBack($this->uuidResolver->getUuid(), (int) $size)
);
}

/**
* @param int $size
* @param int|string $size
*
* @throws \Throwable
*
Expand All @@ -41,10 +40,9 @@ public function serveUuid(Request $request, $uuid, $size = 0): Response
public function serveDefault($size = 0): Response
{
$image = $this->cache()->remember('rendering.system.default_skin_back', 3600, function () use ($size) {
return (string) $this->rendering->skin(
return (string) $this->rendering->skinBack(
null,
(int) $size,
Side::BACK
(int) $size
);
});

Expand Down

0 comments on commit f9ea95b

Please sign in to comment.