Initilizing an image from a string intead of a file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
J.D.
Posts: 2
Joined: 2013-01-28T15:11:58-07:00
Authentication code: 6789

Initilizing an image from a string intead of a file

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
J.D.
Posts: 2
Joined: 2013-01-28T15:11:58-07:00
Authentication code: 6789

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply