<?php
	
	include ('includes/db1config.php');
	include ('../includes/mysql_connect.php');

	if( isset($_FILES) && !empty($_FILES) ){

	    $imageName = saveUploadedImage();
	    $storeNew  = "INSERT INTO marketing_materials (description,detail,file_name,thumbnail,status) VALUES (
	        '','','".mysql_escape_string($imageName['imageName'])."','".mysql_escape_string($imageName['thumbName'])."','')";
	    mysql_query($storeNew) or die(mysql_error());
	    header('Location: /cmsadmin/index.php?admin-page=15	');
	}


	function saveUploadedImage() {
	    
	    include('includes/smart_resize_image.php');

	    $dir           = realpath('../custom/marketing/');
	    if (!file_exists($dir) && !is_dir($dir)) {
	        mkdir($dir);
	    } 

	    $imageName     = '';
	    $filenameParts = pathinfo($_FILES['image']['name']);
	    $filename      = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $filenameParts['filename']));
	    $ext           = strtolower($filenameParts['extension']);
	    $newFilename   = $filename.'.'.$ext;
	    $thumbName     = $filename.'_thumb'.'.'.$ext;
	    $tmp           = $_FILES['image']['tmp_name'];
	    $tmp1          = $_FILES['image']['tmp_name'];
	    $i             = time();

	    while (file_exists($dir.'/'.$newFilename)) {
	        $newFilename = $filename.'-'.$i.'.'.$ext;
	    }

	    $dest   = $dir.'/'.$newFilename;
	    $dest1  = $dir.'/'.$thumbName;
	    $thDest = $dir.'/'.$thumbName;
	    
	    if ( move_uploaded_file($tmp,$dest) ) {
	        
	        list($width, $height) = getimagesize($dest);
	        if ($width > 900 || $height > 600) {
	            if ($width >= $height) {
	                smart_resize_image($dest, 900, 0, true, $dest);
	            } else{
	                smart_resize_image($dest, 0, 600, true, $dest);
	            }
	        }
	    	
	    	if( $ext != 'pdf' ){
		        copy($dest, $dest1);
		        list($width, $height) = getimagesize($dest1);
		        if ($width > 200 || $height > 200) {
		            if ($width >= $height) {
		                smart_resize_image($dest1, 200, 0, true, $dest1);
		            } else{
		                smart_resize_image($dest1, 0, 200, true, $dest1);
		            }
		        }
		        copy($dest1, $dir.'/'.$thumbName);

	    	} else {

	    		$dir    = realpath('../custom/images');
	    		$dir1   = realpath('../custom/marketing/');
	    		$dest   = $dir.'/pdf_thumb.png';
	    		$dest1  = $dir1.'/'.$thumbName;

	    		copy($dest, $dest1);
		        list($width, $height) = getimagesize($dest1);
		        if ($width > 200 || $height > 200) {
		            if ($width >= $height) {
		                smart_resize_image($dest1, 200, 0, true, $dest1);
		            } else{
		                smart_resize_image($dest1, 0, 200, true, $dest1);
		            }
		        }
		        copy($dest1, $dir1.'/'.$thumbName);
	    		$thumbName     = $thumbName;
	    		
	    	}	

	        $imageName = $newFilename;

	    }

	    return array(
	        'thumbName'  => $thumbName,
	        'imageName'  => $imageName
	    );
	}

?>