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