[NTG-context] resize and center and clip image

Mikael P. Sundqvist mickep at gmail.com
Thu May 1 15:38:29 CEST 2014


OK, sorry for the noise. I think I have the solution:

\startexternalfigurecollection[k.jpg]
     \useexternalfigure[k.jpg][k.jpg]
 \stopexternalfigurecollection


\externalfigurecollectionminwidth{k.jpg}
\externalfigurecollectionmaxheight{k.jpg}

\clip[width=8cm,height=7cm]{%
\framed[width=8cm,height=7cm,align={middle,lohi},offset=overlay]{%
\dimen0=\externalfigurecollectionminwidth{k.jpg}
\dimen2=\externalfigurecollectionmaxheight{k.jpg}
\dimen4=8cm
\dimen6=7cm
\startluacode
if tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then
      local skalning =
-0.5*(tex.dimen[4]/tex.dimen[0]*tex.dimen[2]-tex.dimen[6])
      context("\\vskip%ssp", skalning)
      context.externalfigure({"k.jpg"},{width="8cm"})
      context.sprint("foo")
else
      local skalning =
-0.5*(tex.dimen[6]/tex.dimen[2]*tex.dimen[0]-tex.dimen[4])
      context("\\hskip%ssp", skalning)
      context.externalfigure({"k.jpg"},{height="7cm"})
      context.sprint("bar")
end
\stopluacode
}}

If there are any bad side effects of this or simplifications, please
do not hesitate to tell me :)

/Mikael

On Thu, May 1, 2014 at 1:50 PM, Mikael P. Sundqvist <mickep at gmail.com> wrote:
> Hi!
>
> Assume that I want to fill in the text area with images, and I want to
> clip and scale them according to these rules:
>
> 1) If the image is "too wide" (in sense of proportions), then scale it
> so that the height fits the text area height, center it, and clip it
> to the text area. (See the attached image, on top, where the blue area
> is supposed to show the text area and the red rectangle the image.)
>
> 2) If the image is "too high" (in sense of proportions), then scale it
> so that the width fits the text area width, center it, and clip it to
> the text area. (See the attached image, on bottom.)
>
> The background why I want to do this is that I'm making a small
> template for presentations, and I'd like the users to insert images as
> background without really having to think about if their proportions
> fit or not.
>
> Best regards, Mikael


More information about the ntg-context mailing list