Home arrow MySQL arrow Page 3 - Online Photo Album Development using PHP and GD: Part 1

Photo Class - MySQL

This article is the first part of a four part series about saving money and space when it comes to showing off your pictures. Frank will be using PHP, MySQL and GD in this series.

  1. Online Photo Album Development using PHP and GD: Part 1
  2. Photo Sizing
  3. Photo Class
  4. To Gif or Not to Gif
  5. Resizing Images
  6. Conclusion
By: Frank Manno
Rating: starstarstarstarstar / 36
April 22, 2004

print this article



Next we define our class:

class GallerySizer{
var $img; // Original image file object
var $thumb; // Thumbnail file object
var $resize; // Resized image file name
var $width; // Original image width
var $height; // Original image height
var $new_width; // Resized image width
var $new_height; // Resized image height
var $image_path; // Path to image
var $thumbscale; // Scale to resize thumbnail
var $image_file; // Resized image filename
var $thumbnail; // Thumbnail image file object
var $random_file; // Resized image file name (random)

To define a class file, you simply use the keyword class followed by the class name; in our case, GallerySizer. The convention behind class names is to capitalize the first letter of the class name, followed by capitalizing the first letter of every following word without underscores or dashes (ie: GallerySizer and not Gallery_Sizer).

The variables we've created are known as member data, when discussing them in terns of Object Oriented Programming (OOP). These variables are global to the class, which will allow any method (function) to access them. They are used throughout the script for various functions, including creating the thumbnail, determining the resizable scale for the new images, creating a random filename for the newly converted images, etc.

* Retrieves path to uploaded image.
* Retrieves filename of uploaded image
function getLocation($image){
$this->image_file = str_replace("..", "/", $image);
$this->image_path = IMAGE_BASE . $this->image_file;
return true;

The method above, getLocation($image), accepts an image as its argument. The image will be passed from the upload form to the method, which will then initialize the $image_file variable to hold the name of the image, and the $image_path variable to hold the path to where the resized image will reside on the server.

<span style="background-color: #ffff00;">/*****
<br />* Determines image type, and creates an image object
<br />*/
<br />function loadImage(){
br />$this->img null;
br />$extension strtolower(end(explode('.'$this->image_path)));
br />if ($extension == 'jpg' || $extension == 'jpeg'){
br />$this->img imagecreatefromjpeg($this->image_path);
br />} else if ($extension == 'png'){
br />$this->img imagecreatefrompng($this->image_path);
br />} else {
br />return false;
br />}
br />// Sets a random name for the image based on the extension type
<br />$file_name strtolower(current(explode('.'$this->image_file)));
br />$this->random_file $file_name $this->getRandom() . "." $extension;
br />$this->thumbnail $this->random_file;
br />$this->converted $this->random_file;
br />$this->resize $this->random_file;
br />return true;
br />}

The loadImage() function above determines the file-type of the current image by splitting the filename into an array, split by the dot (.) in its name, using PHP's explode() function. The end() function simply retrieves the last element in the array.

Based on the image type, we call the "imagecreatefromXXXX" function, which, in the case of a JPEG, returns a pointer to a true-color image. This pointer is used later on the code to create our resized and thumbnail images.

To ensure that every image uploaded is unique, we retrieve the name of the image (less the extension), and add a random value to the image name. In this case, our getRandom() function will return the current date/time value, which will then be appended to the filename, creating a unique name.


>>> More MySQL Articles          >>> More By Frank Manno

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: