Can we measure height of a div using php? - php

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 ...

Related

Many many css backgrounds performance

I am trying different methods to turn an image into divs that represent each pixel.
One method has been using php imagecolorat to create divs with a background-color which works well.
However what if I had thousands of divs width:1px;height:1px; each with the same css background-image with incremented background position?
What performance hit would this have? Ie: does the browser draw the entire image many times or only the section that is visible as the background? Note the original image is several hundred kb.
Thanks
Only the section of the image should be drawn, however the drawing operation will be called as many times as the amount of pixels, so that will be a performance hit. Did you try writing any test code and measuring how it works?
Thousands of divs will hurt your performance no matter what, because the browser has to parse each div, add it to the DOM, then render it.
The actual performance will be different on different browsers. Only the part of the image being requested will be drawn, but the rendering function that draws the image will be called for EVERY div. It's more or less whether the specific browser's implementation can render one pixel of an image faster or parse a string and print the image. If the browser has the image in-memory, the difference might be moot. But if the browser has to open the image file every time, then the disk access will kill you.
That being said, I'm willing to bet there are better ways of doing what you are trying to do. You can get the pixel the mouse clicked/hovered/etc using JavaScript, and then add an absolute div at that specific point to create the "filter" you are talking about. Furthermore, if two pixels next to each other get tagged, you can just expand the former div, rather than creating a new one, which will help the performance by a lot
In other words, look into just loading the image with an img tag, and then using JavaScript to do what you are trying to do.

Generating a star rating automatically?

I'm trying to find some way to convert a number 1 - 5 into a star rating, which can also accomodate decimals, like 4.3 or 3.34, and be very precise (like on Amazon.com). The rating doesn't have to be usable, it just has to be a static image. Any help?
I'm fine with using PHP or jQuery, whichever it needs to be.
You could create an image that is white with transparent star cutouts. Then place that over the top of a yellow div of a given length. Lets say your image was 100px wide. If you had a 3 star rating you would make the yellow div 100*(3/5) and 3 of the stars would be filled in.
EDIT:
A similar idea. You could have an image with all 5 stars on it. Place this image within a div with overflow:hidden. Then you set the width of the div the same way as above. The smaller the div the less stars you'll see.
EDIT2:
Fiddle just for fun. All the granularity you could ask for.
http://jsfiddle.net/qFMyC/
This must be sufficient for your purpose:
http://www.fyneworks.com/jquery/star-rating/#tab-Testing
I wrote this ages ago. I was obsessed with not using JS. It needs some work, but I think it's exactly what you're looking for.
http://yefomit.com/internet/simple_css_rater
I'm hoping you mean turning the 1 -5 value into an actual star representation.
It depends how granular you want to be, I think Amazon's is accurate to tenth's (0.1), this is probably achieved server side to save on client load and take advantage of caching the images; but it could be done client side.
#kingjiv posted a good client side solution but for server side:
If you're not too worried about granularity you could create 11 images 0, 0.5, 1, 1.5 etc. etc. and pass the value into the image src attribute. This would serve the right image and they'd be nicely cached for re-use over and over on your pages.
If you want to be really granular though you could create them in code server side, cache them on creation each time ready for quickly serving afterwards. With this method you could go to whatever degree of accuracy took your fancy.
For PHP I'd recommend looking at Imagik
What I have done in the past is to create 2 divs and put them on top of each other.
On the bottom div you set the width equal to the width of 5 stars. So, if your star is 50px wide, your bottom div is 250px wide.
On the top div you use the same star with a different color. Then you set the width of that div in php or javascript to 50 * $decimalStarRating.

PHP Determining Font Size based off of width

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.

Splitting a long page into a number of pages

I want to pull a dynamic content, which consists of a long text input with some images, into a div with a fixed width (300px) and height (1000px), the challenge is I cannot use overflow: auto in css when the content's length is exceeding the div's height (1000px), instead, I am asked to split the long content into pages with a pagination.
Is it possible to achieve with PHP or do I have to use javascript (jquery)?
I was thinking to count the number of characters and splitting them, but it doesn't seem correct when the content comes with different sizes of images...
Any ideas??
This might be very complicated(I'd like to say "impossible") to do it on the serverside, because there are too many clientside effects that can't be calculated(browser-default-settings for margins, paddings, line-height, font-size and user-setting for zooming), I would prefer to do this on clientside.
I made a little example using jQuery: http://jsfiddle.net/doktormolle/XwUuA/
It takes the childnodes of the target-element, and wraps them into new elements which have the same dimensions like the target-element(as long as the height of the wrapper does'nt exceed the height of the target-element).
Maybe it's useful to you(It's a draft, of course there still has to be worked on it to match your needs)
You could use PHP. Find out how many characters you can get per line, and how many lines of characters will fit in your div. Then, with PHP, count characters, divide by characters/line, then you'll have how many lines your text will take up. Then you can use getimagesize() to get an images dimensions, and go from there.
See the PHP function for more info.
I wanted to do something similar with HTML but in a C# Windows Forms application.
What I wanted to do was to generate some contents based on some database tables and send them out to the printer. The contents had to fit into A4 papers.
After lots of trial and error I measured the maximum size of the contents based on their size, place etc. and wrote the numbers in the CSS portion of my HTML.
With that I could get a nice result. Still some slight errors on some inputs, but that worked for me!

Pagination of text from xml file onto html page

O.K. so I'm developing a website to feature my fiction writings. I'm putting all of my documents into XML files, pulling and parsing them from the server with PHP and displaying them on the page. You can visit the page here for an example.
As implied from the background image, What I would like to do is take the text and split it into two columns, (with the text from the first spilling into the second), then allow for the overflow to be paginated so that there is no scrolling necessary. In other words, I'd like for the text to read like a book with the paging based on how long the body of the XML document is.
I would like for this to be done on the server side using PHP or something similar. Is there a way I can do this with an xsl stylesheet or a server-side script? I've been looking everywhere and can't seem to find anything.
Any help is appreciated.
Mr. Mutant
This is a surprisingly hard problem in general, and it's one you'll have no end of trouble with if you try to do it on the server. The problem with paginating HTML text is that where the page breaks go are entirely contingent on the client. The server doesn't know the client's screen resolution, font selection, or window size, and apart from the text itself those are the dependent variables for the problem.
I'd be surprised if at this point there weren't some jQuery library that just does this, but when I had to implement it myself about 7 years ago, here's the approach I took:
Create a div for each column. Each one contains the entirety of the document text. Style the divs with fixed line height. Put the column divs bottom in the document's z-order. Now you can lay out the rest of the page, leaving holes of known size in the layout that the divs can show through, and by manipulating the vertical position of each div you can control which line is the first to appear inside a given hole.
You can then let the client manipulate the font size, and as long as you recalculate the height of the holes and then reposition the divs properly, it will all magically work.
There may be ways of doing this in HTML5 that are easier; I would definitely look into that.

Categories