upload_image.class.php

<?php

/*
    Uploaded image file is authenticated as mime type image, temporary
    directory is created, optional thumbnail is generated,
    contents of directory (uploaded image and thumbnail (opt) ) are moved to
    desired directory location.
*/


class upload_image
{

 const 
TEMP_DIR "temp_imgs"// Temporary image folder


public function save_image($form_fieldname 'file',
                           
$image_destination './',
                           
$create_thumb FALSE,
                           
$thumbnail_size 150){




if(
is_uploaded_file($_FILES[$form_fieldname]['tmp_name'])){

       
$img_mime_type getimagesize($_FILES[$form_fieldname]['tmp_name']);

       if((
$img_mime_type[2] == IMAGETYPE_GIF) ||
         (
$img_mime_type[2] == IMAGETYPE_JPEG) ||
         (
$img_mime_type[2] == IMAGETYPE_PNG))
       {

           
//create temp images directory
           
if(!is_dir(self::TEMP_DIR)){

           
mkdir(self::TEMP_DIR,0666,true);

           }

           
//move the uploaded file into the temp directory
           
move_uploaded_file($_FILES[$form_fieldname]['tmp_name'],self::TEMP_DIR.'/'.$_FILES[$form_fieldname]['name']);

           
$picture_name $_FILES[$form_fieldname]['name'];

           if (
$create_thumb){

              
//create thumbnail of uploaded img
              
$this->createthumb(self::TEMP_DIR.'/'.$picture_name,self::TEMP_DIR.'/thumb_'.$picture_name,$thumbnail_size,$thumbnail_size);

              
//move the thumbnail
              
if(!is_file("$image_destination/thumb_{$picture_name}")){
                  
rename(self::TEMP_DIR.'/thumb_'.$picture_name"$image_destination/thumb_{$picture_name}");
              } else {
                      echo 
"<br /><b>! An Image Thumb with that name already exists</b><br />";
                      
unlink(self::TEMP_DIR.'/thumb_'.$picture_name);
              }

           }

           
//move the full sized image
           
if(!is_file("$image_destination/$picture_name")){
               
rename(self::TEMP_DIR.'/'.$picture_name"$image_destination/$picture_name");
           } else {
                   echo 
"<b>! An image with that name already exists</b>";
                   
unlink(self::TEMP_DIR.'/'.$picture_name);
           }


           
//remove the temp image directory   // (needs some "delete all files" functionality really, just incase)
           
rmdir(self::TEMP_DIR);

       } else {
               die(
'You may only upload file types JPEG/JPG, PNG or GIF.');
       }

}


// End function upload_image


//  function createthumb creates a thunbnail of an image
//  @param  ('initialImageName.jpg','tn_imageName.jpg',new_width,new_height);

public function createthumb($name,$filename,$new_w,$new_h){
    
$ext pathinfo($namePATHINFO_EXTENSION);
    if (
preg_match('/jpe?g/i'$ext)) {
        
$src_img imagecreatefromjpeg($name);
    }
    if (
preg_match('/png/i'$ext)) {
        
$src_img imagecreatefrompng($name);
    }

           
$old_x imageSX($src_img);
           
$old_y imageSY($src_img);

              if (
$old_x >= $old_y) {
                 
$thumb_w $new_w;
                 
$thumb_h $old_y * ($new_h/$old_x);
              }
              if (
$old_x $old_y) {
              
$thumb_w $old_x * ($new_w/$old_y);
              
$thumb_h=$new_h;
              }
              


   
$dst_img ImageCreateTrueColor($thumb_w,$thumb_h);

   
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);




   if (
preg_match('/png/i'$ext)) {
    
imagepng($dst_img,$filename);
   } else {
       
imagejpeg($dst_img,$filename);
   }

imagedestroy($dst_img);

imagedestroy($src_img);

}




// End Class

/*

EXAMPLE CALL
------------

$upload = new upload_image();

$upload->save_image('file','./',TRUE,150);

*/

?>

Paul's Code Library

Categories

Home

AJAX

classes

database_functions

date_functions

file_functions

googlevideo_and_youtube_api

htaccess

image_functions

string_functions