Archive for February, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY // (Disney web site)

Friday, February 15th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY // get image name and build full pathname if (!empty($_POST[’pix’])) { $original = SOURCE_DIR.$_POST[’pix’]; } else { $original = NULL; } // abandon processing if no image selected if (!$original) { $result = ‘No image selected’; } // otherwise resize the image else { // begin by getting the details of the original list($width, $height, $type) = getimagesize($original); // calculate the scaling ratio if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) { $ratio = 1; } elseif ($width > $height) { $ratio = MAX_WIDTH/$width; } else { $ratio = MAX_HEIGHT/$height; } // strip the extension off the image filename $imagetypes = array(’/.gif$/’, ‘/.jpg$/’, ‘/.jpeg$/’, ‘/.png$/’); $name = preg_replace($imagetypes, ‘’, basename($original)); // create an image resource for the original switch($type) { case 1: $source = @ imagecreatefromgif($original); if (!$source) { $result = ‘Cannot process GIF files. Please use JPEG or PNG.’; } break; case 2: $source = imagecreatefromjpeg($original); break; case 3: $source = imagecreatefrompng($original); break; default: $source = NULL; $result = ‘Cannot identify file type.’; } // make sure the image resource is OK if (!$source) {
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

GENERATING THUMBNAIL IMAGES In spite of its destructive (Managed web hosting)

Thursday, February 14th, 2008

GENERATING THUMBNAIL IMAGES In spite of its destructive name, imagedestroy() has no effect on the original image, nor on the thumbnail that s just been saved to file. The function simply frees up the server memory by destroying the image resources required during processing. 8. Before testing the page, you need to add some code just after the opening tag to display the message reporting the outcome like this: $result

“; } ?>

9. Save create_thumb.php and load it in a browser. Select an image from the drop-down menu and click Create thumbnail. If all goes well, there should be a scaled- down version of the image you chose in the thumbs subfolder of upload_test. Check your code, if necessary, with create_thumb03.php in the download files. Resizing an image automatically on upload Now that you have a script that creates a thumbnail from a larger image, it takes only a few minor changes to merge it with the file upload script from Chapter 6. Rather than build the entire script in a single page, this is a good opportunity to use a PHP include (includes were covered in Chapter 4). PHP Solution 8-3: Merging the image upload and resizing scripts The starting point for this PHP Solution is create_thumb.php from the preceding section, together with upload.php from Chapter 6. Alternatively, use create_thumb03.php and upload_thumb01.php from the download files for this chapter. The finished scripts are in create_thumb.inc.php and upload_thumb02.php. 1. In create_thumb.php, select the entire PHP block above the DOCTYPE declaration. Copy the selected code to your computer clipboard, and paste it inside a blank PHP page. The new page should contain PHP script only; you don t need a DOCTYPE or XHTML skeleton. Save the page in the includes folder as create_thumb.inc.php. 2. Remove the comment on line 2 together with the conditional statement that surrounds the script (don t forget the closing curly brace just before the closing PHP tag). You should be left with the following: Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Place (Web hosting ecommerce)

Wednesday, February 13th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Place this code under the second comment: // save the resized copy switch($type) { case 1: if (function_exists(’imagegif’)) { $success = imagegif($thumb, THUMBS_DIR.$name.’_thb.gif’); $thumb_name = $name.’_thb.gif’; } else { $success = imagejpeg($thumb, THUMBS_DIR.$name.’_thb.jpg’, 50); $thumb_name = $name.’_thb.jpg created’; } break; case 2: $success = imagejpeg($thumb, THUMBS_DIR.$name.’_thb.jpg’, 100); $thumb_name = $name.’_thb.jpg created’; break; case 3: $success = imagepng($thumb, THUMBS_DIR.$name.’_thb.png’); $thumb_name = $name.’_thb.png created’; } if ($success) { $result = “$thumb_name created”; } else { $result = ‘Problem creating thumbnail’; } As with the earlier switch statement, you need to check whether the server supports GIF. Even if the server can read GIF files, it might not be able to create them, so case 1 begins by using function_exists() to establish if it s safe to use imagegif(). If it is, imagegif() is used to save the thumbnail to file. If GIF creation isn t supported, the else clause uses imagejpeg() to save it as a JPEG file with a quality of 50. Note that function_exists() takes the name of the function as a string with- out the final parentheses like this: if (function_exists(’imagegif’)) // RIGHT if (function_exists(imagegif())) // WRONG The functions that save the image to file return a Boolean true or false, which is stored in $success. A message reporting the outcome is stored in $result. 7. All that remains is to remove from memory the two image resources that you ve been working with. Place this code under the final comment: // remove the image resources from memory imagedestroy($source); imagedestroy($thumb);
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Christian web host - GENERATING THUMBNAIL IMAGES Figure 8-1. The x and

