Svg images and it's content data

Hello,

Is there any way to access asset content via asset scripts? What I need is to create metadata with width and height for svg images.

One way to do it is to use getJSON function to use external api to determine width and height and then put it to metadata. Is any other option to do it?

Greetings.

I just added it to the docs: https://docs.squidex.io/id-02-documentation/developer-guides/scripting#helper-methods

I would use the getAssetText method and a little bit of regex.

1 Like

Thank You @Sebastian!

1 Like

Hello,

getAssetText always returns ‘noAsset’ string, I’ve tested it with create asset script. When using id of asset that is already uploaded (and smaller than 4MB) also getting the same response.

Can you post your script?

Of course:

var id = ctx.assetId
// or var id = some_existing_svg_asset_id

if (ctx.asset.mimeType === 'image/svg+xml') {
    getAssetText(id, function(result) {

        ctx.command.metadata['content'] = result
        ctx.command.metadata['id'] = id

        replace();
    });
}

Sorry, the documentation was wrong here.

It should be getAssetText(ctx.asset)

1 Like

I think this would be general extension, that would be a useful general extension to support metadata for svgs. If you want you can provide a pull request. You just have to implement this interface: https://github.com/Squidex/squidex/blob/master/backend/src/Squidex.Domain.Apps.Entities/Assets/IAssetMetadataSource.cs

1 Like

Now It works :slight_smile: almost…

I can’t write metadata:

ctx.command.metadata[‘pixelWidth’] = width;

got error ‘FieldAccessExeption’, I’m assuming that pixelWidth/Height is reserved metadata name and I can’t write to it? Is any way to do it?

WHich version are you using?

{“version”:“7.12.0.0”}

Okay. I will have a look. Could be a limitation with the script engine.

1 Like

Thanks, I think it’s the case - writing metadata with name ‘pixelwidth’ works ok ‘pixelWidth’ returns an error.

I fixed the problem and added automatic SVG metadata extraction.

Thank You, great work!

1 Like