I'm using PHPQuery parse HTML file and I need the height and width of a given tag, in jQuery it's possible to use something like
height=$("div.content").height();
width=$("div.content").width();
but in PHPQuery there is no such thing as height(), width() functions so is there any way to get the dimensions?
PHPQuery just lets you traverse the document in a manner similar to jQuery. It does not actually render the document and so it has no idea what size each element will actually be when rendered by a browser.
In order to compute the width and height of an element, it would need to parse your stylesheet, apply all of those rules, fetch any images used by that element (and its descendants) and render the element using the appropriate font(s) -- and possibly even render parent elements as well. And there's no guarantee that the resulting dimensions will match some particular client configuration, since the client-side environment may cause the element to render at a different size (DPI being the most obvious example, but even the size of the browser window could affect the size of any given element).
Related
(Following this question: html columns whose width automatically changes according to their content )
From a PHP script, I print some HTML code, and I would like to know what will be the height of the result (for paging calculation). Suppose I have the following input:
$html code I want to print (string);
$css code, with full details of the font and size of all items that appear in the HTML (string);
all relevant browser settings (fonts, sizes, etc.);
page $width, in pixels (int).
I create a page that looks like this:
<html>
<head>
<style>$css</style>
</head>
<body style='width:$width'>
$html
</body>
</html>
I want to know, from within the PHP script, the number of pixels from the top of the page to the bottom of the text.
EDIT: I need to do this on the server side, before the content is displayed, because I use this to calculate what content to put. For example, if the content is too high, I might decide to put only the first half, etc.
Another possible use is to calculate what width is needed to achieve a particular height. For example, I might try to put it in a div with width 200px, but then the result may be too high; then I try width 400px, etc., until I find the width the gives the height that I want.
If you want to create your own server-side browser, you will have problems with load time. And if this is not a big problem, you can parse css (something like this: link) to find out what paddings, margins, fonts, line-heights and so on used in your $html variable, then, if you know font specifics, you can try to calculate content height (naturally given, from what browser the user entered).
You might be able to use the TCPDF class to do this. According to this thread the class has limit support for CSS but if your styles are supported the idea would be to examine the current positions, (GetX() and GetY()) before and after a writeHTML method call.
HTML rendering is done at client-side while PHP resides at server.
As such, PHP cannot determine HTML element height from server-side.
You would, however, be able to obtain that using JavaScript.
See: Get full height of a clipped DIV
How can I determine the explicit width of a HTML element that has width auto using PHP?
JQuery has the ability to do this if width:auto; is used it can compute the actual width.
Get the current computed width for the first element in the set of
matched elements.
JQuery probably does this with the help of the browser as it is client side.
How can I achieve this using PHP? I have the HTML as a string.
Update
All CSS is inline, if the width is auto, the only thing left to determine the width of the element is its immediate parent and/or its contents.
It is not possible. PHP does not know how the HTML/CSS will be rendered (if rendered at all).
The only way this would be possible in PHP is if you have both the HTML and the complete CSS as strings AND the width of the element is defined in absolute pixels or the width of its container (or the parent's container, etc) is defined in absolute pixels. If all these conditions were true, it would still be extremely difficult to figure out. Basically you'd have to code a rendering engine in PHP.
If you were really lucky, the element you needed would have an inline style="width:px" and you could parse for that attribute.
This is possible in Javascript because the browser knows its own size. Under most circumstances, the HTML in a string doesn't have a size until rendered in a browser.
I'm creating a scenario where I create a graph from an XML file (nodes+edges). The graph is being generated as an SVG inside an iframe. What I want to do is to be able to click on a node and have that node focused (different colour, scrolled into position).
What I've got is that I'm passing a URL in the attributes passed to graphviz that has the focus node in the query string so my-url.php?focus=2 will focus node id 2. Then, during attribute creation, I change the color of the focus node to bright yellow.
The problem is that I can't find a way to scroll down to this node. I have to manually search for the node by scrolling down. Is there a way I can get the coordinates of the node in focus and pass it to the window hosting the iframe? That way, I would be able to use Javascript to scroll down (or right).
Any ideas? I'm not looking for a complete solution. I can live with a strategy if it's feasible (and would work on the latest browsers. It doesn't have to be backward compatible).
Thanks.
You can retrieve the position of the nodes in the SVG by calling dot without the -T option (it produces the dot file where nodes are annotated with a "pos" attribute indicating where they are drawn. Then, when $_GET['focus'] is 42 (ie. my-url.php?focus=42 is requested), my-url.php should produce Javascript code which will scroll the iframe to the position of node 42, using something like scrollTo (http://stackoverflow.com/questions/1192228/scrolling-an-iframe-with-javascript).
To ensure that this works even if the image has been scaled (it's SVG, right), you would need to multiply the position to which you scroll by the ratio between the total width of the SVG and the width of the dot drawing as set by the "size" graph attribute (same thing for height).
I have static width of an element, and an unknown string length which is inserted by the user. I'm trying to keep javascript out otherwise this problem would be solved via using jQuery method described in other posts.
I'm generating a static page with php that won't change and I'm trying to calculate the size of the text in order to fit inside the div.
Is this possible with only having the following:
- String Length
- Width of Container
- Server side with PHP
and if so could you please lend me a hand in finding the correct solution?
Cheers
If this is about an HTML element, you will not be able to determine the width in PHP with 100% reliability. You would have to actually render the element in the client browser to find it out, and that is possible only using JavaScript.
Plus there are many factors on client side that can play into the result:
The operating system's text size level
The browser's zoom level
The availability of the font used on the client machine
You may be best off manually determining the element's width in an average web browser with normal zoom (or if it's dynamic text, maybe use what #Jared proposes)
This answer (by Pekka, see his response as well):
PHP Accurate Font Width
Suggests imagettfbbox, if you're using a truetype font.
Can we measure height of a div using php?
This is not possible at all: PHP serves HTML code. The browser renders it. Only after it is rendered, can height be determined reliably. Different browsers may end up with different heights. Different user settings (like font size) may end up with different heights.
The only way to find out an element's height is using JavaScript which runs in the browser. You can theoretically send the results back to a separate PHP script using Ajax, but I doubt that'll make much sense.
You could use jQuery's .height() like so:
var height = $("#elementID").height();
(there are native JavaScript approaches to this as well, but they tend to be a bit long and complicated.)
As others have said here, you cannot use PHP to read the height/width of a div already rendered. However, aside from the javascript options already presented keep in mind that you can use PHP to set the height/width of a div before it is sent to the browser. This would be in the form of an inline style of course. This is not the most elegant solution and to be honest I would avoid it, but you did not state what specifically it is that you want to do, and why.
Not sure if that info will help you in your implementation but it wasn't mentioned so far and thought I would contribute it.
No, we cannot. div is rendered by a browser based on CSS/JS. in a different browsers it can be different (IE, Firefox). It does not depends of PHP.
In case you are using text inside the div you could use strlen() to have some kind of measurement of height. I am saying some kind ofcourse because you are just counting the number of characters which then can be equated to some height depending on the font-size of the text, the line-height, the width of the div.
Lets say one screenheight can output 2000 characters on your website
If you count 4000 characters you have 2 screenheigths.
954 characters = almost half of a screenheight ...
i have used this method once to calculate the amount of ads i could implement in the sidebanners on a blog styled website with mainly textcontent on it ...
The height of a vertical ad was about one screenheight. If the text that needed to be outputted was 7000 characters i knew i had room for 3 ads ...