ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CropRectangle.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
36{
38
39 public const ID = 'crop_rectangle';
40 public const QUALITY = 30;
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 CropToRectangle;
51 }
52
53 public function dependsOnEngine(): ?string
54 {
55 return GDEngine::class;
56 }
57
58
59 public function processStream(
60 FileInformation $information,
61 FileStream $stream,
62 FlavourDefinition $for_definition
63 ): \Generator {
64 if (!$for_definition instanceof CropToRectangle) {
65 throw new \InvalidArgumentException('Invalid definition');
66 }
67 $image = $this->from($stream);
68 if (!is_resource($image) && !$image instanceof \GdImage) {
69 // we can't handle this image
70 return;
71 }
72
73 $stream_path = $stream->getMetadata('uri');
74 if ($stream_path === 'php://memory') {
75 [$source_width, $source_height] = getimagesizefromstring((string) $stream);
76 } else {
77 [$source_width, $source_height] = getimagesize($stream_path);
78 }
79
80 $target_width = $for_definition->getMaxWidth();
81 $target_height = (int) ($for_definition->getMaxWidth() / $for_definition->getRatio());
82
83 [$cutout_width, $cutout_height, $x_shift, $y_shift] = $this->calculateCutout(
84 (int) $source_width,
85 (int) $source_height,
86 $for_definition->getRatio()
87 );
88
89 $thumb = imagecreatetruecolor(
90 $target_width,
91 $target_height
92 );
93
94 imagecopyresampled(
95 $thumb,
96 $image,
97 0,
98 0,
99 $x_shift,
100 $y_shift,
101 $target_width,
102 $target_height,
103 $cutout_width,
104 $cutout_height
105 );
106
107
108 imagedestroy($image);
109
110 $target_stream = $this->to($thumb, $for_definition->getQuality());
111
112 yield new Result(
113 $for_definition,
114 $target_stream,
115 0,
116 $for_definition->persist()
117 );
118 }
119
124 private function calculateCutout(
125 int $source_width,
126 int $source_height,
127 float $target_ratio
128 ): array {
129 $cutout_width = $source_width;
130 $cutout_height = (int) ($source_width / $target_ratio);
131 $x_shift = 0;
132 $y_shift = (int) (($source_height - $cutout_height) / 2);
133
134 if ($cutout_height > $source_height) {
135 $cutout_height = $source_height;
136 $cutout_width = (int) ($cutout_height * $target_ratio);
137 $x_shift = (int) (($source_width - $cutout_width) / 2);
138 $y_shift = 0;
139 }
140
141 return [$cutout_width, $cutout_height, $x_shift, $y_shift];
142 }
143}
calculateCutout(int $source_width, int $source_height, float $target_ratio)
dependsOnEngine()
Return the class name of the Engine that is required for this Machine to work.
canHandleDefinition(FlavourDefinition $definition)
Check if a corresponding configuration can be processed by this Machine.
processStream(FileInformation $information, FileStream $stream, FlavourDefinition $for_definition)
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'))