<?php

include_once("class.upload.php");

//upload image take folder path , height, width
function uploadImages($handle, $url, $width, $height) {
    $randomname = '';
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->image_resize = true;
        $handle->image_x = $width;
        $handle->image_y = $height;
        $handle->image_ratio_y = false;
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    $full = $randomname . "." . $handle->file_src_name_ext;
    return $full;
}

function uploadImages2($handle, $url) {
    $randomname = '';
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->image_resize = true;
        //$handle->image_x              = $width;
        //$handle->image_y              = $height;
        $handle->image_ratio_y = false;
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    $full = $randomname . "." . $handle->file_src_name_ext;
    return $full;
}

function updateImages($handle, $hiddenFileName, $url, $width, $height) {
    //uploadfile
    $randomname = "new";
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->image_resize = true;
        $handle->image_x = $width;
        $handle->image_y = $height;
        $handle->image_ratio_y = false;
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    if ($randomname == "new") {
        $full = $_POST['' . $hiddenFileName . ''];
    } else {
        $full = $randomname . "." . $handle->file_src_name_ext;
    }
    return $full;
}

function uploadfile($handle, $url) {
    $randomname = '';
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    $full = $randomname . "." . $handle->file_src_name_ext;
    return $full;
}

function updatefile($handle, $hiddenFileName, $url) {
    //uploadfile
    $randomname = "new";
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));

        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    if ($randomname == "new") {
        $full = $_POST['' . $hiddenFileName . ''];
    } else {
        $full = $randomname . "." . $handle->file_src_name_ext;
    }
    return $full;
}

function updateImagesWithoutHeaight($handle, $hiddenFileName, $url, $width) {
    //uploadfile
    $randomname = "new";
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->image_resize = true;
        $handle->image_x = $width;
        //$handle->image_y              = $height;
        $handle->image_ratio_y = true;
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    if ($randomname == "new") {
        $full = $_POST['' . $hiddenFileName . ''];
    } else {
        $full = $randomname . "." . $handle->file_src_name_ext;
    }
    return $full;
}

function uploadImageswhioutresize($handle, $url) {
    $randomname = '';
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->image_resize = false;
        /* $handle->image_x              = $width;
          $handle->image_y              = $height;
          $handle->image_ratio_y        = false; */
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    $full = $randomname . "." . $handle->file_src_name_ext;
    return $full;
}

function updateImagesWithoutresiz($handle, $hiddenFileName, $url) {
    //uploadfile
    $randomname = "new";
    if ($handle->uploaded) {
        $randomname = md5(uniqid(mt_rand(), true));
        $handle->image_resize = false;
        //$handle->image_x              = $width;
        //$handle->image_y              = $height;
        $handle->image_ratio_y = true;
        $handle->file_new_name_body = $randomname;
        $handle->process($url);
        if ($handle->processed) {
            $handle->clean();
        } else {
            echo 'error : ' . $handle->error;
        }
    }
    if ($randomname == "new") {
        $full = $_POST['' . $hiddenFileName . ''];
    } else {
        $full = $randomname . "." . $handle->file_src_name_ext;
    }
    return $full;
}

#########
## multiple part , fatma

function uploadfileMultiple($inputName, $url, $oldNamesArr = array()) {
    $newNamesToRet = array();
    removefileMultiple($oldNamesArr, $url);
    foreach (reArrayFiles($_FILES[$inputName]) as $single) {
        $handle = new upload($single);
        $fileName = uploadfile($handle, $url);
        $newNamesToRet[] = $fileName;
    }
    return $newNamesToRet;
}


function uploadfileMultipleWithName($inputName, $url, $oldNamesArr = array()) {
    $newNamesToRet = array();
    $OldNames = array();
    removefileMultiple($oldNamesArr, $url);
    foreach (reArrayFiles($_FILES[$inputName]) as $single) {
        $handle = new upload($single);
        $fileName = uploadfile($handle, $url);
        $newNamesToRet[] = $fileName;
        $OldNames[] = $single['name'];
    }
    return array($newNamesToRet, $OldNames);
}



function removefileMultiple($oldNamesArr, $url) {
    foreach ($oldNamesArr as $single) {
        unlink($url . '/' . $single);
    }
}

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i = 0; $i < $file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

############
?>