-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Rasterize with Gmagick/Imagick if available #98
Comments
The great thing of 'php-svg' is that it has so little dependency. |
Yes, and there is no plan to change that. |
I am thinking Then I've made a start on this work, but there's plenty more to do:
@meyfa Can you kindly share your thoughts on this proposed solution/direction? I'd be happy to work some more on this. I've made a start in #216 (it's a mess.. I know..).
From a users perspective this woud look something like: <?php
// Unchanged
$image = new SVG(100, 100);
$doc = $image->getDocument();
// circle with radius 20 and green border, center at (50, 50)
$doc->addChild(
(new SVGCircle(50, 50, 20))
->setStyle('fill', 'none')
->setStyle('stroke', '#0F0')
->setStyle('stroke-width', '2px')
);
// Change start
// rasterize to a 200x200 image, i.e. the original SVG size scaled by 2.
// the background will be transparent by default.
$rasterImage = $image->toRasterImage(200, 200);
header('Content-Type: image/png');
//old: imagepng($rasterImage);
$rasterImage->toPng()
// Or write to a file:
//old: imagepng($rasterImage, 'some/path/to/file.png');
$rasterImage->toPng('some/path/to/file.png'); The above would pick the optimal rasterizer based on available extensions (probably based on some order of preference for rasterizers). Alternatively which rasterizer should be used can be specified, like so: <?php
// new SVG(100, 100);
$rasterImage = $image->toRasterImage(200, 200, GdRasterizer::class); Alternatively a preferred rasterizer can be set like so: <?php
// new SVG(100, 100);
$rasterImage = $image->toRasterImage(200, 200, GdRasterizer::class);
$rasterImage = $image->toRasterImage(200, 200); // Library determines which rasterizer to use.
// Set preferred rasterizer:
RasterizerFactory::setRasterizer(ImagickRasterizer::class);
$rasterImage = $image->toRasterImage(200, 200); // Uses `ImagickRasterizer`. |
@Niellles I think your proposal is reasonable. It's probably important to make the rasterizer configurable, e.g. with an argument, like you propose. Keep in mind that Basically, we should support:
Your design seems to satisfy all requirements 👍 We could also have a global configuration setting, but this can always be added later (to just override the default) and is also perhaps not the best design. Small nitpick: The functions should probably be called |
The current rasterizer implementation is severely lacking due to limitations with PHP's GD module. The only advantage of GD over Gmagick, and the reason this library exists, is that GD is much more widely available.
I propose to add a simple adapter for Gmagick/Imagick nonetheless. A check would need to be done, determining whether Gmagick/Imagick is available. If it is, use it. Otherwise, use the GD rasterizer.
Work on GD would still need to continue, but this way, if you are lucky enough to have one of the aforementioned packages installed on your system, you could profit immediately.
The text was updated successfully, but these errors were encountered: