ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FitSquare.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
36{
38
39 public const ID = 'fit_square';
40
41
42 public function getId(): string
43 {
44 return self::ID;
45 }
46
47
48 public function canHandleDefinition(FlavourDefinition $definition): bool
49 {
50 return $definition instanceof FitToSquare;
51 }
52
53 public function dependsOnEngine(): ?string
54 {
55 return GDEngine::class;
56 }
57
58 public function processStream(
59 FileInformation $information,
60 FileStream $stream,
61 FlavourDefinition $for_definition
62 ): \Generator {
63 if (!$for_definition instanceof FitToSquare) {
64 throw new \InvalidArgumentException('Invalid definition');
65 }
66 $image = $this->from($stream);
67 if (!is_resource($image) && !$image instanceof \GdImage) {
68 return;
69 }
70
71 $size = $for_definition->getMaxSize();
72
73 $cur_width = imagesx($image);
74 $cur_height = imagesy($image);
75
76 if ($cur_width < $size && $cur_height < $size) {
77 return;
78 }
79
80 $width_ratio = $size / $cur_width;
81 $height_ratio = $size / $cur_height;
82 $ratio = min($width_ratio, $height_ratio);
83
84 $new_height = (int) floor($cur_height * $ratio);
85 $new_width = (int) floor($cur_width * $ratio);
86 $resized = imagescale(
87 $image,
88 $new_width,
89 $new_height,
90 IMG_BICUBIC
91 );
92 imagedestroy($image);
93
94 $stream = $this->to(
95 $resized,
96 $for_definition->getQuality(),
97 );
98
99 yield new Result(
100 $for_definition,
101 $stream,
102 0,
103 $for_definition->persist()
104 );
105 }
106}
canHandleDefinition(FlavourDefinition $definition)
Check if a corresponding configuration can be processed by this Machine.
Definition: FitSquare.php:48
processStream(FileInformation $information, FileStream $stream, FlavourDefinition $for_definition)
Definition: FitSquare.php:58
dependsOnEngine()
Return the class name of the Engine that is required for this Machine to work.
Definition: FitSquare.php:53
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
The base interface for all filesystem streams.
Definition: FileStream.php:32
to(\GdImage $image, ?int $quality=null)
Currently this is the only way to make a FileStream from a GD image resource.
if(!file_exists('../ilias.ini.php'))