19 declare(strict_types=1);
42 public const ID =
'crop_square';
57 return GDEngine::class;
66 throw new \InvalidArgumentException(
'Invalid definition');
68 $image = $this->
from($stream);
69 if (!is_resource($image) && !$image instanceof \GdImage) {
73 $stream_path = $stream->getMetadata()[
'uri'] ??
'';
74 $must_flip = $this->
maybeRotate($stream_path, $image);
76 if ($stream_path ===
'php://memory') {
77 [$width, $height] = getimagesizefromstring((
string) $stream);
79 [$width, $height] = getimagesize($stream_path);
88 if ($width > $height) {
90 $x = (
int) (($width - $height) / 2);
91 $smallest_side = (
int) $height;
94 $y = (
int) (($height - $width) / 2);
95 $smallest_side = (
int) $width;
98 $size = (
int) $for_definition->getMaxSize();
100 $thumb = imagecreatetruecolor($size, $size);
115 imagedestroy($image);
117 $stream = $this->
to($thumb, $for_definition->getQuality());
127 protected function maybeRotate(
string $stream_path, \GdImage &$image): bool
131 if ($exif->isRunning() && ($exif_data = $exif->read($stream_path)) !== []) {
132 switch ($exif_data[
'Orientation'] ?? null) {
134 $image = imagerotate($image, 90, 0);
137 $image = imagerotate($image, 180, 0);
140 $image = imagerotate($image, -90, 0);
148 if ($imagick->isRunning()) {
149 $imagick = new \Imagick($stream_path);
150 $image_orientation = $imagick->getImageOrientation();
151 switch ($image_orientation) {
152 case \Imagick::ORIENTATION_RIGHTTOP:
153 $imagick->rotateImage(
'none', 90);
154 $imagick->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);
157 case \Imagick::ORIENTATION_BOTTOMRIGHT:
158 $imagick->rotateImage(
'none', 180);
159 $imagick->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);
162 case \Imagick::ORIENTATION_LEFTBOTTOM:
163 $imagick->rotateImage(
'none', -90);
164 $imagick->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);
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.
dependsOnEngine()
Return the class name of the Engine that is required for this Machine to work.
maybeRotate(string $stream_path, \GdImage &$image)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
canHandleDefinition(FlavourDefinition $definition)
Check if a corresponding configuration can be processed by this Machine.
to(\GdImage $image, int $quality=null)
Currently this is the only way to make a FileStream from a GD image resource.
trait GdImageToStreamTrait
static ofString(string $string)
Creates a new stream with an initial value.
processStream(FileInformation $information, FileStream $stream, FlavourDefinition $for_definition)
The base interface for all filesystem streams.