ImageMagick COM+: Trying to convert SVG-file to PNG via Classic ASP
I'm using ImageMagickObject COM+ in Classic ASP. I am trying to convert an SVG-file to a PNG one. I tried the example seen here:
convert rose.jpg rose.png
This works fine when performed in the command-line. This example also works when executed using classic ASP. So, everything seems to work. I then try the same command via ASP, only using an SVG-file instead of JPG as the source file like so:
convert rose.svg rose.png
This does not work. I get no error, but still no PNG-file.
So I then tried this command in the command-line, and it works. I have tried different SVG-files and they all fail to convert using ASP, but all work via the command-line.
The component therefore seems to be installed, and I can convert and write to file from ASP, so I'm guessing the permissions are fine as well.
What could be the issue here?
I modified the example posted below. This code currently works:
Dim sourceFile : sourceFile = server.mappath("/tempbild/rose.jpg") Dim destFile : destFile = server.mappath("/tempbild/test.png") Dim img: Set img = CreateObject("ImageMagickObject.MagickImage.1") Dim DrawResult DrawResult = img.Convert(sourceFile, destFile) If Err.Number <> 0 Then Response.Write("ImageMagick component failed: " & Err.Number & ": " & Err.Description) else response.write("ImageMagick component tested successfully: " & DrawResult) end if Set img = nothing
When I change the file type to SVG in either sourceFile or destFile, then it stops working. I do not get an error message and no DrawResult.
ImageMagick has a rather unique way of working if you use the COM object. The parameters in the commandline should be passed as strings in the COM call, but they keep their "-" prefix. I have an example function that draws a polygon, that should give you an idea how to implement Imagemagick instructions in ASP:
function DrawPoly(destFile, coordinates, fillcolor, strokecolor) ' Draws a single polygon and returns a result-image Dim img: Set img = CreateObject("ImageMagickObject.MagickImage.1") dim polygon, DrawCommand, DrawResult polygon = trim(coordinates) DrawCommand = "polygon " & trim(polygon) DrawResult = img.Convert(Server.Mappath(destFile), "-matte", "-fill", fillColor, "-stroke", strokeColor, "-draw", DrawCommand, Server.Mappath(destFile)) If Err.Number <> 0 Then Response.Write(Err.Number & ": " & Err.Description & vbCrLf & msgs) DrawPoly = destFile Set img = nothing end function
edit: Here is another piece of code, a bit simpler, to see if ImageMagick works at all. Please note you need to give write permissions to the IUSR_[machinename] in the directory you are writing the image.
Dim destFile : destFile = server.mappath("/test.jpg") Dim img: Set img = CreateObject("ImageMagickObject.MagickImage.1") Dim DrawResult DrawResult = img.Convert("logo:","-format","%m,%h,%w",destFile) If Err.Number <> 0 Then Response.Write("ImageMagick component failed: " & Err.Number & ": " & Err.Description) else response.write("ImageMagick component tested successfully: <a href=""/~temp/test.jpg"" target=""_blank"">" & DrawResult & "</a>") end if Set img = nothing
Ok, I finally managed to find the answer here. It seems the IIS does not use the TEMP directory when converting between jpg and png, which is why it works. However, when converting to or from svg, the TEMP directory is written to. On the TEMP directory of the server (c:\windows\temp in my case) you need at least to give "change" rights to IUSR user, I added "write" rights.