Jump to content

[SOLVED]Reading SVG image data


IIYAMA

Recommended Posts

  • Moderators
Posted (edited)

I am trying to read the svg file images and write them to temporary files.

But it looks like the format isn't correct decoded and the temporary files don't work.

local image = attributes["xlink:href"] 
if image then 
    --data:image/png;base64, 
  
    local _,stringMiddle = string.find(image,"base64,") 
    if stringMiddle then     
        local stringEnd = string.len(image) 
        local imageInfo = string.sub(image, 0, stringMiddle) 
        local imageFileData = string.sub(image, stringMiddle+1, stringEnd) 
        if imageFileData then 
             
            -- remove spaces etc. 
            --------------------------------------------------------------------------------------------- 
            --------------------------------------------------------------------------------------------- 
            imageFileData = string.gsub(imageFileData,'\\r', "\r") 
            imageFileData = string.gsub(imageFileData,'\\n', "\n") 
            -- from [url=http://stackoverflow.com/questions/23488404/decode-base64-string-and-write-to-file]http://stackoverflow.com/questions/2348 ... te-to-file[/url] -- 
            --------------------------------------------------------------------------------------------- 
            ----------------------------- 
             
            imageFileData = base64Decode(imageFileData) 
             
            if imageFileData and imageFileData ~= "" then 
                local file = fileCreate("tempFolder/tempolaryImage.png") 
                if file then 
                    fileWrite(file, imageFileData)  
                    fileClose(file) 
                    if fileExists("tempFolder/tempolaryImage.png") then 
                        outputChatBox("yea") 
                        local texture = dxCreateTexture ( "tempFolder/tempolaryImage.png") 
                        if texture then 
                            outputChatBox("texture was a success!") 
                        else 
                            outputChatBox("texture failed!") 
                        end 
                        --fileDelete("tempFolder/tempolaryImage.png") -- delete ones readed. 
                    end 
                end 
            else 
                outputChatBox("imageFileData failed!") 
            end 
        end 
    end 
end 

The file I am decoding looks like this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB28AAAOsCAIAAADiCGozAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ 
bWFnZVJlYWR5ccllPAACPgVJREFUeNrs3QlcFOX/B/ARFhcBueQQREEUVBBCQBRRVARvkET5eSTl 
RWVFSaXkffZDf6mlpYlp/zDJ8ML7WlEpxBARQUhQVxAUQW4FQRb8z7G77C6zB5fn5/16lcvMM881 
szOz3332mXb+/v5lZWXEC6Gvr//CynrjoTOb4fHjx+gEAAAAAAAAAACA5uEQLyrElp+f7+bmhnAe 
OvMldpqZmRn6AQAAAAAAAAAAoHnU0AUAAAAAAAAAAAAAoBSiyQAAAAAAAAAAAACgHEf1pLa2tlOm 
  
[b]etc. etc. etc.[/b] 
  
EwqFQjDqdPJpZnV4uEK6EB4u5+l1Wq18vthr9FgP6aLQcvmotlq6GRIT5TymOajEd0jLuXrfybGx 
sY+MH9JUmb/6lRxLKlrpI17+4vv3z5TuTB3dcvFQ1qr/PNjxWIzFrye8sPVss/lCbe5rq/Mrm32n 
WoJ8cfm61xDe1QAAAABu3f8XYACcGqtfVbrzzAAAAABJRU5ErkJggg== 

Edited by Guest
Posted

Just out of curiosity, have you tried drawing the image in the browser? You should be able to enter the decoded data directly into the src attribute of an image class.

  • Moderators
Posted

Hmmm that is a good one.

I tried it, but as result the quotes are misplaced. In the decoded string there are also quotes. Probably the reason why they use base64. The browser will also think: "Wtf, I can't decode a decoded image."

  • Moderators
Posted

Fixed it!

imageFileData = string.gsub(imageFileData,'\n', "") 

I had to remove the line breaks, now it works like charm!

And thx for the reply Dealman!

This is the circa the result:

In illustrator

Ingame Not original scale X and Y.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...