function create_thumbnail()
{
    global $img, $Pivot_Vars;
    $thumb = new Image($Pivot_Vars['crop'], $Pivot_Vars['crop_w'], $Pivot_Vars['crop_h'], $Pivot_Vars['crop_x'], $Pivot_Vars['crop_y'], $Pivot_Vars['type']);
    $ext = strtolower($img->ext);
    if ($ext == 'gif' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
        gd_crop($thumb);
    } else {
        echo "This file extension is not supported, please try JPG, GIF or PNG";
        print_module_footer();
    }
}
function create_thumbnail()
{
    global $img;
    //echo "<pre>\n"; print_r($_GET); echo "</pre>";
    $thumb = new Image($_GET['crop'], $_GET['crop_w'], $_GET['crop_h'], $_GET['crop_x'], $_GET['crop_y'], $_GET['type']);
    $ext = strtolower($img->ext);
    if ($ext == 'gif' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
        gd_crop($thumb);
    } else {
        echo __("This file extension is not supported, please try JPG, GIF or PNG");
        print_module_footer();
    }
}
function gd_crop($thumb)
{
    global $img, $PIVOTX;
    $sx = $thumb->x;
    $sy = $thumb->y;
    $sw = $thumb->w;
    $sh = $thumb->h;
    $scalew = $sw / $PIVOTX['image']['mw'];
    $scaleh = $sh / $PIVOTX['image']['mh'];
    if ($thumb->type == "bounded") {
        $factor = $_GET['factor'];
    } else {
        if ($thumb->type == "free") {
            $factor = 1;
        } else {
            $factor = max($scalew, $scaleh);
        }
    }
    $dx = 0;
    $dy = 0;
    $dw = $thumb->w / $factor;
    $dh = $thumb->h / $factor;
    $ext = strtolower($img->ext);
    printf("<div id='editor'><h1 style=\"padding: 6px; margin: 0 0 10px;  border-bottom: 1px solid #AAA;\">%s:  <b>'%s'</b></h1>\n", __("PivotX thumbnail creator"), basename($img->name));
    if ($ext == 'gif') {
        echo "<small style='color:red;'>(" . __("When using GIF files, there is a significant chance that you can't use PivotX to make thumbnails. If you have problems with making thumbnails, we suggest using PNG or JPG files.") . ")</small><br /><br />\n";
    }
    if (!in_array($ext, array('gif', 'jpg', 'png', 'jpeg'))) {
        echo "<strong>" . __("You can only make thumbnails of .gif, .jpg and .png images with PivotX.") . "</strong>\n";
        die;
    }
    $filename = $img->name;
    $sitepath = stripTrailingSlash($PIVOTX['paths']['site_path']);
    // Check if the base path is already in $_GET['crop']..
    if (strpos($_GET['crop'], $sitepath) === 0) {
        $thumbfilename = $_GET['crop'];
        $siteurl = stripTrailingSlash($PIVOTX['paths']['site_url']);
        $thumblink = str_replace($sitepath, $siteurl, $_GET['crop']);
    } else {
        $thumbfilename = $PIVOTX['paths']['upload_base_path'] . $_GET['crop'];
        $thumblink = $PIVOTX['paths']['upload_base_url'] . $_GET['crop'];
    }
    if ($PIVOTX['image']['local']) {
        if ($ext == "jpeg") {
            $ext = "jpg";
        }
        if ($ext == "jpg") {
            $src = imagecreatefromjpeg($filename);
        }
        if ($ext == "png") {
            $src = imagecreatefrompng($filename);
        }
        if ($ext == "gif") {
            $src = imagecreatefromgif($filename);
        }
        if (function_exists('imagecreatetruecolor')) {
            $dst = imagecreatetruecolor($dw, $dh);
            $tmp_img = imagecreatetruecolor($sw, $sh);
        } else {
            $dst = imagecreate($dw, $dh);
        }
        if (function_exists('imagecopyresampled')) {
            // GD 2.0 has a bug that ignores the 'source_x' and 'source_y'..
            // to compensate, we use a temp image..
            imagecopy($tmp_img, $src, 0, 0, $sx, $sy, $sw, $sh);
            imagecopyresampled($dst, $tmp_img, 0, 0, 0, 0, $dw, $dh, $sw, $sh);
        } else {
            imagecopyresized($dst, $src, 0, 0, $sx, $sy, $dw, $dh, $sw, $sh);
        }
        if ($ext == "jpg") {
            imagejpeg($dst, $thumbfilename, $PIVOTX['image']['qual']);
        }
        if ($ext == "png") {
            imagepng($dst, $thumbfilename, ceil($PIVOTX['image']['qual'] / 10));
        }
        // Ensure the created thumb has the correct file permission.
        chmodFile($thumbfilename);
        ImageDestroy($dst);
    } else {
        $remotefile = str_replace($PIVOTX['paths']['upload_base_path'], $PIVOTX['paths']['upload_base_url'], $filename);
        $remotefile = sprintf("%s%s", $PIVOTX['paths']['host'], urlencode($remotefile));
        $remoteurl = getDefault($PIVOTX['config']->get('remote_crop_script'), 'http://www.mijnkopthee.nl/remote/crop.php');
        $remote = sprintf('%s?img=%s&dx=%s&dy=%s&sx=%s&sy=%s&dw=%s&dh=%s&sw=%s&sh=%s&ext=%s', $remoteurl, $remotefile, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh, $img->ext);
        if (@($fp = fopen($remote, "rb"))) {
            $handle = fopen($thumb->name, "wb");
            while (!feof($fp)) {
                fwrite($handle, fread($fp, 8192));
            }
            fclose($handle);
            fclose($fp);
        } else {
            echo "<p><strong>" . __("Couldn't make thumbnail remotely using") . " {$remoteurl}</strong></p>";
        }
    }
    srand((double) microtime() * 1000000);
    $rand = rand(10000, 99999);
    echo '<div id="testWrap" style="float:left;">';
    printf("<img src='%s?%s' alt='%s'><br />\n", $thumblink, $rand, $thumblink);
    echo "</div>";
    print_module_footer();
}
function gd_crop($thumb)
{
    global $img, $mw, $mh, $qual, $local, $Cfg, $Paths;
    $sx = $thumb->x;
    $sy = $thumb->y;
    $sw = $thumb->w;
    $sh = $thumb->h;
    $scalew = $sw / $mw;
    $scaleh = $sh / $mh;
    if ($thumb->type == "bounded") {
        $factor = max($sw, $sh) / max($mw, $mw);
    } else {
        if ($thumb->type == "free") {
            $factor = 1;
        } else {
            $factor = max($scalew, $scaleh);
        }
    }
    $dx = 0;
    $dy = 0;
    $dw = $thumb->w / $factor;
    $dh = $thumb->h / $factor;
    printf("<div id='editor'><h1 style=\"padding: 6px; margin: 0 0 10px;  border-bottom: 1px solid #AAA;\">Pivot thumbnail creator:  <b>'%s'</b></h1>\n", $img->name);
    if ($img->ext == 'gif') {
        echo "<small style='color:red;'>(When using GIF files, there is a significant chance that you can't use Pivot to make thumbnails. If you have problems with making thumbnails, we suggest using PNG or JPG files.)</small><br /><br />\n";
    }
    $filename = $Paths['upload_url'] . $img->name;
    $thumbfilename = $Paths['upload_url'] . $thumb->name;
    if ($local) {
        $ext = strtolower($img->ext);
        if ($ext == "jpeg") {
            $ext = "jpg";
        }
        if ($ext == "jpg") {
            $src = imagecreatefromjpeg($img->name);
        }
        if ($ext == "png") {
            $src = imagecreatefrompng($img->name);
        }
        if ($ext == "gif") {
            $src = imagecreatefromgif($img->name);
        }
        if (function_exists('imagecreatetruecolor')) {
            $dst = imagecreatetruecolor($dw, $dh);
            $tmp_img = imagecreatetruecolor($sw, $sh);
        } else {
            $dst = imagecreate($dw, $dh);
        }
        if (function_exists('imagecopyresampled')) {
            // GD 2.0 has a bug that ignores the 'source_x' and 'source_y'..
            // to compensate, we use a temp image..
            imagecopy($tmp_img, $src, 0, 0, $sx, $sy, $sw, $sh);
            imagecopyresampled($dst, $tmp_img, 0, 0, 0, 0, $dw, $dh, $sw, $sh);
        } else {
            imagecopyresized($dst, $src, 0, 0, $sx, $sy, $dw, $dh, $sw, $sh);
        }
        if ($ext == "jpg") {
            ImageJPEG($dst, $thumb->name, $qual);
        }
        if ($ext == "png") {
            ImagePNG($dst, $thumb->name, $qual);
        }
        ImageDestroy($dst);
    } else {
        $remotefile = sprintf("%s%s", $Paths['host'], urlencode($filename));
        $remote = sprintf("http://www.mijnkopthee.nl/remote/crop.php?img=%s&dx=%s&dy=%s&sx=%s&sy=%s&dw=%s&dh=%s&sw=%s&sh=%s&ext=%s", $remotefile, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh, $img->ext);
        //debug("remote: $remote");
        if (@($fp = fopen($remote, "rb"))) {
            $handle = fopen($thumb->name, "wb");
            while (!feof($fp)) {
                fwrite($handle, fread($fp, 8192));
            }
            fclose($handle);
            fclose($fp);
        } else {
            echo "could not make thumbnail<br />";
            echo $remote;
        }
    }
    srand((double) microtime() * 1000000);
    $rand = rand(10000, 99999);
    echo '<div id="testWrap" style="float:left;">';
    printf("<img src='%s?%s'><br />\n", $thumbfilename, $rand);
    echo "</div>";
    //printf("<h2>Saved as <b>%s</b></h2>\n",$thumb->name);
    print_module_footer();
}