Archive for January, 2008

795Chapter 42GraphicsAlthough we won t show it as a (Sex offenders web site)

Thursday, January 17th, 2008

795Chapter 42GraphicsAlthough we won t show it as a separate listing, we took a copy of Listing 42-7, changed thefunction name argument from spiketo top-hat, and renamed the file fractal2.php. Theresulting image is shown in Figure 42-4. Figure 42-4:Fractal 2Creating and displaying these images can be time consuming and the more so the more linesegments are created. Your Web server may time out while the creation is happening. Youroptions then are to decrease the number of generations in the fractal code or to raise thetimeouts in your Web server or PHP configuration files. Tweaking fractal code is definitely an art, and your humble authors are not particularly goodartists. We wish you luck in improving on our images. For a much more extended example of producing graphics with gd, see Chapter 48. Gotchas and TroubleshootingCode to produce images can be especially difficult to debug, because some of the simplesttricks (for example, diagnostic print statements) can t be used as easily. What follows is a listof symptoms you may encounter in running gd-enabled PHP code and some things you cantry to correct them. Cross- ReferenceCaution47
You want to have a cheap webhost for your apache application, then check apache web hosting services.

794Part IVConnectionsListing 42-6(continued) $path = add_point_to_path ($path, make_point(495, (Hosting web)

Thursday, January 17th, 2008

794Part IVConnectionsListing 42-6(continued) $path = add_point_to_path ($path, make_point(495, 395)); $path = add_point_to_path ($path, make_point(5, 395)); $path = add_point_to_path ($path, make_point(5, 5)); return($path); } ?> Now we can combine all these elements and actually make images. Listing 42-7 shows the filethat produced our original example in Figure 42-3. After loading all the functions from theincluded files, this code creates a gdimage of specific height and width and allocates colorsinto that image. (The background is white, and the lines are black.) The fractal creation code starts off by creating a standard rectangular path (containing five points and, therefore, four [implicit] line segments). It then passes this off to the transform_pathfunction, asking it to return the path that results from applying the spike() function to the rectangle four times. The rectangle path starts with four line segments, andevery segment is itself replaced by four segments. So the four successive iterations have 16segments, 64 segments, 256 segments, and 1024 segments, respectively. Then all that remains is to display the complicated path that we ve generated. We call ourown function display_path()to draw all the lines into the image, send off an HTTP headerannouncing a PNG, call imagepng()for the conversion and output, and then dispense withthe internal gdimage. Listing 42-7:fractal1.php
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

793Chapter 42Graphics} function point_along_segment ($first_point, $second_point, $proportion) { (Best web hosting)

Wednesday, January 16th, 2008

793Chapter 42Graphics} function point_along_segment ($first_point, $second_point, $proportion) { $delta_x = (point_x($second_point) - point_x($first_point)); $delta_y = (point_y($second_point) - point_y($first_point)); return(make_point(point_x($first_point) + $proportion * $delta_x, point_y($first_point) + $proportion * $delta_y)); } function point_off_segment ($first_point, $second_point, $proportion, $proportional_distance) { $delta_x = (point_x($second_point) - point_x($first_point)); $delta_y = (point_y($second_point) - point_y($first_point)); return(make_point(point_x($first_point) + $proportion * $delta_x - $proportional_distance * $delta_y, point_y($first_point) + $proportion * $delta_y + $proportional_distance * $delta_x)); } function make_small_rectangle () { $path = make_path(); $path = add_point_to_path ($path, make_point(75, 275)); $path = add_point_to_path ($path, make_point(375, 275)); $path = add_point_to_path ($path, make_point(375, 125)); $path = add_point_to_path ($path, make_point(75, 125)); $path = add_point_to_path ($path, make_point(75, 275)); return($path); } function make_large_rectangle () { $path = make_path(); $path = add_point_to_path ($path, make_point(5, 5)); $path = add_point_to_path ($path, make_point(495, 5)); Continued47
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

792Part IVConnectionsListing 42-6(continued) (Abyss web server) point_along_segment($prev_point, $point, 0.75)); $path_to_return =

Tuesday, January 15th, 2008

