understanding php processing and page output order [Solved]

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
vanderlay
Forum Newbie
Posts: 10
Joined: Tue Oct 24, 2006 5:20 am

understanding php processing and page output order [Solved]

Post by vanderlay »

Hi All,

I have been using php for some time now and have got the hang of fairly high level processing including dynamic images from mysql output that leads me to the point of needing to better understand the processing /logic order of the php engine so i can increase the speed of my scripts.

so the Q i have is how is the following rendered by php,

template.php
require -> mysqldata.php to get info from db
require -> imagegd.php takes mysql data and var from template.php and resizes image then outputs to browser(no save)

Q1. a)does template.php have to wait for mysqldata.php to be fully rendered/computed before moving on to imagegd.php or
b)does imagegd.php get called immediately and has to wait for mysqldata.php to supply the info needed (i presume the (a) is correct)

Q2. once imagedgd.php has been called and an image generated is that image pushed to the browser or is the template.php rendered as html sent to browser and the browser then requests the imagegd.php script and the image is generated.

Q3. is there a good way ( i have tried ob_start("ob_gzhandler"); ) to buffer the page and wait for all images but it only seems to work on text/layout rather than graphics/fla etc.

thanks
Art
Last edited by vanderlay on Wed May 16, 2007 6:10 am, edited 1 time in total.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Re: understanding php processing and page output order

Post by kyberfabrikken »

vanderlay wrote: Q1. a)does template.php have to wait for mysqldata.php to be fully rendered/computed before moving on to imagegd.php or
b)does imagegd.php get called immediately and has to wait for mysqldata.php to supply the info needed (i presume the (a) is correct)
PHP is single threaded. It will execute everything chronologically, each operation blocking for the next. So that would be A.
vanderlay wrote: Q2. once imagedgd.php has been called and an image generated is that image pushed to the browser or is the template.php rendered as html sent to browser and the browser then requests the imagegd.php script and the image is generated.
What do you mean by push to the browser? You can't output an image and HTML from the same script.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Please don't double post.....
vanderlay
Forum Newbie
Posts: 10
Joined: Tue Oct 24, 2006 5:20 am

Post by vanderlay »

thanks kyberfabrikken,

by pushed i mean is the image generated in the first pass and sent with the rest of the code or

is it generated and left in a tmp storage area until the html goes to the browser and the browser asks for that image or

is the image not generated but waits until the browser parses the html and requests the url (imagegd.php) and then generates the image

hope that makes sense

Art
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

When you enter the address of a file in your browser, the browser will generate a HTTP request to the server, asking for that file. Assuming that the file has the extension .php, the server will load the file, and invoke the PHP interpreter, feeding the file into the interpreter. THe interpreter will return some output, which the server will then send back to the browser.

Normally, PHP scripts will generate HTML, which the browser will then render and display. When the browser does this, it will recognise any tags, which requires external resources; Such as <img> tags. For such a tag, the browser will generate new HTTP request, asking for the file. For example, suppose you have the following file at your server:

foo.php

Code: Select all

<?php
echo "<html><head><title></title></head><body>";
echo "<img src='foo.gif' />"
echo "</body></html>";
?>
You can request that in a browser, by typing the name of the file. For example: http://localhost/foo.php. This will result in the following:
  • The browser sends a request to the server at localhost, asking for the file foo.php
  • The server will return the output of foo.php. This ends the request.
  • The browser renders the HTML.
  • The browser realise, that there is an <img> tag.
  • The browser sends a new request to the server at localhost, asking for the file foo.gif
  • The server will return the file foo.gif. This ends the request.
  • The browser will display the image.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Just a quick one. A web page is composed of numerous resources. The HTML the browser renders, the CSS it uses to style the page, and the images the HTML tells the browser to display. Each resource requires a separate roundtrip to the server, i.e. you don't get the HTML and Image in one request, you need two. The first to grab the HTML, and the second to the URL address specified in the <img> tag for the image.

Assuming you want PHP to output an image, then the image must be output from a URL specified in a HTML <img> tag. So...

1. Browser requests the page URL.
2. PHP processes the request and returns the HTML. This HTML has a <img> tag pointing to the image's URL (could be another PHP script for serving the image).
3. Browser requests the Image URL.
4. PHP processes this separate second request, outputs image (with the correct Content-Type header - check that).
5. Browser displays the received Image as the original HTML specifies (with a little help from any CSS or inline styling).

You description is a little vague on what's done. If the page level request generates this image, then you need to store it temporarily for that second browser request. All you'd do is add the temporary Image url to the template (the HTML). Once the browser requests the temporary Image url the page is done.

It's probably better to store the Image somewhere away from the document root (like /tmp or a standard location away from the document root) and let a PHP script fetch the Image file, serve it to the browser, and then delete it immediately afterwards (or leave it cached for a short time if that's usefull for any repeat requests). Less writing your application does in the document_root, the better - on a shared host that tends to require 777 permissions for Linux (which is rarely a good thing since that's a world writeable permission).
vanderlay
Forum Newbie
Posts: 10
Joined: Tue Oct 24, 2006 5:20 am

Post by vanderlay »

[s]thx[/s] thanks guys, now it all makes sense, funny how the fundamentals are sometimes missed in the learning curve but they will eventually be needed.

So if you were having a page with several (10-20) gd(createfrompng & resize) generated images, would you
A)do them on the fly ie wait for the page to request them once the php has been sent to the browser - <img src="image.php"> or
B)have the php generate them on the first pass and store them awaiting for request after <img src="img.png">

again, [s]thx[/s] thanks for your detailed help.
Art
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post by kyberfabrikken »

vanderlay wrote: So if you were having a page with several (10-20) gd(createfrompng & resize) generated images, would you
A)do them on the fly ie wait for the page to request them once the php has been sent to the browser - <img src="image.php"> or
B)have the php generate them on the first pass and store them awaiting for request after <img src="img.png">
That depends. If the image is only valid for a very short time, I would generate it on the fly. If the image could potentially be requested several times, or by multiple users, I would generate it and store it. The latter takes a little bit more work, but it's performing much better.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

vanderlay wrote: So if you were having a page with several (10-20) gd(createfrompng & resize) generated images, would you
A)do them on the fly ie wait for the page to request them once the php has been sent to the browser - <img src="image.php"> or
B)have the php generate them on the first pass and store them awaiting for request after <img src="img.png">
I agree with kyberfabrikken. Method I often use is to have a central storage place for the "generated image". If the file doesn't exist when required generate it. This means that in a site where images are constantly changing you could delete all "generated" images and the system would recreate them as required.
vanderlay
Forum Newbie
Posts: 10
Joined: Tue Oct 24, 2006 5:20 am

Post by vanderlay »

great stuff, will try the suggestion of storing then generate if needed or on a cron schedule. After thinking about your input I have also added an ajax interface that may reduce the need for as many images refreshed as often.

thanks again for you quality input.


Art :lol:
Post Reply