How to get the original size of a image?

Hi,

I have a image and I want to display it on the entire page.
I use this code and it works :

 #image.decode(bytes(img), width: 100%, height: 100%)

I have see that decode will be deprecated but with the lib I use today with my rust code it is the only way I found to make it works.

Now, I can have a image that is rotated. So If I use this way, I have only a part of a image.
I would like to check the width and the height of the original image to see if I need to rotate it or not.

But I do not found a way to get the original size of the image in my typst code.
Is there a way to do that, or maybe a auto rotate to take all the place ?

You can use measure.

/// Create an image that can be used with the magnify functions.
/// Must be used with `source()` function.
///
/// Note. You can use standard `image` by providing both width and height.
#let image(source, ..args) = {
  assert(
    type(source) == bytes,
    message: "Use source() function as the first argument",
  )
  let image = std.image.with(source, ..args)
  context image(..measure(image()))
}
1 Like

Sometimes I found very difficult to understand how to use typst…

Thank you, now I can have a image without deprecated decode like this :

#let i = image(bytes(img), format: "png")
#i

But I can access to its size… I wanted to display it to see but the document can be generated with this code :

   #let width = context i.width
   #width

or this
#context measure(i, width: 100pt)

Finally found layout(size => measure(i, …size))

Thank you !!!

I try to “flipped” the page if height of image is bigger that width but this not works :

 #let i = image(bytes(img), format: "png")
 #layout(size => [
          #let (width, height) = measure(i, ..size)
          #if height > width [
            #set page(
              margin: 0pt,
              flipped: false,
            )
            #i
          ]
          #if height < width [
            #set page(
              margin: 0pt,
              flipped: true,
            )
            #i
          ]
        ])

Please follow rules for composing messages: How to post in the Questions category - #7 by Andrew. Don’t you see that the code snippet doesn’t show the # that was initially written?

Since you need to measure original size, it makes no sense to constrain measure with currently available layout space/size.

I don’t understand if you just want to place images, or is it a background image.

Here is how you rotate the image:

#set page(background: context {
  let images = ("horizontal.jpg", "square.jpg", "vertical.jpg")
  let img = image.with(images.at(here().page() - 1))
  let fill = img(width: 100%, height: 100%)
  let (width, height) = measure(img())
  if width <= height { fill } else { rotate(-90deg, reflow: true, fill) }
})
#pagebreak()
#pagebreak()
3 pages

Sources:

Since page-dependent context can’t be used with page.flipped, I don’t know if it can be done cleanly.

#let images = ("horizontal.jpg", "square.jpg", "vertical.jpg")
#let is-flipped() = {
  let img = image(images.at(here().page() - 1))
  let (width, height) = measure(img)
  width > height
}
#let background-image = context {
  image(images.at(here().page() - 1), width: 100%, height: 100%)
}

#set page(background: background-image)

#context page(flipped: is-flipped())[]
#context page(flipped: is-flipped())[]
#context page(flipped: is-flipped())[]