792Part IVConnectionsListing 42-6(continued) point_along_segment($prev_point, $point, 0.75)); $path_to_return = add_point_to_path($path_to_return, $point); } $prev_point = $point; } return($path_to_return); } function top_hat ($path) { // Takes a path and returns a path$path_to_return = make_path(); $prev_point = NULL; foreach ($path as $point) { if ($point && $prev_point) { $path_to_return = add_point_to_path($path_to_return, $prev_point); $path_to_return = add_point_to_path($path_to_return, point_along_segment($prev_point, $point, 0.35)); $path_to_return = add_point_to_path($path_to_return, point_off_segment($prev_point, $point, 0.35, 0.24)); $path_to_return = add_point_to_path($path_to_return, point_off_segment($prev_point, $point, 0.65, 0.24)); $path_to_return = add_point_to_path($path_to_return, point_along_segment($prev_point, $point, 0.65)); $path_to_return = add_point_to_path($path_to_return, $point); } $prev_point = $point; } return($path_to_return);
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

791Chapter 42Graphics// (Web hosting account) and a number of times to

Tuesday, January 15th, 2008

791Chapter 42Graphics// and a number of times to apply the // function. // Returns a path$path_to_return = $path_input; for ($i = 0; $i < $iterations; $i++) { $path_to_return = $function_name($path_to_return); } return($path_to_return); } ?> What we have so far is a way to represent and draw paths composed of line segments, andalso functions that can repeatedly apply transformation functions to these paths. What weneed now is the transformation functions themselves the functions that we pass in thatactually twiddle the locations of points in the data structures. Listing 42-6 shows a set of such functions. The spikefunction takes a path as argument andreturns a path where every two-point line segment has been replaced by a five-point line seg- ment with a spike in the middle. The top-hatfunction does something similar, except thatsix points are involved, and the spike is rectangular. We also include a couple of functions tocreate rectangular paths of standard sizes, to use as starting points. Listing 42-6:path_manipulation.phpFrom our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

790Part IVConnectionsListing 42-4(continued) (Web host) } function display_path ($image, $path,

Monday, January 14th, 2008

790Part IVConnectionsListing 42-4(continued) } function display_path ($image, $path, $color) { static $line_count = 0; $prev_point = NULL; foreach ($path as $point) { if ($point && $prev_point) { $line_count++; imageline($image, point_x($prev_point), point_y($prev_point), point_x($point), point_y($point), $color); } $prev_point = $point; } } ?> Listing 42-5 shows a single function, which, among other arguments, takes the name of a func- tion name to apply. (This function is in its own file because our original version of this codehad more complex transformation functions for more complex fractal examples, removed for reasons of space. We may restore these examples to the code on the Web site at www. troutworks.com/phpbook.) The function transform_pathtakes an input path as first argument, and as second argumentit takes the name of a function that, in turn, is expected to take a path as argument and returna path as a result. The third argument to transform_path()is a number of times that thepath-to-path function should be successively applied to create a new path. The reason thatthis kind of second-order function is useful is that, otherwise, we may find ourselves writing anew looping function every time we wanted to build a new fractal. With this approach, we canbundle the varying part of the fractal code into a function that we pass into transform_pathand avoid duplicating work. Listing 42-5:path_tranform.phpIf you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

789Chapter 42GraphicsWe end up drawing paths by drawing (Web hosting compare)

Sunday, January 13th, 2008

789Chapter 42GraphicsWe end up drawing paths by drawing line segments between all the points in a path. If wewant to draw a simple line segment, we draw a path that has two points in it; if we want todraw a rectangle, then we draw a path that has five points in it (with the starting pointrepeated to close off the rectangle). (We could have made a line segment a primitive entityhere, but paths seemed more concise for our fractal purposes.) Now, how shall we represent points and paths? The easiest way to make lists of things in PHPis to use arrays. So we declare that a point is an array that happens to contain two numbers, and a path is an array that happens to contain a sequence of points. The resulting structuresare multidimensional PHP arrays, but if we define well-named constructor and accessor func- tions, we can forget about that and just write code that acts as though these things are gen- uine datatypes. Listing 42-4 shows such code, which defines the datatypes in terms of functions to createthem (starting with make_), functions to access their parts, and functions to draw them intoan image (starting with display_). Points cannot be drawn and have no display function; paths are drawn by drawing lines between successive pairs of points. Listing 42-4:path_display.phpWe highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

788Part IVConnectionsExample: Fractal images There s a fine tradition (Top ten web hosting)

Saturday, January 12th, 2008

788Part IVConnectionsExample: Fractal images There s a fine tradition of livening up the potentially unexciting topic of line drawing by usingfractals as examples, and your authors are not about to mess with tradition. In addition toshowing how you can produce a complex image programmatically, this kind of example isalso a good fit for PHP because its arrays and loose datatypes make it very easy to build complex data structures corresponding to fractal images, without a lot of declarations. What s a fractal? It s a shape that is self-similar, in that the parts of a fractal have a shape simi- lar to the shape of the whole, and the parts of those parts have a similar shape, and so on. In theory, you can keep zooming into ever-smaller pieces of an ideal fractal, and keep findingthe same patterns repeated. In practice, computer-generated fractals bottom out after somelimited number of generations into nonfractal shapes like simple curves and line segments. An example of the kind of image we re going to create is shown in Figure 42-3. Although it maynot look like it, this image is simply a lot of small line segments with endpoints connectedinto a path. Figure 42-3:Fractal 1Our job is to calculate the endpoints of all those line segments and then display them appro- priately as a PNG image. We re going to be slightly more ambitious than simply creating a one-off piece of fractal display code and construct a little framework that makes it easy tovary the fractal parameters and to generate new kinds of displays. To start with, we build some data structures to represent the complex shapes that we are dis- playing. We use these data structures both in our intermediate calculations and for drawingthe end result. Let s say somewhat arbitrarily that: .A coordinate point is a pair of numbers. .A path is a list of points.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

