<?php

function ImageCompressFn($imagename, $source, $modwidth, $modheight,$folderPath){
        $prefix = date('Ymd_his');
    
          $imagename = $imagename;    //$_FILES['new_image']['name'];
          $source = $source;    //$_FILES['new_image']['tmp_name'];
    
          //$target = "images/".$imagename;
          $target = $folderPath.$imagename;
          move_uploaded_file($source, $target);

          $imagepath = $imagename;
          $save = $folderPath.$prefix.$imagepath; //This is the new file you saving
          $file = $folderPath.$imagepath; //This is the original file
          
          $returnImgname="";
          $returnImgname = $prefix.$imagepath;

          list($width, $height) = getimagesize($file) ; 

          $what = getimagesize($file);
            switch(strtolower($what['mime']))
            {
                case 'image/png':
                        $tn = imagecreatetruecolor($width, $height) ; 
                        $image = imagecreatefrompng($file) ; 
                        imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ; 
                        imagepng($tn, $save, 9) ;
                    break;
                case 'image/jpeg':
                        $tn = imagecreatetruecolor($width, $height) ; 
                        $image = imagecreatefromjpeg($file) ; 
                        imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ; 
                        imagejpeg($tn, $save, 70) ;
                    break;
                case 'image/gif':
                        $tn = imagecreatetruecolor($width, $height) ; 
                        $image = imagecreatefromgif($file) ; 
                        imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ; 
                        imagegif($tn, $save, 70) ;
                    break;
                default: die();
            }
        

        // THUMB IMAGE CODE
        
          $save = $folderPath."thumb_".$prefix.$imagepath; //This is the new file you saving
          $file = $folderPath.$imagepath; //This is the original file

          list($width, $height) = getimagesize($file) ; 

          $modwidth = $modwidth;    //300; 
          $modheight = $modheight;  //300; 
          
            switch(strtolower($what['mime']))
            {
                case 'image/png':
                        $tn = imagecreatetruecolor($modwidth, $modheight) ; 
                        $image = imagecreatefrompng($file) ; 
                        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                        imagepng($tn, $save, 9) ;
                    break;
                case 'image/jpeg':
                        $tn = imagecreatetruecolor($modwidth, $modheight) ; 
                        $image = imagecreatefromjpeg($file) ; 
                        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                        imagejpeg($tn, $save, 70) ;
                    break;
                case 'image/gif':
                        $tn = imagecreatetruecolor($modwidth, $modheight) ; 
                        $image = imagecreatefromgif($file) ; 
                        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                        imagegif($tn, $save, 70) ;
                    break;
                default: die();
            } 
            
            return $returnImgname;
}

