cropSize($image); $background = $this->driver()->handleInput($this->background); foreach ($image as $frame) { $this->modify($frame, $resize, $background); } return $image; } /** * @throws ColorException */ protected function modify( FrameInterface $frame, SizeInterface $resize, ColorInterface $background, ): void { // create new canvas with target size & transparent background color $modified = Cloner::cloneEmpty($frame->native(), $resize, $background); // make image area transparent to keep transparency // even if background-color is set $transparent = imagecolorallocatealpha( $modified, $background->channel(Red::class)->value(), $background->channel(Green::class)->value(), $background->channel(Blue::class)->value(), 127, ); // create transparent area to place the original on top imagealphablending($modified, false); // do not blend / just overwrite imagecolortransparent($modified, $transparent); imagefilledrectangle( $modified, $resize->pivot()->x() * -1, $resize->pivot()->y() * -1, abs($resize->pivot()->x()) + $frame->size()->width() - 1, abs($resize->pivot()->y()) + $frame->size()->height() - 1, $transparent, ); // place original imagecopy( $modified, $frame->native(), $resize->pivot()->x() * -1, $resize->pivot()->y() * -1, 0, 0, $frame->size()->width(), $frame->size()->height(), ); // set new content as resource $frame->setNative($modified); } }