#native_company# #native_desc#

Create An Online Photo Gallery, Part 2

By PHP Builder Staff
on April 10, 2012

Miscellaneous Functions
Those are the functions that actually do the brunt of the work. The functions that I will go over here are required, but they do much less grunt work and more clean-up. First, we know we have an error function, and we also know we have a clean() and check_img function. The last one is our crumbs function. I’ll start with the easiest one first: clean.
Code For: clean()
function clean($txt){   // Small function to clean up the names   // Changes underscores to spaces   $text = str_replace(‘_’, ‘ ‘, $txt);   return $text; }
What we’re doing here is taking the value of $txt and replacing all of the underscores (_) with spaces so that when displayed, the names look like “My Photo Gallery” and not “My_Photo_Gallery”. Relatively minor, but nice to have none-the-less.

The next function helps us display errors to the user. Basically, we create a row and column, and tell the user that there was an error.
Code For: throw_error()
function throw_error($title, $message, $php, $debug = FALSE){   // Displays a small error message to the user if something goes wrong.   // A public function able to be called from anywhere within the class and   // customized to specific ideas.   /*   DEBUG mode is set to “off” (FALSE) by default. Only turn “on” (TRUE) if   you are having problems and need to see the exact php output in order to   solve your problems.   */   $this->error = ‘<;tr><;td><;span style=”color: #f00; font-size: 14px;”>’.$title.'<;/span><;br>’;   $this->error .= ‘<;span style=”color: #000; font-size: 10px; background: #f00;”>’.$message.'<;/span>’;   if($debug === TRUE){     $this->error .= ‘<br><span style=”background: #ccc; color: #000;”><b>PHP Error Output</b><br>’.$php.'</span>’;   }   $this->error .= ‘</td></tr>’;   echo $this->error; }
It’s pretty easy to understand what we’re doing here…we’re generating an error message for the user. When we use the throw_error function in our code (to actually throw an error) we pass along a few values, including the title and message. We can also display the last php error (which will be held in $php_errormsg–we’ll talk about this later), but first we have to decide if we want or need to display it in the first place. Remember that this option is set to FALSE by default.

Now we’ve got the information passed to the function, so it’s just a matter of formatting it in a nice way to tell the user that something went wrong. I prefer to display the output like so:

Error Title
Error Message Goes Here
PHP Error Output
PHP Generated error message goes here

That’s how the output will look to your user if an error is encountered. Since there is very little chance of that–we can be assured–it shouldn’t show up. We’ve got the clean() and throw_error() functions taken care of, so there are just a few more small functions and we’ll be ready to go.

One of the more important functions is the check_img function. This function checks to make sure that the file that is in our list is actually an image. We use the mime_content_type() function to return the type of image it is. We define our list of proper image formats in an array. If the mime-type is in our array, it’s a photo that we want to display!! If not, we just ignore it.

Code For: check_img()
function check_img($img){   // Checks to make sure that the file is actually an image, not a document or anything like that.   // Uses mime types. If you don’t have mime-magic enabled on your server, ask your admin to enable it.   $img_types = array(‘image/gif’, ‘image/jpg’, ‘image/jpeg’, ‘image/png’, ‘image/bmp’, ‘image/x-windows-bmp’);   // If the mime type is in our array of image mime-types, return true, else return false   if(in_array(mime_content_type($img), $img_types)===TRUE){     return TRUE;   }   else{     return FALSE;   } }
Easy enough right? On the next page, I’ll go over the last three items before we test our photo gallery. Hopefully you’ve been testing along the way for syntax errors. If you’ve been “copy & pasting”, you shouldn’t have to worry about syntax errors, however, it’s always good to check.