Wednesday, February 13th, 2008

GENERATING THUMBNAIL IMAGES Figure 8-1. The x and y coordinates can be used to extract part of an image. Consequently, when using PHP to create a thumbnail, the x and y coordinates aren t really relevant. You simply set all four coordinates to the top-left corner (0), and use the actual width and height of the original image. This makes a thumbnail of the entire image. So let s get back to the code. 5. Insert the following line of code immediately beneath the first of the three com ments that you inserted at the end of the previous step: // create the resized copy imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, . $thumb_height, $width, $height); Note that you don t need to assign the result of imagecopyresampled() to a vari able; $thumb now contains the scaled-down image, but you still need to save it to a file. 6. The functions that save an image to file need to know the file type. So this means using another switch statement to select the appropriate function: imagegif(), imagejpeg(), or imagepng(). Each function takes the following two arguments: The image resource being held in memory The pathname of the file you want to save the image to You can build the pathname with THUMBS_DIR, followed by $name (the original file name minus the extension), plus _thb.gif, _thb.jpg, or _thb.png, as appropriate. This results in the thumbnail for kinkakuji.jpg being saved in the thumbs sub- folder as kinkakuji_thb.jpg. The function that creates JPEG files takes a third, optional argument: an integer between 0 and 100 to indicate the quality of the image. JPEG compresses the image, so a lower number produces a smaller file size, but of lower quality. If you omit the third argument, the default is 75. Except when saving a GIF in JPEG format, it s a good idea to specify 100. You can always reduce the quality later if the file is too big, but you can t restore picture quality once it s been reduced. 221
Check Tomcat Web Hosting services for best quality webspace to host your web application.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY //

Tuesday, February 12th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY // create the resized copy // save the resized copy // remove the image resources from memory } } } If $source is false, there must be a problem with copying the original, so there s no point in continuing. However, if a valid image resource exists, the else statement is executed. It begins by multiplying the width and height of the original by the scaling ratio. Because the dimensions of an image must be integers, the calculation is passed to round(), which returns the nearest whole number. Then you need to create the image resource for the resized copy. This is done by passing the width and height of the thumbnail to imagecreatetruecolor() and storing the resource as $thumb. Just three more steps remain, as indicated by the three comments at the end of the new code. Before moving on to them, let s pause to see how the copy is actually created. The function that creates a resized copy of an image is imagecopyresampled(), which takes wait for it ten arguments! While this sounds horrendous, the arguments fall into five pairs as follows: References to the two image resources copy first, original second The x and y coordinates of where to position the top-left corner of the copied image The x and y coordinates of the top-left corner of the original The width and height of the copy The width and height of the area to copy from the original The only time you need to worry about the x and y coordinates is when you want to extract a specific area, rather than copy the whole image. The coordinates are measured in pixels from the top left of the image. Figure 8-1 shows the effect of the following code: imagecopyresampled($thumb, $source, 0, 0, 170, 20, $thumb_width, . $thumb_height, 170, 102); The x coordinate of the original image is set at 170 pixels from the top left, and the y coordinate is at 20 pixels. By setting the width and height of the original to 170 and 102, respectively, PHP copies only the area outlined in white. Although this is impressive, you can probably already see a problem: you need to view the image first to find the best area to extract and get the correct coordinates. Although you could use a pixel ruler, it s messy. You get a much more satisfactory result in a graphics program, such as Photoshop, where you can crop and rescale the image quickly and accurately.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

GENERATING THUMBNAIL IMAGES select the appropriate function. Insert (Php web hosting)

Monday, February 11th, 2008

