<?php

session_start();

ob_start();

include_once("../dbconnect.php");

function createthumb($name, $newname, $new_w, $new_h, $border=false, $transparency=true, $base64=false) {

    if(file_exists($newname))

        @unlink($newname);

    if(!file_exists($name))

        return false;

    $arr = split("\.",$name);

    $ext = $arr[count($arr)-1];



    if($ext=="jpeg" || $ext=="jpg" || $ext=="JPEG" || $ext=="JPG"){

        $img = @imagecreatefromjpeg($name);

    } elseif($ext=="png"){

        $img = @imagecreatefrompng($name);

    } elseif($ext=="gif") {

        $img = @imagecreatefromgif($name);

    }

    if(!$img)

        return false;

    $old_x = imageSX($img);

    $old_y = imageSY($img);

	$thumb_w = $new_w;

    $thumb_h = $new_h;

  

    $thumb_w = ($thumb_w<1) ? 1 : $thumb_w;

    $thumb_h = ($thumb_h<1) ? 1 : $thumb_h;

    $new_img = ImageCreateTrueColor($thumb_w, $thumb_h);

   

    if($transparency) {

        if($ext=="png") {

            imagealphablending($new_img, false);

            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);

            imagefill($new_img, 0, 0, $colorTransparent);

            imagesavealpha($new_img, true);

        } elseif($ext=="gif") {

            $trnprt_indx = imagecolortransparent($img);

            if ($trnprt_indx >= 0) {

                //its transparent

                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);

                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

                imagefill($new_img, 0, 0, $trnprt_indx);

                imagecolortransparent($new_img, $trnprt_indx);

            }

        }

    } else {

        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));

    }

   

    imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);

    if($border) {

        $black = imagecolorallocate($new_img, 0, 0, 0);

        imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);

    }

    if($base64) {

        ob_start();

        imagepng($new_img);

        $img = ob_get_contents();

        ob_end_clean();

        $return = base64_encode($img);

    } else {

        if($ext=="jpeg" || $ext=="jpg" || $ext=="JPEG" || $ext=="JPG"){

            imagejpeg($new_img, $newname);

            $return = true;

        } elseif($ext=="png"){

            imagepng($new_img, $newname);

            $return = true;

        } elseif($ext=="gif") {

            imagegif($new_img, $newname);

            $return = true;

        }

    }

    imagedestroy($new_img);

    imagedestroy($img);

    return $return;

}

$action = isset($_GET['action']) ? $_GET['action'] : '';

 switch ($action) {

	 



	

    case 'add' :

        add();

        break;

    case 'delete' :

        delete();

        break;	

		

	    case 'edit' :

	     edit();

         break;

		    case 'status' :

	     status();

         break;

		

 

		

default :

        // if action is not defined or unknown

        // move to main category page

        header('Location: welcomepage.php');

		

}

function add()

{

    echo "insert into mec_gallery(mec_g_id,mec_image,mec_name,mec_description,mec_status) values('".$_POST['mec_g_id']."','".$_FILES['mec_image']['name']."','".$_POST['mec_name']."','".$_POST['mec_description']."',1)";

	mysql_query("insert into mec_gallery(mec_g_id,mec_image,mec_name,mec_description,mec_status) values('".$_POST['mec_g_id']."','".$_FILES['mec_image']['name']."','".$_POST['mec_name']."','".$_POST['mec_description']."',1)");

	 $lastid=mysql_insert_id();

	

			$ftmp = $_FILES['mec_image']['tmp_name'];

            $oname = $_FILES['mec_image']['name'];

            $fname = '../products/waste/'.$_FILES['mec_image']['name'];

			$userfile_type = $_FILES['mec_image']['type'];

            

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			



			createthumb($fname, "../products/gallery/".$lastid.".jpg", $width,$height,false, true, false);

		createthumb($fname, "../products/gallery/".$lastid."in.jpg", 600,400,false, true, false);

		//createthumb($fname, "../products/gallery/".$lastid."flash.jpg", 1920,800,false, true, false); 

		//createthumb($fname, "../products/gallery/".$lastid."ff.jpg", 1200,1200,false, true, false);

unlink('../products/waste/'.$_FILES['mec_image']['name']);

header("location:gallery.php?mec_g_id=".$_POST['mec_g_id']);

}

function delete()

{

	mysql_query("delete from mec_gallery where mec_p_id=".$_GET['mec_p_id']);

	header("location:gallery.php?mec_g_id=".$_GET['mec_g_id']);

}



function edit()

{

	mysql_query("update mec_gallery set mec_image='".$_FILES['mec_image']['name']."', mec_name='".$_POST['mec_name']."',mec_description='".$_POST['mec_description']."' where mec_p_id='".$_POST['mec_p_id']."'");

if($_FILES['mec_image']['name']!=""){

	$lastid=$_POST['mec_p_id'];

	

			$ftmp = $_FILES['mec_image']['tmp_name'];

            $oname = $_FILES['mec_image']['name'];

            $fname = '../products/waste/'.$_FILES['mec_image']['name'];

			$userfile_type = $_FILES['mec_image']['type'];

            $sizes = getimagesize($ftmp);

            $width	=	$sizes[0];

            $height	=	$sizes[1];

    

            $extenssion = strstr($oname, ".");

        

            $prod_img		= $fname;	

            move_uploaded_file($ftmp, $prod_img);

			createthumb($fname, "../products/gallery/".$lastid.".jpg", $width,$height,false, true, false); 

			createthumb($fname, "../products/gallery/".$lastid."in.jpg", 600,40,false, true, false); 

			//createthumb($fname, "../products/gallery/".$lastid."flash.jpg", 1920,800,false, true, false); 

			//createthumb($fname, "../products/gallery/".$lastid."ff.jpg", 1200,1200,false, true, false); 

unlink('../gallery/waste/'.$_FILES['mec_image']['name']);

}

header("location:gallery.php?mec_g_id=".$_POST['mec_g_id']);

}

function status()

{

	mysql_query("update mec_gallery set mec_status='".$_GET['mec_status']."' where mec_p_id=".$_GET['mec_p_id']);

	header("location:gallery.php?mec_g_id=".$_GET['mec_g_id']);

}

?>