<?php

    $path = '../../';
    require_once $path . 'core/includes/header.inc.php';
    $page_meta['title'] = 'Recrop Image';
    require_once $path . 'core/admin/includes/auth.php';

    $filename = $_GET['image'];
    $imagefolders = $$_GET['type'];
    $tempFileUrl = '/'.$imagefolders['originals']['path'].'/'.$filename;
    $tempFile   = SITE_PATH.$tempFileUrl;
    $minheight = 0;
    $minwidth = 0;

    foreach ($imagefolders as $folder) {
        if ($folder['crop']==true) {
            $aspectratio = $folder['width']/$folder['height'];
            if ($folder['width']>$minwidth) {
                $minwidth = $folder['width'];
            }
            if ($folder['height']>$minheight) {
                $minheight = $folder['height'];
            }
        }
    }

    $size = get_image_size($imagefolders['originals']['path'],$filename);

    $cropped = false;

    if ($_POST['submitBtn']!='') {
        foreach ($imagefolders as $folder) {
            if ($folder['crop']==true) {
                  $targetFile = SITE_PATH.'/'.$folder['path'].'/'.$filename;
                copy($tempFile,$targetFile);
                image_crop($folder['path'],$filename,$_REQUEST['jcropW'],$_REQUEST['jcropH'],$_REQUEST['jcropX'],$_REQUEST['jcropY']);
                the_image_resize($folder['width'],$folder['height'],$folder['path'],$filename);
            }
        }
        $cropped = true;
    }

?>
<!doctype html>
<html lang="en">
<head>
<?php
    require_once $path . 'core/admin/templates/meta.php';
?>
</head>
<body style="width: auto; min-width:0px;">
    <?php
    if (!$cropped) {
    ?>
    <form action="" id="jcropImage" enctype="multipart/form-data" method="post" style="text-align: center;float:none;">
        <img src="<?=$tempFileUrl?>" id="jCropTarget" alt="recrop" style="width: <?=$size['width']?>px; max-width: <?=$size['width']?>px; height: <?=$size['height']?>px;" />

        <input type="hidden" id="aspectratio" value="<?=$aspectratio?>" />
        <input type="hidden" id="minwidth" value="<?=$minwidth?>" />
        <input type="hidden" id="minheight" value="<?=$minheight?>" />
        <input type="hidden" name="image" value="<?=$filename?>" />
        <input type="hidden" name="jcropX" id="jcropX" value="0" />
        <input type="hidden" name="jcropY" id="jcropY" value="0" />
        <input type="hidden" name="jcropW" id="jcropW" value="0" />
        <input type="hidden" name="jcropH" id="jcropH" value="0" />
        <input type="submit" class="button submit" name="submitBtn" value="Re-Crop Image" id="cropButton" />
    </form>
    <?php
    } else {
    ?>
        <p class="success">Image cropped successfully</p>
        <script>
            parent.$.fancybox.close();
        </script>
    <?php
    }
    ?>
</body>
</html>
