Skip to content

Commit

Permalink
Simplify Driver::createShape()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jan 12, 2025
1 parent faaa116 commit 227b81d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
23 changes: 11 additions & 12 deletions src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Intervention\Image\Drivers\Vips;

use Intervention\Image\Decoders\BinaryImageDecoder;
use Intervention\Image\Drivers\AbstractDriver;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\Image;
use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
Expand Down Expand Up @@ -110,30 +108,31 @@ public function __invoke(): ImageInterface
}

/**
* @param string $shape
* @param array<string, string|int> $attributes
*
* @param int $width
* @param int $height
* @throws RuntimeException
* @return VipsImage
*/
public static function createShape(string $shape, array $attributes, int $width, int $height): ImageInterface
public static function createShape(string $shape, array $attributes, int $width, int $height): VipsImage
{
$xmlAttributes = implode(
' ',
array_map(
fn ($key, $value) => sprintf('%s="%s"', $key, htmlspecialchars((string) $value)),
fn($key, $value) => sprintf('%s="%s"', $key, htmlspecialchars((string) $value)),
array_keys($attributes),
$attributes
)
);

$svg = <<<EOL
<svg viewBox="0 0 {$width} {$height}" xmlns="http://www.w3.org/2000/svg">
<{$shape} {$xmlAttributes} />
</svg>
EOL;
$svg = '<svg viewBox="0 0 ' . $width . ' ' . $height . '" xmlns="http://www.w3.org/2000/svg">' .
'<' . $shape . ' ' . $xmlAttributes . ' />' .
'</svg>';

try {
return InputHandler::withDecoders([new BinaryImageDecoder()], new self())->handle($svg);
} catch (\Exception $e) {
return VipsImage::svgload_buffer($svg);
} catch (VipsException $e) {
throw new RuntimeException('Could not create shape: ' . $e->getMessage(), previous: $e);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Modifiers/DrawBezierModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function apply(ImageInterface $image): ImageInterface
};
}, $chunks, array_keys($chunks)));

$polygon = Driver::createShape(
$bezier = Driver::createShape(
'path',
[
'd' => $points,
Expand All @@ -47,7 +47,7 @@ public function apply(ImageInterface $image): ImageInterface
$frames = [];
foreach ($image as $frame) {
$frames[] = $frame->setNative(
$frame->native()->composite($polygon->core()->native(), [BlendMode::OVER])
$frame->native()->composite($bezier, [BlendMode::OVER])
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Modifiers/DrawEllipseModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function apply(ImageInterface $image): ImageInterface
$frames = [];
foreach ($image as $frame) {
$frames[] = $frame->setNative(
$frame->native()->composite($ellipse->core()->native(), [BlendMode::OVER])
$frame->native()->composite($ellipse, [BlendMode::OVER])
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Modifiers/DrawLineModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DrawLineModifier extends GenericDrawLineModifier implements SpecializedInt
*/
public function apply(ImageInterface $image): ImageInterface
{
$ellipse = Driver::createShape(
$line = Driver::createShape(
'line',
[
'x1' => $this->drawable->start()->x(),
Expand All @@ -40,7 +40,7 @@ public function apply(ImageInterface $image): ImageInterface
$frames = [];
foreach ($image as $frame) {
$frames[] = $frame->setNative(
$frame->native()->composite($ellipse->core()->native(), [BlendMode::OVER])
$frame->native()->composite($line, [BlendMode::OVER])
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Modifiers/DrawPolygonModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function apply(ImageInterface $image): ImageInterface
$frames = [];
foreach ($image as $frame) {
$frames[] = $frame->setNative(
$frame->native()->composite($polygon->core()->native(), [BlendMode::OVER])
$frame->native()->composite($polygon, [BlendMode::OVER])
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Modifiers/DrawRectangleModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function apply(ImageInterface $image): ImageInterface
$frames = [];
foreach ($image as $frame) {
$frames[] = $frame->setNative(
$frame->native()->composite($rectangle->core()->native(), [BlendMode::OVER])
$frame->native()->composite($rectangle, [BlendMode::OVER])
);
}

Expand Down

0 comments on commit 227b81d

Please sign in to comment.