19 declare(strict_types=1);
39 public const ID =
'crop_rectangle';
55 return GDEngine::class;
65 throw new \InvalidArgumentException(
'Invalid definition');
67 $image = $this->
from($stream);
68 if (!is_resource($image) && !$image instanceof \GdImage) {
73 $stream_path = $stream->getMetadata(
'uri');
74 if ($stream_path ===
'php://memory') {
75 [$source_width, $source_height] = getimagesizefromstring((
string) $stream);
77 [$source_width, $source_height] = getimagesize($stream_path);
80 $target_width = $for_definition->getMaxWidth();
81 $target_height = (
int) ($for_definition->getMaxWidth() / $for_definition->getRatio());
83 [$cutout_width, $cutout_height, $x_shift, $y_shift] = $this->
calculateCutout(
86 $for_definition->getRatio()
89 $thumb = imagecreatetruecolor(
108 imagedestroy($image);
110 $target_stream = $this->
to($thumb, $for_definition->getQuality());
129 $cutout_width = $source_width;
130 $cutout_height = (
int) ($source_width / $target_ratio);
132 $y_shift = (
int) (($source_height - $cutout_height) / 2);
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);
141 return [$cutout_width, $cutout_height, $x_shift, $y_shift];
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.
trait GdImageToStreamTrait
processStream(FileInformation $information, FileStream $stream, FlavourDefinition $for_definition)
calculateCutout(int $source_width, int $source_height, float $target_ratio)
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.
The base interface for all filesystem streams.