Search code examples
phpscreenshotsnapshot

Height of a webpage for snapshots in PHP


We have an application that takes snapshots of certain web pages. It's quite tightly integrated into the code, so we're not ready to incorporate another library.

But we don't have a way of being able to calculate the web page height, so we end up taking snaps of 8000px height. Which is now proving troublesome when inserted into PDFs.

Is there a way to find the height of the webpage in PHP?


Solution

  • By definition, no. You can reliably tell the height of a web page only after it has been rendered, because the rendering engine decides how it is going to interpret the markup provided.

    PHP does not have a HTML rendering engine, so it's impossible to tell a page height using PHP.

    You need to utilize your snapshot application for this. Only the renderer built into that app can give you reliable info about how tall the web page is going to be in the end result.

    Don't forget the page height can vary even between different versions of the same browser (most prominently, Internet Explorer) depending on how margin padding etc. are interpreted.

    If the images that the snapshot app produces have too much space to the bottom, consider using ImageMagick and its -trim option that can remove excess space from images.