Object Orient Programming: Image Class

Object Orient Programming: Image ClassIn Part four of the Object Oriented tutorial, I share the code I use for my image class. In this class there are 4 function.

  • One to Upload
  • One to Resize
  • One to get a file extenstion
  • and One to resize on the fly

In order to use the resize function, you will need the gd 2 library. If you're not sure if you have this, create a php file, and place this code in it.

phpinfo();
?>

View the file, and search for gd. you can find more info on gd here

First the 82 lines of code that I call func_images.php

class images {

function upload($Request_Name, $Img_Dir){
$Image = $_FILES[$Request_Name]['name'];
$sub_Image = str_replace(" ", "_", $Image);

//Make sure we dont overright a file
$dir_arr = Array();
$handle = opendir("$Img_Dir");
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$dir_arr[] = $file;
}
}
if(in_array("$sub_Image", $dir_arr)) {
$_SESSION['Message'] = "$sub_Image Already exists";
return FALSE;
}
//Save Full Size Image
$copy = copy ($_FILES[$Request_Name]['tmp_name'], "$Img_Dir".$sub_Image);
if(!$copy) {
$_SESSION['Message'] = "an image must be select";
return FALSE;
}
return $sub_Image;
}
function getExtension($filename){
$dot = substr (strrchr ($filename, "."), 1);
return $dot;
}
function resize($Image, $Img_Dir, $width, $height, $mod_name = '', $target_dir = ''){
$ext = $this->getExtension($Image);
if($ext != "jpg" && $ext != "jpeg" && $ext != "gif"){
$_SESSION['Message'] = "Only .jpg, .jpeg, and .gif are supported";
return FALSE;
}
$sourcefile = $Img_Dir . $Image;
$imagesize = getimagesize("$sourcefile");
if($imagesize[0] > $width || $imagesize[1] > $width) {
if ($imagesize[0] > $imagesize[1]) {
$percentage = ($width / $imagesize[0]);
} else {
$percentage = ($height / $imagesize[1]);
}
$new_width = round($imagesize[0] * $percentage);
$new_height = round($imagesize[1] * $percentage);
$dest_x = $new_width;
$dest_y = $new_height;
if($target_dir != ''){
$targetfile = $target_dir . $mod_name . $Image;
} else {
$targetfile = $Img_Dir . $mod_name . $Image;
}
$jpegqual = 100;
switch($ext){
case 'jpg' :
case 'jpeg' :
$source_id = imageCreatefromjpeg("$sourcefile");
break;
case 'gif' :
$source_id = imageCreatefromgif("$sourcefile");
break;
}
$target_id = imagecreatetruecolor($dest_x, $dest_y);
$target_pic = imagecopyresampled ($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$imagesize[0],$imagesize[1]);
imagejpeg ($target_id,"$targetfile",$jpegqual);
}
}
function fly_resize($source, $width, $height) {
$sourcefile = $source;
$imagesize = getimagesize("$sourcefile");
if($imagesize[0] > $width || $imagesize[1] > $width) {
if ($imagesize[0] > $imagesize[1]) {
$percentage = ($width / $imagesize[0]);
} else {
$percentage = ($height / $imagesize[1]);
}
$size = array();
$size[0] = round($imagesize[0] * $percentage);
$size[1] = round($imagesize[1] * $percentage);
return $size;
}
}
}
?>

This code is written so you can copy it, and use it, without making any changes, which is what object is all about; code you can use anywhere without chaning it. You will need to know how to call each function, so I will explain that. Ill also break each function apart and give a break down of whats going on.

Ok lets look at the function


Related Articles

  • 4 Powerful Features of Javascript Programming Language
    In the community of web developers and surfers, Javascript is highly popular as client side scripting language for web browsers. Lets take a look at some of the features of this language.
  • Object Oriented Programming With PHP 4.1.x
    Object Oriented Programming provides raw power to PHP 4.1.x Part 1:Introduction First off, an object (also know as a class) is a very simple section of code that has a section of its own variables and functions. In a simple way an object is kind of like a program itself. Objects can be used fo...
  • Basic Design Principles Part 2 Tone, Texture, Light, And Shade
    This specifically applies to drawings more than photography, but tone and texture are very important. Tone refers to shading of light and dark on an object and texture is the visual and tactile surface characteristics of an object.
  • Intro To Object: Option Variables
    Now that we have our first simple class (which we build in Intro To Object: Building Your First Class) were going to modify it a bit.
  • Working with Objects in PHP 5
    The second part of the series showing new PHP programmers how to deal with Objects and learn the basics of Object Oriented Programming.
  • Introduction to Procedural Programming
    Well start of with if
    Here is the basic usage of it
    variable would be something like $variable
  • Intro To Object: Creating Your First Class
    You have havent done so yet, I strongly recommend you first read my Intro To Object before starting this tutorial.
  • Server Side Includes
    This very helpful tutorial makes it much easier to make changes throughout your entire website. It is easy to follow and has plenty of code.
  • Intro To Object Oriented
    One thing you dont see too much, is tutorials on object oriented php. Now some may be familiar with object from Java and C++, and then on the other hand, many of you may have no clue what object is. For this tutorial, you need to be pretty fluent in php.
  • An Introduction to ROR (Resources of a Resource)
    Why do people use ROR for describing the content, objects, and structure of websites in a generic fashion? Don

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.