Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
adv banner
Web Programming  Home Web Programming PHP Object Orient Programming: Image Class
rss

Object Orient Programming: Image Class

Author: Nathanael Mitchell More by this author


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



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Object Orient Programming: Image Class"