1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378:
<?php
if (isset($_REQUEST['performcrop'])) {
if (!defined('OFFSET_PATH'))
define('OFFSET_PATH', 3);
require_once(dirname(dirname(__FILE__)) . '/admin-globals.php');
require_once(dirname(dirname(__FILE__)) . '/functions-image.php');
admin_securityChecks(ALBUM_RIGHTS, $return = currentRelativeURL());
} else {
zp_register_filter('admin_toolbox_image', 'crop_image::toolbox');
zp_register_filter('edit_image_utilities', 'crop_image::edit', 99999);
$plugin_is_filter = 5 | ADMIN_PLUGIN;
$plugin_description = gettext("An image cropping tool.");
$plugin_author = "Stephen Billard (sbillard)";
$plugin_category = gettext('Media');
return;
}
class crop_image {
static function toolbox($albumname, $imagename) {
$album = newAlbum($albumname);
if ($album->isMyItem(ALBUM_RIGHTS)) {
$image = newimage($album, $imagename);
if (isImagePhoto($image)) {
?>
<li>
<a href="<?php echo WEBPATH . "/" . ZENFOLDER . '/' . PLUGIN_FOLDER; ?>/crop_image.php?a=<?php echo pathurlencode($albumname); ?>
&i=<?php echo urlencode($imagename); ?>&performcrop=frontend "><?php echo gettext("Crop image"); ?></a>
</li>
<?php
}
}
}
static function edit($output, $image, $prefix, $subpage, $tagsort) {
if (isImagePhoto($image)) {
if (is_array($image->filename)) {
$albumname = dirname($image->filename['source']);
$imagename = basename($image->filename['source']);
} else {
$albumname = $image->albumlink;
$imagename = $image->filename;
}
$output .=
'<div class="button buttons tooltip" title="' . gettext('Permanently crop the actual image.') . '">' . "\n" .
'<a href="' . WEBPATH . "/" . ZENFOLDER . '/' . PLUGIN_FOLDER . '/crop_image.php?a=' . pathurlencode($albumname) . "\n" .
'&i=' . urlencode($imagename) . '&performcrop=backend&subpage=' . $subpage . '&tagsort=' . html_encode($tagsort) . '">' . "\n" .
'<img src="images/shape_handles.png" alt="" />' . gettext("Crop image") . '</a>' . "\n" .
'<br class="clearall" />' .
'</div>' . "\n";
}
return $output;
}
}
$albumname = sanitize_path($_REQUEST['a']);
$imagename = sanitize_path($_REQUEST['i']);
$album = newAlbum($albumname);
if (!$album->isMyItem(ALBUM_RIGHTS)) {
if (!zp_apply_filter('admin_managed_albums_access', false, $return)) {
redirectURL(FULLWEBPATH . '/' . ZENFOLDER . '/admin.php?from=' . $return);
}
}
$use_side = getOption('image_use_side');
$albumobj = newAlbum($albumname);
$imageobj = newImage($albumobj, $imagename);
if (isImagePhoto($imageobj)) {
$imgpath = $imageobj->localpath;
$imagepart = basename($imgpath);
$timg = zp_imageGet($imgpath);
$width = $imageobj->getWidth();
$height = $imageobj->getHeight();
} else {
die(gettext('attempt to crop an object which is not an image.'));
}
switch ($use_side) {
case 'longest':
$size = min(400, $width, $height);
if ($width >= $height) {
$sr = $size / $width;
$sizedwidth = $size;
$sizedheight = round($height / $width * $size);
} else {
$sr = $size / $height;
$sizedwidth = Round($width / $height * $size);
$sizedheight = $size;
}
break;
case 'shortest':
$size = min(400, $width, $height);
if ($width < $height) {
$sr = $size / $width;
$sizedwidth = $size;
$sizedheight = round($height / $width * $size);
} else {
$sr = $size / $height;
$sizedwidth = Round($width / $height * $size);
$sizedheight = $size;
}
break;
case 'width':
$size = $width;
$sr = 1;
$sizedwidth = $size;
$sizedheight = round($height / $width * $size);
break;
case 'height':
$size = $height;
$sr = 1;
$sizedwidth = Round($width / $height * $size);
$sizedheight = $size;
break;
}
$args = array($size, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL);
$imageurl = getImageProcessorURI($args, $albumname, $imagepart);
$iW = round($sizedwidth * 0.9);
$iH = round($sizedheight * 0.9);
$iX = round($sizedwidth * 0.05);
$iY = round($sizedheight * 0.05);
if (isset($_REQUEST['crop'])) {
XSRFdefender('crop');
$cw = $_REQUEST['w'];
$ch = $_REQUEST['h'];
$cx = $_REQUEST['x'];
$cy = $_REQUEST['y'];
$rw = $width / $sizedwidth;
$rh = $height / $sizedheight;
$cw = round($cw * $rw);
$ch = round($ch * $rh);
$cx = round($cx * $rw);
$cy = round($cy * $rh);
$quality = getOption('full_image_quality');
$rotate = false;
if (zp_imageCanRotate()) {
$rotate = getImageRotation($imgpath);
}
if (DEBUG_IMAGE)
debugLog("image_crop: crop " . basename($imgpath) . ":\$cw=$cw, \$ch=$ch, \$cx=$cx, \$cy=$cy \$rotate=$rotate");
if ($rotate) {
$timg = zp_rotateImage($timg, $rotate);
}
$newim = zp_createImage($cw, $ch);
zp_resampleImage($newim, $timg, 0, 0, $cx, $cy, $cw, $ch, $cw, $ch, getSuffix($imagename));
@chmod($imgpath, 0777);
@unlink($imgpath);
if (zp_imageOutput($newim, getSuffix($imgpath), $imgpath, $quality)) {
if (DEBUG_IMAGE)
debugLog('image_crop Finished:' . basename($imgpath));
} else {
if (DEBUG_IMAGE)
debugLog('image_crop: failed to create ' . $imgpath);
}
@chmod($imgpath, FILE_MOD);
zp_imageKill($newim);
zp_imageKill($timg);
Gallery::clearCache(SERVERCACHE . '/' . $albumname);
$imageobj->set('EXIFOrientation', 0);
$imageobj->updateDimensions();
$imageobj->set('thumbX', NULL);
$imageobj->set('thumbY', NULL);
$imageobj->set('thumbW', NULL);
$imageobj->set('thumbH', NULL);
$imageobj->save();
if ($_REQUEST['performcrop'] == 'backend') {
$return = FULLWEBPATH . '/' . ZENFOLDER . '/admin-edit.php?page=edit&album=' . pathurlencode($albumname) . '&saved&subpage=' . sanitize($_REQUEST['subpage']) . '&tagsort=' . sanitize($_REQUEST['tagsort']) . '&tab=imageinfo';
} else {
$return = FULLWEBPATH . $imageobj->getLink();
}
redirectURL($return);
}
if (isset($_REQUEST['subpage'])) {
$subpage = sanitize($_REQUEST['subpage']);
$tagsort = sanitize($_REQUEST['tagsort']);
} else {
$subpage = $tagsort = '';
}
printAdminHeader('edit', gettext('crop image'));
?>
<script src="<?php echo WEBPATH . '/' . ZENFOLDER ?>/js/jquery.Jcrop.js" type="text/javascript"></script>
<link rel="stylesheet" href="<?php echo WEBPATH . '/' . ZENFOLDER ?>/js/jquery.Jcrop.css" type="text/css" />
<link rel="stylesheet" href="<?php echo WEBPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER ?>/crop_image/crop_image.css" type="text/css" />
<script type="text/javascript" >
var jcrop_api;
jQuery(window).load(function() {
initJcrop();
function initJcrop() {
jcrop_api = jQuery.Jcrop('#cropbox');
jcrop_api.setOptions({
onchange: showCoords,
onSelect: showCoords,
bgOpacity: .4,
bgColor: 'black'
});
jcrop_api.setOptions({aspectRatio: 0});
resetBoundingBox();
}
jQuery('#aspect-ratio-width').keyup(aspectChange);
jQuery('#aspect-ratio-height').keyup(aspectChange);
});
function clearAspect() {
jcrop_api.setOptions({aspectRatio: 0});
$('#aspect-ratio-width').val('');
$('#aspect-ratio-height').val('');
resetBoundingBox();
showCoords(jcrop_api.tellSelect());
}
function aspectChange() {
var aspectWidth = jQuery('#aspect-ratio-width').attr('value');
var aspectHeight = jQuery('#aspect-ratio-height').attr('value');
if (!aspectWidth)
aspectWidth = aspectHeight;
if (!aspectHeight)
aspectHeight = aspectWidth;
if (aspectHeight) {
jcrop_api.setOptions({aspectRatio: aspectWidth / aspectHeight});
} else {
jcrop_api.setOptions({aspectRatio: 0});
}
showCoords(jcrop_api.tellSelect());
}
function swapAspect() {
var aspectHeight = $('#aspect-ratio-width').val();
var aspectWidth = $('#aspect-ratio-height').val();
$('#aspect-ratio-width').val(aspectWidth);
$('#aspect-ratio-height').val(aspectHeight);
jcrop_api.setOptions({aspectRatio: aspectWidth / aspectHeight});
showCoords(jcrop_api.tellSelect());
}
function clearAspect() {
$('#aspect-ratio-width').val('');
$('#aspect-ratio-height').val('');
}
function showCoords(c) {
var new_width = Math.round(c.w * (<?php echo $width ?> /<?php echo $sizedwidth ?>));
var new_height = Math.round(c.h * (<?php echo $height ?> /<?php echo $sizedheight ?>));
jQuery('#x').val(c.x);
jQuery('#y').val(c.y);
jQuery('#x2').val(c.x2);
jQuery('#y2').val(c.y2);
jQuery('#w').val(c.w);
jQuery('#h').val(c.h);
jQuery('#new-width').text(new_width);
jQuery('#new-height').text(new_height);
}
function resetBoundingBox() {
jcrop_api.setSelect([<?php echo $iX; ?>, <?php echo $iY; ?>, <?php echo $iX + $iW; ?>, <?php echo $iY + $iH; ?>]);
}
function checkCoords() {
return true;
}
</script>
</head>
<body>
<?php printLogoAndLinks(); ?>
<div id="main">
<?php printTabs(); ?>
<div id="content">
<?php zp_apply_filter('admin_note', 'crop_image', ''); ?>
<h1><?php echo gettext("Image cropping") . ": <em>" . $albumobj->name . " (" . $albumobj->getTitle() . ") /" . $imageobj->filename . " (" . $imageobj->getTitle() . ")</em>"; ?></h1>
<div id="notice_div">
<p><?php echo gettext('You can crop your image by dragging the crop handles on the image'); ?></p>
<p id="notice" class="notebox" style="width:<?php echo $sizedwidth; ?>px" ><?php echo gettext('<strong>Note:</strong> If you save these changes they are permanent!'); ?></p>
</div>
<div style="display:block">
<div style="text-align:left; float: left;">
<div style="width: <?php echo $sizedwidth; ?>px; height: <?php echo $sizedheight; ?>px; margin-bottom: 10px; border: 4px solid gray;">
<!-- This is the image we're attaching Jcrop to -->
<img src="<?php echo html_encode(pathurlencode($imageurl)); ?>" id="cropbox" />
<p class="floatright">
<?php echo sprintf(gettext('(<span id="new-width">%1$u</span> x <span id="new-height">%2$u</span> pixels)'), round($iW * ($width / $sizedwidth)), round($iH * ($height / $sizedheight)));
?>
</p>
</div>
<span class="clearall" ></span>
<?php
printf(gettext('width:%1$s %2$s height:%3$s %4$s clear %5$s'), '<input type="text" id="aspect-ratio-width" name="aspect-ratio-width" value="" size="5" />', ' <span id="aspect" ><a id="swap_button" href="javascript:swapAspect();" title="' . gettext('swap width and height fields') . '" > <img src="crop_image/swap.png"> </a></span> ', '<input type="text" id="aspect-ratio-height" name="aspect-ratio-height" value="" size="5" />', '<a href="javascript:clearAspect();" title="' . gettext('clear width and height fields') . '" >', '</a>')
?>
<!-- This is the form that our event handler fills -->
<form class="dirty-check" name="crop" id="crop" action="?crop" onsubmit="return checkCoords();" autocomplete="off">
<?php XSRFToken('crop'); ?>
<input type="hidden" size="4" id="x" name="x" value="<?php echo $iX ?>" />
<input type="hidden" size="4" id="y" name="y" value="<?php echo $iY ?>" />
<input type="hidden" size="4" id="x2" name="x2" value="<?php echo $iX + $iW ?>" />
<input type="hidden" size="4" id="y2" name="y2" value="<?php echo $iY + $iH ?>" />
<input type="hidden" size="4" id="w" name="w" value="<?php echo $iW ?>" />
<input type="hidden" size="4" id="h" name="h" value="<?php echo $iH ?>" />
<input type="hidden" id="a" name="a" value="<?php echo html_encode($albumname); ?>" />
<input type="hidden" id="i" name="i" value="<?php echo html_encode($imagename); ?>" />
<input type="hidden" id="tagsort" name="tagsort" value="<?php echo html_encode($tagsort); ?>" />
<input type="hidden" id="subpage" name="subpage" value="<?php echo html_encode($subpage); ?>" />
<input type="hidden" id="crop" name="crop" value="crop" />
<input type="hidden" id="performcrop" name="performcrop" value="<?php echo html_encode(sanitize($_REQUEST['performcrop'])); ?>" />
<p class="buttons">
<button type="button" onclick="clearAspect();" >
<img src="../images/fail.png" alt="" /><strong><?php echo gettext("Reset"); ?></strong>
</button>
<button type="submit" id="submit" name="submit" value="<?php echo gettext('Apply the cropping') ?>">
<img src="../images/pass.png" alt="" /><strong><?php echo gettext("Apply"); ?></strong>
</button>
<?php
if ($_REQUEST['performcrop'] == 'backend') {
?>
<button type="reset" value="<?php echo gettext('Back') ?>" onclick="window.location = '../admin-edit.php?page=edit&album=<?php echo pathurlencode($albumname); ?>&subpage=<?php echo $subpage; ?>&tagsort=<?php echo html_encode($tagsort); ?>&tab=imageinfo'">
<img src="../images/arrow_left_blue_round.png" alt="" /><strong><?php echo gettext("Back"); ?></strong>
</button>
<?php
} else {
?>
<button type="reset" value="<?php echo gettext('Back') ?>" onclick="window.location = '../../index.php?album=<?php echo pathurlencode($albumname); ?>&image=<?php echo urlencode($imagename); ?>'">
<img src="../images/arrow_left_blue_round.png" alt="" /><strong><?php echo gettext("Back"); ?></strong>
</button>
<?php
}
?>
</p>
<br />
</form>
</div>
<br style="clear: both" />
</div><!-- block -->
</div><!-- content -->
<?php printAdminFooter(); ?>
</div><!-- main -->
</body>
</html>