GENERATING THUMBNAIL IMAGES select the appropriate function. Insert the following code immediately after the code in the previous step: $name = preg_replace($imagetypes, ‘’, basename($original)); // create an image resource for the original switch($type) { case 1: $source = @ imagecreatefromgif($original); if (!$source) { $result = ‘Cannot process GIF files. Please use JPEG or PNG.’; } break; case 2: $source = imagecreatefromjpeg($original); break; case 3: $source = imagecreatefrompng($original); break; default: $source = NULL; $result = ‘Cannot identify file type.’; } } } The switch statement checks the number stored in $type and creates an image resource called $source using the correct function for the file type. All servers should support imagecreatefromjpeg() and imagecreatefrompng(), but a server using an older version of GD might not support imagecreatefromgif(). That s why I have used the error control operator (see Preventing errors when an include file is missing in Chapter 4) if $type is 1 (a GIF file). If the server can t handle GIF files, $source will be false, so a suitable error message is stored in $result. A different error message is created if $type is not 1, 2, or 3. 4. After making sure that the image resource for the original is OK, you can go ahead and create the thumbnail. Insert the following code immediately after the switch statement from the previous step: $result = ‘Cannot identify file type.’; } // make sure the image resource is OK if (!$source) { $result = ‘Problem copying original’; } else { // calculate the dimensions of the thumbnail $thumb_width = round($width * $ratio); $thumb_height = round($height * $ratio); // create an image resource for the thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); 219
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE (Web site design and hosting) EASY 1.

Monday, February 11th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 1. You no longer need to display the results onscreen, so change the first echo command in step 3 of the previous section like this: if (!$original) { $result = ‘No image selected’; } 2. Delete the three echo commands at the end of the code in step 3, and replace them with the following code: else { $ratio = MAX_HEIGHT/$height; } // strip the extension off the image filename $imagetypes = array(’/.gif$/’,'/.jpg$/’,'/.jpeg$/’,'/.png$/’); $name = preg_replace($imagetypes, ‘’, basename($original)); } } Don t forget that all the code needs to go inside the original conditional state- ment that makes sure the script runs only when the form has been submitted. I ve included the closing curly brace of that statement in the preceding code as a reminder. The first new line of code creates an array of regular expressions to identify the following filename extensions: .gif, .jpg, .jpeg, and .png. The next line uses basename() to extract the filename and passes it to preg_replace(), which searches the $imagetypes array for a match and replaces it with nothing. Let s say $original contains the following pathname: C:/htdocs/phpsolutions/images/kinkakuji.jpg By passing it to basename(), it becomes this: kinkakuji.jpg Finally, .jpg is removed, leaving you with this: kinkakuji This value is stored in $name and can be used to build the name of the resized image. 3. As explained earlier, the first step in working with an image in PHP is to create an image resource in memory. To create a scaled-down copy, you need two image resources: one for the original image and another for the thumbnail image. Let s begin with the original image. The function used to create an image resource from an existing image depends on the file type. Since you stored that information in $type, you can use a switch statement (see Using the switch statement for decision chains in Chapter 3) to
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

GENERATING THUMBNAIL IMAGES As you saw in Chapter (Web hosting colocation)

Sunday, February 10th, 2008

GENERATING THUMBNAIL IMAGES As you saw in Chapter 4, getimagesize() returns an array containing four elements. On that occasion, we were interested only in the fourth element: a string containing the width and height of an image, ready to insert into an tag. This time, it s the first three elements we want: the width, height, and image type. The list() construct lets you assign array elements directly to variables. The array elements are assigned to the variables in the same order. So, the first variable ($width) gets the first element of the array produced by getimagesize($original) in other words, the image s width. The second variable ($height) gets the height of the image, and so on. If you pass fewer variables to list() than the number of array elements, any surplus ones are ignored. The calculation of the scaling ratio is a simple arithmetic calculation. If the width and height of the original image are smaller or equal to the maximum, you don t want to scale the image. So the ratio is set to 1. Otherwise, you divide the maximum by the larger of the two dimensions. If the image is square, the ratio is determined by dividing the maximum height by the height of the original. 4. Save create_thumb.php and load it in a browser. Click Create thumbnail without selecting an image. You should see No image selected at the top of the screen. Then pick an image from the drop-down menu, and test the page again. You should see something like the screen- shot to the right. Try several different images. The scaling ratio should change for images of differ ent dimensions. All the photos in the images folder are JPEG files, so Image type should always be 2. Compare your code with create_thumb02.php in the download files, if necessary. The getimagesize() function returns the image type as a number. You can find a full list at www.php.net/manual/en/function.getimagesize.php. The ones of interest to web developers are the first three, as follows: 1 GIF 2 JPEG 3 PNG The GD image functions handle each type of image differently, so these numbers will be used to create the script s branching logic. 8 PHP Solution 8-2: Creating a scaled-down copy Continue working with the same file. Alternatively, use create_thumb02.php from the download files. The finished script for this section is in create_thumb03.php. 217
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Web site domain - PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 2.

