photos_resize.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /* Copyright (C) 2010-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009 Meos
  4. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/photos_resize.php
  21. * \ingroup core
  22. * \brief File of page to resize photos
  23. */
  24. //if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1');
  25. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
  26. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  27. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  28. //if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  29. //if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  30. //if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show
  31. //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
  32. //if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  33. //if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
  34. require '../main.inc.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  38. $langs->load("products");
  39. $langs->load("other");
  40. $id=GETPOST('id','int');
  41. $action=GETPOST('action','alpha');
  42. $modulepart=GETPOST('modulepart','alpha')?GETPOST('modulepart','alpha'):'produit|service';
  43. $original_file = isset($_REQUEST["file"])?urldecode($_REQUEST["file"]):'';
  44. // Security check
  45. if (empty($modulepart)) accessforbidden('Bad value for modulepart');
  46. $accessallowed=0;
  47. if ($modulepart=='produit|service')
  48. {
  49. $result=restrictedArea($user,'produit|service',$id,'product&product');
  50. if ($modulepart=='produit|service' && (! $user->rights->produit->lire && ! $user->rights->service->lire)) accessforbidden();
  51. $accessallowed=1;
  52. }
  53. // Security:
  54. // Limit access if permissions are wrong
  55. if (! $accessallowed)
  56. {
  57. accessforbidden();
  58. }
  59. $object = new Product($db);
  60. if ($id > 0)
  61. {
  62. $result = $object->fetch($id);
  63. if ($result <= 0) dol_print_error($db,'Failed to load object');
  64. $dir=$conf->product->multidir_output[$object->entity]; // By default
  65. if ($object->type == 0) $dir=$conf->product->multidir_output[$object->entity];
  66. if ($object->type == 1) $dir=$conf->service->multidir_output[$object->entity];
  67. }
  68. /*
  69. * Actions
  70. */
  71. if ($action == 'confirm_resize' && (isset($_POST["file"]) != "") && (isset($_POST["sizex"]) != "") && (isset($_POST["sizey"]) != ""))
  72. {
  73. $fullpath=$dir."/".$original_file;
  74. $result=dol_imageResizeOrCrop($fullpath,0,$_POST['sizex'],$_POST['sizey']);
  75. if ($result == $fullpath)
  76. {
  77. header("Location: ".DOL_URL_ROOT."/product/photos.php?id=".$id.'&action=addthumb&file='.urldecode($_POST["file"]));
  78. exit;
  79. }
  80. else
  81. {
  82. $mesg=$result;
  83. $_GET['file']=$_POST["file"];
  84. }
  85. }
  86. // Crop d'une image
  87. if ($action == 'confirm_crop')
  88. {
  89. $fullpath=$dir."/".$original_file;
  90. $result=dol_imageResizeOrCrop($fullpath,1,$_POST['w'],$_POST['h'],$_POST['x'],$_POST['y']);
  91. if ($result == $fullpath)
  92. {
  93. header("Location: ".DOL_URL_ROOT."/product/photos.php?id=".$id.'&action=addthumb&file='.urldecode($_POST["file"]));
  94. exit;
  95. }
  96. else
  97. {
  98. $mesg=$result;
  99. $_GET['file']=$_POST["file"];
  100. }
  101. }
  102. /*
  103. * View
  104. */
  105. llxHeader($head, $langs->trans("Image"), '', '', 0, 0, array('/includes/jquery/plugins/jcrop/js/jquery.Jcrop.min.js','/core/js/lib_photosresize.js'), array('/includes/jquery/plugins/jcrop/css/jquery.Jcrop.css'));
  106. print_fiche_titre($langs->trans("ImageEditor"));
  107. if ($mesg) print '<div class="error">'.$mesg.'</div>';
  108. $infoarray=dol_getImageSize($dir."/".urldecode($_GET["file"]));
  109. $height=$infoarray['height'];
  110. $width=$infoarray['width'];
  111. print $langs->trans("CurrentInformationOnImage").': ';
  112. print $langs->trans("Width").': <strong>'.$width.'</strong> x '.$langs->trans("Height").': <strong>'.$height.'</strong><br>';
  113. print '<br>'."\n";
  114. print '<!-- Form to resize -->'."\n";
  115. print '<form name="redim_file" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
  116. print '<fieldset id="redim_file">';
  117. print '<legend>'.$langs->trans("Resize").'</legend>';
  118. print $langs->trans("ResizeDesc").'<br>';
  119. print $langs->trans("NewLength").': <input class="flat" name="sizex" size="10" type="text" > px &nbsp; '.$langs->trans("or").' &nbsp; ';
  120. print $langs->trans("NewHeight").': <input class="flat" name="sizey" size="10" type="text" > px &nbsp; <br>';
  121. print '<input type="hidden" name="file" value="'.$_GET['file'].'" />';
  122. print '<input type="hidden" name="action" value="confirm_resize" />';
  123. print '<input type="hidden" name="product" value="'.$id.'" />';
  124. print '<input type="hidden" name="id" value="'.$id.'" />';
  125. print '<br><input class="button" name="sendit" value="'.dol_escape_htmltag($langs->trans("Resize")).'" type="submit" />';
  126. print '</fieldset>'."\n";
  127. print '</form>';
  128. print '<br>'."\n";
  129. /*
  130. * Crop image
  131. */
  132. print '<br>'."\n";
  133. if (! empty($conf->use_javascript_ajax))
  134. {
  135. $infoarray=dol_getImageSize($dir."/".urldecode($_GET["file"]));
  136. $height=$infoarray['height'];
  137. $width=$infoarray['width'];
  138. $widthforcrop=$width; $refsizeforcrop='orig'; $ratioforcrop=1;
  139. if (! empty($_SESSION['dol_screenwidth']) && ($widthforcrop > round($_SESSION['dol_screenwidth']/2)))
  140. {
  141. $widthforcrop=min(round($_SESSION['dol_screenwidth']/2),$widthforcrop);
  142. $refsizeforcrop='screenwidth';
  143. $ratioforcrop=2;
  144. }
  145. print '<!-- Form to crop -->'."\n";
  146. print '<fieldset id="redim_file">';
  147. print '<legend>'.$langs->trans("Recenter").'</legend>';
  148. print $langs->trans("DefineNewAreaToPick").'...<br>';
  149. print '<br><center>';
  150. print '<div style="border: 1px solid #888888; width: '.$widthforcrop.'px;">';
  151. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$object->entity.'&file='.$original_file.'" alt="" id="cropbox" width="'.$widthforcrop.'px"/>';
  152. print '</div>';
  153. print '</center><br>';
  154. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="post" onsubmit="return checkCoords();">
  155. <div class="jc_coords">
  156. '.$langs->trans("NewSizeAfterCropping").':
  157. <label>X1 <input type="text" size="4" id="x" name="x" /></label>
  158. <label>Y1 <input type="text" size="4" id="y" name="y" /></label>
  159. <label>X2 <input type="text" size="4" id="x2" name="x2" /></label>
  160. <label>Y2 <input type="text" size="4" id="y2" name="y2" /></label>
  161. <label>W <input type="text" size="4" id="w" name="w" /></label>
  162. <label>H <input type="text" size="4" id="h" name="h" /></label>
  163. </div>
  164. <input type="hidden" id="file" name="file" value="'.urlencode($original_file).'" />
  165. <input type="hidden" id="action" name="action" value="confirm_crop" />
  166. <input type="hidden" id="product" name="product" value="'.$id.'" />
  167. <input type="hidden" id="refsizeforcrop" name="refsizeforcrop" value="'.$refsizeforcrop.'" />
  168. <input type="hidden" id="ratioforcrop" name="ratioforcrop" value="'.$ratioforcrop.'" />
  169. <input type="hidden" name="id" value="'.$id.'" />
  170. <br><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Recenter")).'" />
  171. </form>'."\n";
  172. print '</fieldset>'."\n";
  173. print '<br>';
  174. }
  175. llxFooter();
  176. $db->close();