Resize SVG and convert to PNG without losing quality

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
Znupi

Resize SVG and convert to PNG without losing quality

Post by Znupi »

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.
Znupi

Re: Resize SVG and convert to PNG without losing quality

Post by Znupi »

Bump :-( can anyone help?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Resize SVG and convert to PNG without losing quality

Post by el_supremo »

[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
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Resize SVG and convert to PNG without losing quality

Post by el_supremo »

Can you post the svg file you're using?

Pete
Znupi

Re: Resize SVG and convert to PNG without losing quality

Post by Znupi »

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 :-)
Znupi

Re: Resize SVG and convert to PNG without losing quality

Post by Znupi »

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:

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
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 :P
Post Reply