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

Resizing Images - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Again, we make use of GD's imagecreatetruecolor() to create a pointer to a true-colored image.

imagecopyresampled($tmp_resize, $this->img, 0, 0, 0, 0, RESIZE_WIDTH, RESIZE_HEIGHT, $this->width, $this->height);
imagedestroy($this->img);
$this->resize = $tmp_resize;

We then resize our image to the specified size, passing to it the temporary image pointer, the original image, and the 4 parameters dealing with the co-ordinates and size (similar to the ones passed in the setThumbnail() function.

GD has a built-in function to destroy the original image uploaded. We'll use this function once our image has been resized, followed by assigning the value of $tmp_resize, to our resized image object ($this->resize).

/*****
* Copies thumbnail image to specified thumbnail directory.
* Sets permissions on file
*/
function copyThumbImage(){
imagejpeg($this->thumb, $this->thumbnail);
if(!@copy($this->thumbnail, THUMB_BASE . $this->thumbnail)){
echo("Error processing file... Please try again!");
return false;
}
if(!@chmod($this->thumbnail, 666)){
echo("Error processing file... Please try again!");
return false;
}
if(!@unlink($this->thumbnail)){
echo("Error processing file... Please try again!");
return false;
}
return true;
}

Because we're not storing our images in a database, we need to copy the file over to our new images to our desired directories, specified in our constant variables.

imagejpeg($this->thumb, $this->thumbnail);

The imagejpeg() function will actually write the JPEG file information to a file that will now reside in the filesystem. We pass, as arguments, the thumbnail pointer ($this->thumb) and the object which will represent the actual thumbnail file ($this->thumbnail).

We then copy the image to our thumbnail directory, set the desired permissions (making use of PHP's chmod() function), and unlink/delete the temporary thumbnail image.

/*****
* Copies the resized image to the specified images directory.
* Sets permissions on file.
*/
function copyResizedImage(){
imagejpeg($this->resize, $this->converted);
if(!@copy($this->converted, IMAGE_BASE . $this->converted)){
echo("Error processing file... Please try again!");
return false;
}
if(!@chmod($this->converted, 666)){
echo("Error processing file... Please try again!");
return false;
}
if(!unlink($this->converted)){
echo("Error processing file... Please try again!");
return false;
}
// Delete the original uploaded image
if(!unlink(IMAGE_BASE . $this->image_file)){
echo("Error processing file... Please try again!");
return false;
}
return true;
}

Our copyResizedImage() function is identical to the copyThumbImage() function, except that we work with the resized image rather than the thumbnail.

/*****
* Generates a random number. Random number is used to rename
* the original uploaded image, once resized.
*/
function getRandom(){
return "_" . date("dmy_His");
}

Our getRandom() function simply returns a string value with the current date and time with a seconds value.

/*****
* Returns path to thumbnail image
*/
function getThumbLocation(){
return "thumbs/" . $this->random_file;
}

The getThumbLocation() function returns the path to our thumbnail image, so that the value may be inserted into our database for retrieval later on.

/*****
* Returns path to resized image
*/
function getImageLocation(){
return "photos/" . $this->random_file;
}

The getImageLocation() returns the path to our resized image, which will be inserted into our database for retrieval later on.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- 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: