PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/FSN/mediatheque/zp-core/admin-thumbcrop.php

https://gitlab.com/r.collas/site_central
PHP | 314 lines | 283 code | 23 blank | 8 comment | 32 complexity | bee282fe8f2535cc01fcc706d93aa942 MD5 | raw file
  1. <?php
  2. define('OFFSET_PATH', 1);
  3. require_once(dirname(__FILE__) . '/admin-globals.php');
  4. require_once(dirname(__FILE__) . '/functions-image.php');
  5. admin_securityChecks(ALBUM_RIGHTS, $return = currentRelativeURL());
  6. $albumname = sanitize_path($_REQUEST['a']);
  7. $imagename = sanitize_path($_REQUEST['i']);
  8. $albumobj = newAlbum($albumname);
  9. if (!$albumobj->isMyItem(ALBUM_RIGHTS)) { // prevent nefarious access to this page.
  10. if (!zp_apply_filter('admin_managed_albums_access', false, $return)) {
  11. header('Location: ' . FULLWEBPATH . '/' . ZENFOLDER . '/admin.php?from=' . $return);
  12. exitZP();
  13. }
  14. }
  15. // get what image side is being used for resizing
  16. $use_side = getOption('image_use_side');
  17. // get full width and height
  18. $imageobj = newImage($albumobj, $imagename);
  19. $currentthumbimage = $imageobj->getThumb();
  20. setOption('image_use_side', 'longest', false);
  21. $cropwidth = getOption("thumb_crop_width");
  22. $cropheight = getOption("thumb_crop_height");
  23. $imagepart = $imagename;
  24. if (isImagePhoto($imageobj)) {
  25. $width = $imageobj->getWidth();
  26. $height = $imageobj->getHeight();
  27. } else {
  28. $imgpath = $imageobj->getThumbImageFile();
  29. $imagepart = basename($imgpath);
  30. $timg = zp_imageGet($imgpath);
  31. $width = zp_imageWidth($timg);
  32. $height = zp_imageHeight($timg);
  33. }
  34. if (getOption('thumb_crop')) {
  35. $thumbcropwidth = $cropwidth;
  36. $thumbcropheight = $cropheight;
  37. } else {
  38. if (isImagePhoto($imageobj)) {
  39. $thumbcropwidth = $imageobj->getWidth();
  40. $thumbcropheight = $imageobj->getHeight();
  41. } else {
  42. $imgpath = $imageobj->getThumbImageFile();
  43. $imagepart = basename($imgpath);
  44. $thumbcropwidth = zp_imageWidth($timg);
  45. $thumbcropheight = zp_imageHeight($timg);
  46. }
  47. $tsize = getOption('thumb_size');
  48. $max = max($thumbcropwidth, $thumbcropheight);
  49. $thumbcropwidth = $thumbcropwidth * ($tsize / $max);
  50. $thumbcropheight = $thumbcropheight * ($tsize / $max);
  51. }
  52. // get appropriate $sizedwidth and $sizedheight
  53. switch ($use_side) {
  54. case 'longest':
  55. $size = min(400, $width, $height);
  56. if ($width >= $height) {
  57. $sr = $size / $width;
  58. $sizedwidth = $size;
  59. $sizedheight = round($height / $width * $size);
  60. } else {
  61. $sr = $size / $height;
  62. $sizedwidth = Round($width / $height * $size);
  63. $sizedheight = $size;
  64. }
  65. break;
  66. case 'shortest':
  67. $size = min(400, $width, $height);
  68. if ($width < $height) {
  69. $sr = $size / $width;
  70. $sizedwidth = $size;
  71. $sizedheight = round($height / $width * $size);
  72. } else {
  73. $sr = $size / $height;
  74. $sizedwidth = Round($width / $height * $size);
  75. $sizedheight = $size;
  76. }
  77. break;
  78. case 'width':
  79. $size = $width;
  80. $sr = 1;
  81. $sizedwidth = $size;
  82. $sizedheight = round($height / $width * $size);
  83. break;
  84. case 'height':
  85. $size = $height;
  86. $sr = 1;
  87. $sizedwidth = Round($width / $height * $size);
  88. $sizedheight = $size;
  89. break;
  90. }
  91. $args = array($size, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL);
  92. $imageurl = getImageProcessorURI($args, $albumname, $imagepart);
  93. $isCrop = $imageobj->get('thumbY');
  94. $iY = round($isCrop * $sr);
  95. $cr = max($cropwidth, $cropheight) / max(1, getOption('thumb_size'));
  96. $si = min($sizedwidth, $sizedheight);
  97. $oW = round($si * $cr);
  98. $oH = round($si * $cr);
  99. $oX = round(($sizedwidth - $oW) / 2);
  100. $oY = round(($sizedheight - $oH) / 2);
  101. if (is_null($isCrop)) {
  102. $iW = $oW;
  103. $iH = $oH;
  104. $iX = $oX;
  105. $iY = $oY;
  106. } else {
  107. $iX = round($imageobj->get('thumbX') * $sr);
  108. $iW = round($imageobj->get('thumbW') * $sr);
  109. $iH = round($imageobj->get('thumbH') * $sr);
  110. }
  111. if (isset($_REQUEST['crop'])) {
  112. XSRFdefender('thumb_crop');
  113. $cw = $_REQUEST['w'];
  114. $ch = $_REQUEST['h'];
  115. $cx = $_REQUEST['x'];
  116. $cy = $_REQUEST['y'];
  117. if (DEBUG_IMAGE)
  118. debugLog("Thumbcrop-in: \$width=$width \$height=$height \$cx=$cx \$cy=$cy \$cw=$cw \$ch=$ch");
  119. if (isset($_REQUEST['clear_crop']) || ($cw == 0 && $ch == 0)) {
  120. $cx = $cy = $cw = $ch = NULL;
  121. } else {
  122. $rw = $width / $sizedwidth;
  123. $rh = $height / $sizedheight;
  124. $cw = round($cw * $rw);
  125. if ($cropwidth == $cropheight) {
  126. $ch = $cw;
  127. } else {
  128. $ch = round($ch * $rh);
  129. }
  130. $cx = round($cx * $rw);
  131. $cy = round($cy * $rh);
  132. }
  133. if (DEBUG_IMAGE)
  134. debugLog("Thumbcrop-out: \$cx=$cx \$cy=$cy \$cw=$cw \$ch=$ch");
  135. $imageobj->set('thumbX', $cx);
  136. $imageobj->set('thumbY', $cy);
  137. $imageobj->set('thumbW', $cw);
  138. $imageobj->set('thumbH', $ch);
  139. $imageobj->save();
  140. $return = '/admin-edit.php?page=edit&album=' . html_encode(pathurlencode($albumname)) . '&saved&subpage=' . html_encode(sanitize($_REQUEST['subpage'])) . '&tagsort=' . html_encode(sanitize($_REQUEST['tagsort'])) . '&tab=imageinfo';
  141. header('Location: ' . FULLWEBPATH . '/' . ZENFOLDER . $return);
  142. exitZP();
  143. }
  144. $subpage = sanitize($_REQUEST['subpage']);
  145. $tagsort = sanitize($_REQUEST['tagsort']);
  146. printAdminHeader('edit', 'thumbcrop');
  147. ?>
  148. <script src="js/jquery.Jcrop.js" type="text/javascript"></script>
  149. <link rel="stylesheet" href="js/jquery.Jcrop.css" type="text/css" />
  150. <script type="text/javascript" >
  151. //<!-- <![CDATA[
  152. var jcrop_api;
  153. jQuery(window).load(function() {
  154. initJcrop();
  155. function initJcrop() {
  156. jcrop_api = jQuery.Jcrop('#cropbox');
  157. jcrop_api.setOptions({
  158. onchange: showPreview,
  159. onSelect: showPreview,
  160. bgOpacity: .4,
  161. bgColor: 'black'
  162. });
  163. jcrop_api.setOptions({aspectRatio: <?php echo $cropwidth . '/' . $cropheight; ?>});
  164. resetBoundingBox();
  165. }
  166. ;
  167. });
  168. function resetCheck() {
  169. if ($('#clear_crop').prop('checked')) {
  170. jcrop_api.setSelect([<?php echo $oX; ?>, <?php echo $oY; ?>, <?php echo $oX + $oW; ?>, <?php echo $oY + $oH; ?>]);
  171. }
  172. }
  173. function resetBoundingBox() {
  174. if ($('#clear_crop').prop('checked')) {
  175. jcrop_api.setSelect([<?php echo $oX; ?>, <?php echo $oY; ?>, <?php echo $oX + $oW; ?>, <?php echo $oY + $oH; ?>]);
  176. } else {
  177. jcrop_api.setSelect([<?php echo $iX; ?>, <?php echo $iY; ?>, <?php echo $iX + $iW; ?>, <?php echo $iY + $iH; ?>]);
  178. }
  179. }
  180. // Our simple event handler, called from onchange and onSelect
  181. // event handlers, as per the Jcrop invocation above
  182. function showPreview(coords) {
  183. var rx = <?php echo $cropwidth; ?> / coords.w;
  184. var ry = <?php echo $cropheight; ?> / coords.h;
  185. jQuery('#preview').css({
  186. width: Math.round(rx * <?php echo $sizedwidth; ?>) + 'px', // we need to calcutate the resized width and height here...
  187. height: Math.round(ry * <?php echo $sizedheight; ?>) + 'px',
  188. marginLeft: '-' + Math.round(rx * coords.x) + 'px',
  189. marginTop: '-' + Math.round(ry * coords.y) + 'px'
  190. });
  191. jQuery('#x').val(coords.x);
  192. jQuery('#y').val(coords.y);
  193. jQuery('#x2').val(coords.x2);
  194. jQuery('#y2').val(coords.y2);
  195. jQuery('#w').val(coords.w);
  196. jQuery('#h').val(coords.h);
  197. }
  198. function checkCoords() {
  199. return true;
  200. }
  201. ;
  202. // ]]> -->
  203. </script>
  204. </head>
  205. <body>
  206. <?php printLogoAndLinks(); ?>
  207. <div id="main">
  208. <?php printTabs(); ?>
  209. <div id="content">
  210. <h1><?php echo gettext("Custom thumbnail cropping") . ": <em>" . $albumobj->name . " (" . $albumobj->getTitle() . ") /" . $imageobj->filename . " (" . $imageobj->getTitle() . ")</em>"; ?></h1>
  211. <p><?php echo gettext("You can change the portion of your image which is shown in thumbnails by cropping it here."); ?></p>
  212. <div style="display:block">
  213. <div style="float: left; width:<?php echo $thumbcropwidth; ?>px; text-align: center;margin-right: 18px; margin-bottom: 10px;">
  214. <img src="<?php echo html_encode(pathurlencode($currentthumbimage)); ?>" style="width:<?php echo $thumbcropwidth; ?>px;height:<?php echo $thumbcropheight; ?>px; border: 4px solid gray; float: left"/>
  215. <?php echo gettext("current thumbnail"); ?>
  216. </div>
  217. <div style="text-align:left; float: left;">
  218. <div style="width: <?php echo $sizedwidth; ?>px; height: <?php echo $sizedheight; ?>px; margin-bottom: 10px; border: 4px solid gray;">
  219. <!-- This is the image we're attaching Jcrop to -->
  220. <img src="<?php echo $imageurl; ?>" id="cropbox" />
  221. </div>
  222. <!-- This is the form that our event handler fills -->
  223. <form class="dirty-check" name="crop" id="crop" action="?crop" onsubmit="return checkCoords();">
  224. <?php XSRFToken('thumb_crop'); ?>
  225. <input type="hidden" size="4" id="x" name="x" value="<?php echo $iX ?>" />
  226. <input type="hidden" size="4" id="y" name="y" value="<?php echo $iY ?>" />
  227. <input type="hidden" size="4" id="x2" name="x2" value="<?php echo $iX + $iW ?>" />
  228. <input type="hidden" size="4" id="y2" name="y2" value="<?php echo $iY + $iH ?>" />
  229. <input type="hidden" size="4" id="w" name="w" value="<?php echo $iW ?>" />
  230. <input type="hidden" size="4" id="h" name="h" value="<?php echo $iH ?>" />
  231. <input type="hidden" id="cropw" name="cropw" value="<?php echo $cropwidth; ?>" />
  232. <input type="hidden" id="croph" name="croph" value="<?php echo $cropheight; ?>" />
  233. <input type="hidden" id="a" name="a" value="<?php echo html_encode($albumname); ?>" />
  234. <input type="hidden" id="i" name="i" value="<?php echo html_encode($imagename); ?>" />
  235. <input type="hidden" id="tagsort" name="tagsort" value="<?php echo html_encode($tagsort); ?>" />
  236. <input type="hidden" id="subpage" name="subpage" value="<?php echo html_encode($subpage); ?>" />
  237. <input type="hidden" id="crop" name="crop" value="crop" />
  238. <?php
  239. if (getOption('thumb_crop')) {
  240. ?>
  241. <input name="clear_crop" id="clear_crop" type="checkbox" value="1" onclick="resetCheck();" /> <?php echo gettext("Reset to the default cropping"); ?><br />
  242. <br />
  243. <p class="buttons">
  244. <button type="button" onclick="resetBoundingBox();" >
  245. <img src="images/fail.png" alt="" /><strong><?php echo gettext("Reset"); ?></strong>
  246. </button>
  247. <button type="submit" id="submit" name="submit" value="<?php echo gettext('Apply the cropping') ?>">
  248. <img src="images/pass.png" alt="" />
  249. <strong><?php echo gettext("Apply"); ?></strong>
  250. </button>
  251. <button type="reset" value="<?php echo gettext('Back') ?>" onclick="window.location = 'admin-edit.php?page=edit&album=<?php echo html_encode(pathurlencode($albumname)); ?>&subpage=<?php echo html_encode($subpage); ?>&tagsort=<?php echo html_encode($tagsort); ?>&tab=imageinfo'">
  252. <img src="images/arrow_left_blue_round.png" alt="" />
  253. <strong><?php echo gettext("Back"); ?></strong>
  254. </button>
  255. </p><br />
  256. <?php
  257. } else {
  258. echo gettext('Thumbnail cropping is disabled. Enable this option for the theme if you wish cropped thumbnails.');
  259. }
  260. ?>
  261. </form>
  262. </div>
  263. <div style="float: left; width:<?php echo $cropwidth; ?>px; text-align: center; margin-left: 10px; margin-bottom: 10px;">
  264. <div style="width:<?php echo $cropwidth; ?>px;height:<?php echo $cropheight; ?>px; overflow:hidden; border: 4px solid green; float: left">
  265. <img src="<?php echo html_encode(pathurlencode($imageurl)); ?>" id="preview" />
  266. </div>
  267. <?php echo gettext("thumbnail preview"); ?>
  268. </div>
  269. <!-- set the initial view for the preview -->
  270. <script type="text/javascript" >
  271. // <!-- <![CDATA[
  272. jQuery('#preview').css({
  273. width: '<?php echo round($cropwidth / $iW * $sizedwidth); ?>px',
  274. height: '<?php echo round($cropheight / $iH * $sizedheight); ?>px',
  275. marginLeft: '-<?php echo round($cropwidth / $iW * $iX); ?>px',
  276. marginTop: '-<?php echo round($cropheight / $iH * $iY); ?>px'
  277. });
  278. // ]]> -->
  279. </script>
  280. <br style="clear: both" />
  281. </div><!-- block -->
  282. </div><!-- content -->
  283. <?php printAdminFooter(); ?>
  284. </div><!-- main -->
  285. </body>
  286. </html>