Page 1 of 1

Initilizing an image from a string intead of a file

Posted: 2013-01-28T15:37:24-07:00
by J.D.
I am running ImageMagick from the command line, via PHP's exec() function. I need to convert an SVG image to PNG. Something like this:

exec('-convert image.svg image.png');

I need to get the contents of image.svg, and manipulate them before converting the file to PNG. Do I have to save the altered image.svg to a new file, or is there a way to write the PNG file from a string?

For example I would get the file into the $image variable like this:

$image = file_get_contents('image.svg');

Then I would manipulate $image, based on an outside input. How can I directly convert $image to a PNG image without having to save it first as an SVG image? Is that even possible?

Re: Initilizing an image from a string intead of a file

Posted: 2013-01-28T16:03:13-07:00
by anthony
You can specify an image on the command line, but there is command line limits, and the file needs to be converted to base64.

See IM Examples. File handling, inline:
http://www.imagemagick.org/Usage/files/#inline

Re: Initilizing an image from a string intead of a file

Posted: 2013-01-29T09:08:03-07:00
by J.D.
Thank you for the reply. It looks like my image is too large for that, so I'll just have to save it as a file and then have the script delete the file once the conversion is complete.

Re: Initilizing an image from a string intead of a file

Posted: 2013-01-29T09:18:49-07:00
by snibgo
I don't know PHP, but it may allow piping, as in the command...

Code: Select all

convert - image.png
... where something has piped the SVG contents to stdout.