Resize SVG and convert to PNG without losing quality
Resize SVG and convert to PNG without losing quality
I have discussed this with Mikko but haven't reached a conclusion. Please read the comments here: http://valokuva.org/?p=102#comment-5777 to get a picture of what I'm trying to achieve. I would post the question here again, but we already discussed some possible solutions there so I think it's better if you read those comments. Thanks.
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: Resize SVG and convert to PNG without losing quality
[edit] scratch that. It looks like you've tried it.
I'm not sure of all you're trying to do but I'm pretty sure you should be setting the resolution, not the units, with a number like 1000.
The units are things like pixelsperinch or pixelspercentimeter, whereas the resolution is a number.
Pete
I'm not sure of all you're trying to do but I'm pretty sure you should be setting the resolution, not the units, with a number like 1000.
The units are things like pixelsperinch or pixelspercentimeter, whereas the resolution is a number.
Pete
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: Resize SVG and convert to PNG without losing quality
Can you post the svg file you're using?
Pete
Pete
Re: Resize SVG and convert to PNG without losing quality
Sure, here it is: http://znupi.ath.cx/files/s2w1.svg (my server serves it as plain text for some reason.. anyway, my server doesn't have Imagick, my desktop does)
Say I want to resize it to 600x400 (its nominal size is 300x200), how would I do that? Thanks for the reply
Say I want to resize it to 600x400 (its nominal size is 300x200), how would I do that? Thanks for the reply
Re: Resize SVG and convert to PNG without losing quality
I've done it! Yay! I was expecting it to be something like this, but I didn't know all the Imagick functions and etcetera. After some testing and fiddling around, here's what I came up with:
If anyone needs this I hope they come across this thread. Or if someone knows a better way, I hope they come across this thread, too
Code: Select all
$im = new Imagick();
$im->readImage("s2w1.svg");
$res = $im->getImageResolution();
$x_ratio = $res['x'] / $im->getImageWidth();
$y_ratio = $res['y'] / $im->getImageHeight();
$im->removeImage(); // Remove the image because setResolution has to be called before readImage because the image gets rasterized after that, and setResolution has no effect.
$im->setResolution(900 * $x_ratio, 600 * $y_ratio); // this resizes the image to 900x600 pixels! Yay!! No loss of quality!!!
$im->readImage("s2w1.svg");
// Now you can convert to PNG or do whatever you want