GENERATING THUMBNAIL IMAGES select the appropriate function. Insert (Php web hosting)
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.