787Chapter 42GraphicsEmbedded images from filesOf course, HTML has (Free web design)

Saturday, January 12th, 2008

787Chapter 42GraphicsEmbedded images from filesOf course, HTML has had the tag for a long time. This enables you to embed an imageby specifying its file path or URL, like this: This works with static image files, but there is no reason why the image can t have beenrecently created. So you can have a script that 1) creates an image, 2) writes the image datato a local file, and then 3) produces HTML with an appropriate tag referring to the filethat you just made. The only drawbacks to this approach are 1) you re introducing file writes, which may be time- consuming, into the page-generation process, and 2) you need to figure out what to do withthe files after you are done with them. There is one situation this approach is perfect for, however, which is creating and caching images that represent a finite set of possibilities. In this case, you have some way to map from a situation to an image filename. Whenever adisplay situation arises, you check to see if you already have the appropriate file if you do, you simply refer to it by using an tag, and if not, you create the image, write it out to afile, and then refer to it. Eventually, you should need to do no more creation. You can see a page created this way at a just-for-fun site that we made a few years ago. Inwww.sciencebookguide.com/sizescales.html, there is a bar graph in the top part of thepage, and then a scale legend at the bottom, which was a gd-generated GIF. The text and tickmarks of the scale legend depend on the exact bar graph data that is being displayed, but thereare only a limited number of cases. So, long ago, we auto-cached all the possible images, andever since the displays have been static. (This is a good thing, of course, since gdquiterightly no longer supports the GIF format. Sometime soon, we may get around to replacingthe GIFs with PNGs.) Embedded images from scriptsFinally, there is no reason why you cannot have a standalone generated image, as in the sec- tion Full-page images, but, in turn, embed that URL in a different dynamic page via an tag. The only difficulty lies in how to communicate necessary data to the dependent page. You may, for example, have an embedded image tag like this: where ballpage.phphappened to return PNG images of colored balls in various positions inthe image. There is a gotcha lurking here because both Web servers and browsers sometimes pay atten- tion to the suffix of the served file, and in different ways. You may need the suffix of ballpageto be .phpto let Apache (for example) know that the server-side code should be interpretedas PHP (although this behavior can be controlled with configuration files). Some brokenbrowsers, however, may insist that a file that ends in .phpcannot be an image despite theheaders we are sending. This technique requires some cross-browser testing to make surethat your intended users are seeing the same thing you are. Now it s high time to move on to an example of using gdto create images.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

786Part IVConnectionsTable 42-1(continued) Type ExamplesNotesText (Frontpage web hosting) functionsImageString(),takes as arguments

Friday, January 11th, 2008

786Part IVConnectionsTable 42-1(continued) Type ExamplesNotesText functionsImageString(),takes as arguments an image, a font ImageLoadFont()number, x and y coordinates, a text ImageStringstring, and a color. If the fontnumber is between 1 and 5, one of the five built-in fonts is used todraw the string in the given color. Anumber greater than 5 indicates aresult of loading a custom font withImageLoadFont(). Exporting functionsImagePng(),These functions convert the internal ImageJpeg()gdimage to the relevant imageformat and then send to output. Ifonly one argument (an image) isgiven, the image is echoed to theuser; if an additional path nameargument is given, the destination is a file. Image-destruction functionImageDestroy()Takes an image argument and freesall resources associated with theimage. Images and HTTPBefore the user s browser can display an image appropriately, it has to know that an image iscoming, and what the image format is. So it is, unfortunately, not sufficient to simply embed acall to ImageToPng()in your generated HTML and have an image show up. You essentiallyhave three choices in regard to intermixing images with PHP-generated HTML. Full-page imagesYou can make the entire generated page an image. In this case, you need to send an HTTPheader before the image data, announcing that an image of a certain type is on the way. You may, for example, have lines such as the following near the end of your script: // … code to create image in $imageheader( Content-type: image/png ); // announcement to browserimagepng($image); // sending actual PNG-converted image dataimagedestroy($image); // freeing resourcesThis approach has the benefit that you can use any kind of information, including POSTarguments, to decide what the image should contain. The downside is that the resulting pagecan t contain any conventional HTML. In fact, you need to be careful that no textual output issent from your scripts before the header and image because this causes content to be sentprematurely. In this case, you get a Headersalreadysent…error.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.