Saturday, February 9th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 2. When the form is submitted, the pix element of the $_POST array contains the name of the image you want to resize. PHP needs to know the full path to the image, so combine the value of the SOURCE_DIR constant with $_POST[’pix’], and assign it to a shorter variable like this (the code goes inside the conditional statement immediately after the four constants inserted in the previous step): // get image name and build full pathname if (!empty($_POST[’pix’])) { $original = SOURCE_DIR.$_POST[’pix’]; } else { $original = NULL; } The static option of the drop-down menu has no value, so you need to check that $_POST[’pix’] isn t empty. If it is, $original is set to NULL to prevent the rest of the script from going ahead. 3. Next comes the script to calculate the scaling ratio. Insert the following code after the code in the previous step (still inside the original conditional statement): // abandon processing if no image selected if (!$original) { echo ‘No image selected’; } // otherwise resize the image else { // begin by getting the details of the original list($width, $height, $type) = getimagesize($original); // calculate the scaling ratio if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) { $ratio = 1; } elseif ($width > $height) { $ratio = MAX_WIDTH/$width; } else { $ratio = MAX_HEIGHT/$height; } echo “Image selected: $original
“; echo “Original width: $width
Original height: $height
“; echo “Image type: $type
Scaling ratio: $ratio”; } Although there should never be any output ahead of the DOCTYPE declaration, echo is being used here simply for testing purposes and will be removed later. When building scripts, it s always a good idea to display the result of a calculation or conditional statement, as it helps confirm you re getting the expected results. The following line of code needs a little explanation: list($width, $height, $type) = getimagesize($original);
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Web site - GENERATING THUMBNAIL IMAGES The page requires buildFileList5.php (or

Saturday, February 9th, 2008

GENERATING THUMBNAIL IMAGES The page requires buildFileList5.php (or buildFileList4.php, if you re using PHP 4), which should already be in your includes folder from the previous chapter. If you don t have a copy, get it from the download files for Chapter 7. Use buildListFile5() for PHP 5 and buildListFile4() for PHP 4. When loaded into a browser, the form looks like the screenshot to the right, and the drop-down menu should display the names of the photos in the images folder, as shown in Figure 7-1 in the previous chapter. Inside the upload_test folder that you created in Chapter 6, create a new folder called thumbs, and make sure it has the necessary permissions for PHP to write to it. Refer back to Establishing an upload directory in Chapter 6 if you need to refresh your memory. Building the script Once you have created the thumbs folder and checked that the drop-down menu in create_thumb.php is displaying a list of images, you re ready to start. PHP Solution 8-1: Calculating the scaling ratio 1. If you have been reading the chapters in order, you ll know by now that the conditional statement above the DOCTYPE declaration checks whether the name attribute of the submit button is in the $_POST array. Since the submit button is called create, the script inside the conditional statement runs only if the form has been submitted. Replace the placeholder comment with the following code: if (array_key_exists(’create’, $_POST)) { // define constants define(’SOURCE_DIR’, ‘C:/htdocs/phpsolutions/images/’); define(’THUMBS_DIR’, ‘C:/upload_test/thumbs/’); define(’MAX_WIDTH’, 120); define(’MAX_HEIGHT’, 90); } The new code defines four constants: the folder containing the original images, the folder where the resized images are to be stored, and the maximum width and height you want the thumbnails to be. You could use ordinary variables, but defin ing constants at the start of a script makes it easy to identify default values and change them at a later stage. Note that the folder pathnames must end with a trail ing slash. If you re using a remote server or a Mac, replace the pathnames just shown with the correct paths to your images and thumbs folders. The download files also use the pathnames for a Windows local testing environment, so you need to make the changes there, too. 215
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.