ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
FitSquare.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
21 
29 
34 class FitSquare extends AbstractMachine implements FlavourMachine
35 {
37 
38  public const ID = 'fit_square';
39 
40 
41  public function getId(): string
42  {
43  return self::ID;
44  }
45 
46 
47  public function canHandleDefinition(FlavourDefinition $definition): bool
48  {
49  return $definition instanceof FitToSquare;
50  }
51 
52  public function dependsOnEngine(): ?string
53  {
54  return GDEngine::class;
55  }
56 
57  public function processStream(
58  FileInformation $information,
59  FileStream $stream,
60  FlavourDefinition $for_definition
61  ): \Generator {
62  if (!$for_definition instanceof \ILIAS\ResourceStorage\Flavour\Definition\FitToSquare) {
63  throw new \InvalidArgumentException('Invalid definition');
64  }
65  $image = $this->from($stream);
66  if (!is_resource($image) && !$image instanceof \GdImage) {
67  return;
68  }
69 
70  $size = $for_definition->getMaxSize();
71 
72  $cur_width = imagesx($image);
73  $cur_height = imagesy($image);
74 
75  if ($cur_width < $size && $cur_height < $size) {
76  return;
77  }
78 
79  $width_ratio = $size / $cur_width;
80  $height_ratio = $size / $cur_height;
81  $ratio = min($width_ratio, $height_ratio);
82 
83  $new_height = (int)floor($cur_height * $ratio);
84  $new_width = (int)floor($cur_width * $ratio);
85  $resized = imagescale(
86  $image,
87  $new_width,
88  $new_height,
89  IMG_BICUBIC
90  );
91  imagedestroy($image);
92 
93  $stream = $this->to(
94  $resized,
95  $for_definition->getQuality(),
96  );
97 
98  yield new Result(
99  $for_definition,
100  $stream,
101  0,
102  $for_definition->persist()
103  );
104  }
105 }
canHandleDefinition(FlavourDefinition $definition)
Check if a corresponding configuration can be processed by this Machine.
Definition: FitSquare.php:47
processStream(FileInformation $information, FileStream $stream, FlavourDefinition $for_definition)
Definition: FitSquare.php:57
Interface Observer Contains several chained tasks and infos about them.
persist()
Define whether the generated flavor and the respective streams should be persisted, or whether they should only be generated and used in-memory.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
to(\GdImage $image, int $quality=null)
Currently this is the only way to make a FileStream from a GD image resource.
dependsOnEngine()
Return the class name of the Engine that is required for this Machine to work.
Definition: FitSquare.php:52
The base interface for all filesystem streams.
Definition: FileStream.php:31