ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilObjectTileImageFlavourMachine.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
33 
35 {
36  public const ID = "4c7e3aaff42a352fa3fd3dfc4d4a994cc3dfdd97e97c2c2c9932e22e2e57357a";
37  private const FULL_QUALITY_SIZE_THRESHOLD = 100;
40  private ?FileInformation $information = null;
41 
42  public function __construct()
43  {
44  $this->crop = new CropRectangle();
45  }
46 
47  public function getId(): string
48  {
49  return self::ID;
50  }
51 
52  public function canHandleDefinition(FlavourDefinition $definition): bool
53  {
54  return $definition instanceof ilObjectTileImageFlavourDefinition;
55  }
56 
57  public function dependsOnEngine(): ?string
58  {
59  return GDEngine::class;
60  }
61 
62  public function processStream(
63  FileInformation $information,
64  FileStream $stream,
65  FlavourDefinition $for_definition
66  ): \Generator {
68  $this->definition = $for_definition;
69  $this->information = $information;
70 
71  $i = 0;
72  foreach ($for_definition->getWidths() as $width) {
73  if (($crop_stream = $this->cropImage($stream, $width)) === null) {
74  continue;
75  }
76  yield new Result(
77  $for_definition,
78  $crop_stream,
79  $i,
80  true
81  );
82  $i++;
83  }
84  }
85 
86  protected function cropImage(
87  FileStream $stream,
88  int $width
89  ): ?Stream {
90  $quality = $width <= self::FULL_QUALITY_SIZE_THRESHOLD
91  ? 100 // we take 100% jpeg quality for small resultions
92  : $this->definition->getQuality();
93 
94  return $this->crop->processStream(
95  $this->information,
96  $stream,
97  new CropToRectangle(
98  false,
99  $width,
100  $this->definition->getRatio(),
101  $quality
102  )
103  )->current()?->getStream();
104  }
105 }
processStream(FileInformation $information, FileStream $stream, FlavourDefinition $for_definition)
canHandleDefinition(FlavourDefinition $definition)
Check if a corresponding configuration can be processed by this Machine.
dependsOnEngine()
Return the class name of the Engine that is required for this Machine to work.
The base interface for all filesystem streams.
Definition: FileStream.php:31
__construct()
FlavourMachines must be able to be created without further dependencies.