forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
740 additions
and
58 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
Services/Badge/classes/Flavours/class.ilBadgePictureDefinition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
declare(strict_types=1); | ||
|
||
use ILIAS\ResourceStorage\Flavour\Definition\FlavourDefinition; | ||
|
||
class ilBadgePictureDefinition implements FlavourDefinition | ||
{ | ||
private const ID = 'badge_image_resize_flavor'; | ||
|
||
private int $quality = 50; | ||
|
||
private array $sizes = [ | ||
'big' => 512, | ||
'small' => 100, | ||
'xsmall' => 75, | ||
'xxsmall' => 30 | ||
]; | ||
|
||
public function __construct( | ||
) { | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return self::ID; | ||
} | ||
|
||
public function getFlavourMachineId(): string | ||
{ | ||
return ilBadgePictureMachine::ID; | ||
} | ||
|
||
public function getInternalName(): string | ||
{ | ||
return 'badge_picture'; | ||
} | ||
|
||
public function getVariantName(): ?string | ||
{ | ||
return json_encode([ | ||
'quality' => $this->quality, | ||
'sizes' => $this->sizes | ||
]); | ||
} | ||
|
||
public function persist(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getSizes(): array | ||
{ | ||
return $this->sizes; | ||
} | ||
|
||
public function getQuality(): int | ||
{ | ||
return $this->quality; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
Services/Badge/classes/Flavours/class.ilBadgePictureMachine.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of ILIAS, a powerful learning management system | ||
* published by ILIAS open source e-Learning e.V. | ||
* | ||
* ILIAS is licensed with the GPL-3.0, | ||
* see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
* You should have received a copy of said license along with the | ||
* source code, too. | ||
* | ||
* If this is not the case or you just want to try ILIAS, you'll find | ||
* us at: | ||
* https://www.ilias.de | ||
* https://github.com/ILIAS-eLearning | ||
* | ||
*********************************************************************/ | ||
|
||
declare(strict_types=1); | ||
|
||
use ILIAS\Filesystem\Stream\FileStream; | ||
use ILIAS\ResourceStorage\Flavour\Definition\CropToSquare; | ||
use ILIAS\ResourceStorage\Flavour\Definition\FlavourDefinition; | ||
use ILIAS\ResourceStorage\Flavour\Definition\ToGreyScale; | ||
use ILIAS\ResourceStorage\Flavour\Engine\GDEngine; | ||
use ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\AbstractMachine; | ||
use ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\CropSquare; | ||
use ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\GdImageToStreamTrait; | ||
use ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\MakeGreyScale; | ||
use ILIAS\ResourceStorage\Flavour\Machine\FlavourMachine; | ||
use ILIAS\ResourceStorage\Flavour\Machine\Result; | ||
use ILIAS\ResourceStorage\Information\FileInformation; | ||
|
||
/** | ||
* @author Fabian Schmid <[email protected]> | ||
*/ | ||
class ilBadgePictureMachine extends AbstractMachine implements FlavourMachine | ||
{ | ||
use GdImageToStreamTrait; | ||
public const ID = "badge_image_resize_machine"; | ||
private const FULL_QUALITY_SIZE_THRESHOLD = 100; | ||
private CropSquare $crop; | ||
private MakeGreyScale $grey; | ||
private ?ilBadgePictureDefinition $definition = null; | ||
private ?FileInformation $information = null; | ||
|
||
public function __construct() | ||
{ | ||
$this->crop = new CropSquare(); | ||
} | ||
|
||
|
||
public function getId(): string | ||
{ | ||
return self::ID; | ||
} | ||
|
||
public function canHandleDefinition(FlavourDefinition $definition): bool | ||
{ | ||
return $definition instanceof ilBadgePictureDefinition; | ||
} | ||
|
||
public function dependsOnEngine(): ?string | ||
{ | ||
return GDEngine::class; | ||
} | ||
|
||
public function processStream( | ||
FileInformation $information, | ||
FileStream $stream, | ||
FlavourDefinition $for_definition | ||
): \Generator { | ||
/** @var ilBadgePictureDefinition $for_definition */ | ||
$this->definition = $for_definition; | ||
$this->information = $information; | ||
|
||
$i = 0; | ||
foreach ($for_definition->getSizes() as $size) { | ||
yield new Result( | ||
$for_definition, | ||
$this->cropImage($stream, $size), | ||
$i, | ||
true | ||
); | ||
$i++; | ||
} | ||
} | ||
|
||
protected function cropImage( | ||
FileStream $stream, | ||
int $size | ||
) { | ||
$quality = $size <= self::FULL_QUALITY_SIZE_THRESHOLD | ||
? 100 // we take 100% jpeg quality for small resultions | ||
: $this->definition->getQuality(); | ||
|
||
|
||
return $this->crop->processStream( | ||
$this->information, | ||
$stream, | ||
new CropToSquare( | ||
false, | ||
$size, | ||
$quality | ||
) | ||
)->current()->getStream(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.