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.
Related
I had created one wordpress site. I want to make all post downloadable by converting it to image.
I tried using canvas but didn't succeed.
Can any one suggest me better working way on wordpress which lets me convert my post to image and make it downloadable?
I want to make post covered with specific div so that i can define size of content to be downloaded.
Like this HTML2CANVAS but I am unable to do.
PS-I have very small size of content in every post
I think your options are
Use some third-party service, such as http://web-capture.net/ or https://www.url2png.com . Most of them, especially the ones with API that you can call on-demand, will cost you, but there are free alternatives.
If you have access to linux console and some basic knowledge about it, the best approach is to run a real browser (if you're using a headless server, use Xvfb) with your post URL and make a screenshot with ImageMagick. You can crop the image to remove browser header etc. A working-grade explanation here http://www.leonardteo.com/2011/07/taking-server-side-screenshots-of-websites/ .
In both cases PHP will be just the trigger, whether it will call third-party API or your local shell script.
I'd also suggest to avoid JPEG format as it doesn't really play well with text. Use PNG instead.
You may try rendering the text with imagettftext() as #Progrock suggested, but this will be a huge pain, because you obviously have text with more than one line. First you need to determine the width of your image, then use imagettfbbox() to roughly estimate how many characters you can fit into one line, split your text into chunks of that size and write then one by one, adding the Y coordinate. Bonus points if you need paragraphs here... Make sure you're using monotype font, because it won't ever work properly with variable-width letters. look through comments here http://php.net/manual/en/function.imagettftext.php.
My advice - stick with the browser :) You can resize the browser window and crop the extra part.
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 need to store in a database pieces of text and the size of the layer that will wrap the text in the browser screen (I cannot build the layer in the client side - it is a requirement of the project). The whole piece of text must fit into the square layer properly. Users will be able to use different font size and font families from a small group of options and we'll know the selection at the time of computation.
Right now, I compute the layer volume based in the theory that every character size is its font-size pixels height and 50% of the font-size width. With the 50% value, I got the best approximation for the average cases, but it is still not a good solution because it cuts pieces of text or leaves too much blank space at the end. And it's even worst with some wider font-types.
Any idea on how to approach this problem?
Given how easy it is for users to override font choices in the browser, at best this would only be a "best guess" attempt, but you could use GD to draw the text and then compute a bounding box for it using imagettfbbox().
But this only supports simple text, so if you're doing "complicated" stuff with boldface/italics, variable sizes, etc.. then you're SOL for the most part.
Given the requirements, the only way to do this 100% guaranteed is to:
Render the image on the server -- this can be to a normal image, to a vector format such as SVG, or even (more extreme) to Flash -- and use the generated output on the client in some form which guarantees precise rendering as any CSS rendering/local-font issues may differ from the server-generated values and is thus unreliable!
The exact dimensions of the box are thus known independent upon local font rendering issues as they depend only upon the servers "view".
Happy coding.
Edit: I was attempting to describe this problem using the markup I'm currently using, but that seems to be causing confusion. Look at the edit history if you'd like to see that should the question be unclear
I would like to write a php script generating an image from user submitted content.
The content is a block of text of undetermined length on a background. The text could be anything from a couple words to a few sentences.
There could be anywhere from 1 to 6 of these blocks of text that will vary in size. So, since the text varies in length, the container/background will vary in height. And since I could have multiple blocks, the overall image will vary in height.
Given that I need to dymically size so much of this (the image itself, the text containers, possibly font size), and I really don't even know where to start with the GD library, my first questions are:
Is this possible?
If possible, is it feasible to implement without a ton of headache?
Is there a cross-platform alternative (preferably no flash, and likely no <canvas>)?
There is a PHP function that gets the image size and returns it as an array.
$image_data = getimagesize($image_path);
returns an index array with 4 elements:
0: width in pixels
1: height in pixels
2: type as integer (1-GIF, 2-JPG, 3-PNG, 4-SWF, 5-PSD, etc)
3: the string " height='(height)' width='(width)'
You can use the $image_data[0] and $image_data[1] to return the proper width/height, which you can use to dynamically size the containers and whatnot.
The other issues you have are somewhat beyond the amount of time I can put into a post at the moment. Perhaps later I can expound upon those. Good luck.
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 ...