<?php
error_reporting(0);
ob_start();
session_start();
include_once("include/dbconnect.php");
if(isset($_POST['btn_submit']))
{
$cat_name = $_POST['cat_name'];
$cat_image = $_FILES['cat_image']['name'];
$cate_query = "INSERT INTO mec_main_category (mec_category_name,mec_category_image,mec_status) VALUES ('".addslashes($cat_name)."','".$cat_image."',1)";
$cate_query_run = mysqli_query($conn, $cate_query);
$lastid = mysqli_insert_id($conn);
if($_FILES['cat_image']['name']!="") {
    $file = $_FILES['cat_image']['tmp_name']; 
    $sourceProperties = getimagesize($file);
    $fileNewName = time();
    $folderPath = "../products/category/";
    $ext = pathinfo($_FILES['cat_image']['name'], PATHINFO_EXTENSION);
    $imageType = $sourceProperties[2];
    switch ($imageType) {
        case IMAGETYPE_PNG:
            $imageResourceId = imagecreatefrompng($file); 
            $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
            imagepng($targetLayer,$folderPath. $lastid. ".jpg");     
               break;
        case IMAGETYPE_GIF:
            $imageResourceId = imagecreatefromgif($file); 
            $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
            imagegif($targetLayer,$folderPath. $lastid. ".jpg");
               break;
        case IMAGETYPE_JPEG:
            $imageResourceId = imagecreatefromjpeg($file); 
            $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
            imagejpeg($targetLayer,$folderPath. $lastid. ".jpg");
                    break;
               default:
        header("location: add-new-category.php");
            exit;
            break;
    }
    move_uploaded_file($file, $folderPath. $lastid. "in.jpg");
}
}

function imageResize($imageResourceId,$width,$height) {

 
    $targetWidth =1200;
    $targetHeight =800;
 

    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);


    return $targetLayer;
}

?>