Skip to content

Commit

Permalink
Starting to convert tables
Browse files Browse the repository at this point in the history
  • Loading branch information
gvollbach committed Jan 9, 2024
1 parent b15b323 commit a0211fc
Show file tree
Hide file tree
Showing 9 changed files with 740 additions and 58 deletions.
77 changes: 77 additions & 0 deletions Services/Badge/classes/Flavours/class.ilBadgePictureDefinition.php
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 Services/Badge/classes/Flavours/class.ilBadgePictureMachine.php
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();
}

}
7 changes: 6 additions & 1 deletion Services/Badge/classes/class.ilBadgeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public function getImageFromResourceId(ilBadge $badge, ?string $image_rid) : str
if ($image_rid !== null) {
$identification = $this->resource_storage->manage()->find($image_rid);
if ($identification !== null) {
$image_src = $this->resource_storage->consume()->src($identification)->getSrc();
$flavour = $this->resource_storage->flavours()->get($identification, new \ilBadgePictureDefinition());
$urls = $this->resource_storage->consume()->flavourUrls($flavour)->getURLsAsArray(false);
if(is_array($urls) && sizeof($urls) === 4 && isset($urls[1])) {
$image_src = $urls[1];
}
}
} else {
$image_src = $badge->getImage();
Expand All @@ -55,6 +59,7 @@ public function processImageUpload(ilBadge $badge) : void
$array_result = array_pop($array_result);
$stakeholder = new ilBadgeFileStakeholder();
$identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
$this->resource_storage->flavours()->ensure($identification, new \ilBadgePictureDefinition());
$badge->setImageRid($identification);
$badge->update();
} catch (IllegalStateException $e) {
Expand Down
7 changes: 6 additions & 1 deletion Services/Badge/classes/class.ilBadgeImageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function processImageUpload( $badge) : void
if($array_result->getName() !== '') {
$stakeholder = new ilBadgeFileStakeholder();
$identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
$this->resource_storage->flavours()->ensure($identification, new \ilBadgePictureDefinition());
$badge->setImageRid($identification);
$badge->update();
}
Expand Down Expand Up @@ -377,7 +378,11 @@ public function getImageFromResourceId(?string $image_rid, int $badge_id = null)
if ($image_rid !== null) {
$identification = $this->resource_storage->manage()->find($image_rid);
if ($identification !== null) {
$image_src = $this->resource_storage->consume()->src($identification)->getSrc();
$flavour = $this->resource_storage->flavours()->get($identification, new \ilBadgePictureDefinition());
$urls = $this->resource_storage->consume()->flavourUrls($flavour)->getURLsAsArray(false);
if(is_array($urls) && sizeof($urls) === 4 && isset($urls[1])) {
$image_src = $urls[1];
}
}
} else {
if($badge_id !== null) {
Expand Down
Loading

0 comments on commit a0211fc

Please sign in to comment.