Get image width and height (velocity)

Hello,
Is there a way to get a width / height of an image (in url / attachment) as a parameter in a velocity script? I.e. something like #set ($width = $attachment.getWidth()).

Thanks.

I think the following will help you - if you have programming rights.

I need to build a groovy class to do the job, which is called from velocity.
The code uses the TIKA-Software ( https://tika.apache.org/ ) of the Apache-Foundation which is allready used by XWiki.

First:

Put the following Groovy code into a file (without{{grooy}} and {{/groovy}}”)
In the following the file name used is “localtools.attributes.WebHome

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;

class attributes {
  private xwiki
  private context

  void setObjects(xwiki, context) { setXWiki(xwiki);  setContext(context) }
  void setXWiki(xwiki)   { this.xwiki = xwiki }
  void setContext(context) { this.context = context }

  def get (String docName,String attName,String whichAttribute,Boolean removeUnits=false) {
      def attachment
      String R
      def theDoc = this.xwiki.getDocument(docName)
      if  (theDoc.isNew()) {
        print "> Document "+docName+" not found!"
      } else 
      {
        attachment = theDoc.getAttachment(attName)
        if (attachment == null) { print "> Attachment "+attName+" from document "+docName+" not found!" }  
        else 
        {
          Metadata metadata = new Metadata();
          def inAtt = attachment.getContentInputStream()
          try {
            Parser parser = new AutoDetectParser();
            BodyContentHandler handler = new BodyContentHandler();
            ParseContext thisContext = new ParseContext();
            parser.parse(inAtt, handler, metadata, thisContext);
            String[] metadataNames = metadata.names()  //getting the list of all meta data elements 
            if (whichAttribute == "all") {
                R=  "\n|=key|=value\n"
                metadataNames.sort().each  { key -> 
                 R +=  "\n|"+key.toString()+"|"
                 def values = metadata.getValues(key)
                 if (values.size() == 1) { R += values[0].toString() }
                  else { values.each { v -> R+=  v.toString()+"\n" } }
                }
           } else {
               def values = metadata.getValues(whichAttribute)
              if (values.size() ==1 ) { 
                  if (removeUnits) { 
                    return values[0].replaceAll(/[^0-9\.\,]/,"" )  } else 
                   { return (values[0]) }
             } else { return values }
           }
         }   catch (Exception e) { 
                    R += e.getMessage();
                    R += "Failed to retrieve the content of attachment: " + attachment.getReference() }
       }
    }
    return R
  }
}

Second:

Create a velocity and initialize a function to be used:

{{velocity}}
#set($attributes = $xwiki.parseGroovyFromPage("localtools.attributes.WebHome"))
$attributes.setObjects($xwiki,$context)
{{/velocity}}

The function $attributes.get has 4 parameter:

(1) document name
(2) attachment name
(3) “all” or a specific name of the attribute
(4) true/false to discards possible units like " pixels" within the returned value
(default is false)

Some examples:

a) all values

{{velocity}}
$attributes.get($doc.fullName,"LENIN RR.jpg","all")
{{/velocity}}

You will get a long table like (here shortened !)

key value
Component 1 Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
Component 2 Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
Component 3 Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
Compression Type Baseline
Content-Type image/jpeg
File Size 1900484 bytes
Image Height 1960 pixels
Image Width 4032 pixels

b) examples for single values:

{{velocity}}
$attributes.get($doc.fullName,"LENIN RR.jpg","Image Height")
$attributes.get($doc.fullName,"LENIN RR.jpg","Image Height",true)
$attributes.get($doc.fullName,"LENIN RR.jpg","Image Width")
{{/velocity}}

give

1960 pixels
1960
4032 pixels

Note:
The TIKA-Software can do more: E.g. extract the text from a pdf file.