photos_resize.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /* Copyright (C) 2010-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009 Meos
  4. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/photos_resize.php
  22. * \ingroup core
  23. * \brief File of page to resize photos
  24. */
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  28. $langs->load("products");
  29. $langs->load("other");
  30. $id=GETPOST('id','int');
  31. $action=GETPOST('action','alpha');
  32. $modulepart=GETPOST('modulepart','alpha')?GETPOST('modulepart','alpha'):'produit|service';
  33. $original_file = GETPOST("file");
  34. $backtourl=GETPOST('backtourl');
  35. $cancel=GETPOST("cancel");
  36. // Security check
  37. if (empty($modulepart)) accessforbidden('Bad value for modulepart');
  38. $accessallowed=0;
  39. if ($modulepart == 'produit' || $modulepart == 'product' || $modulepart == 'service' || $modulepart == 'produit|service')
  40. {
  41. $result=restrictedArea($user,'produit|service',$id,'product&product');
  42. if ($modulepart=='produit|service' && (! $user->rights->produit->lire && ! $user->rights->service->lire)) accessforbidden();
  43. $accessallowed=1;
  44. }
  45. elseif ($modulepart == 'project')
  46. {
  47. $result=restrictedArea($user,'projet',$id);
  48. if (! $user->rights->projet->lire) accessforbidden();
  49. $accessallowed=1;
  50. }
  51. elseif ($modulepart == 'holiday')
  52. {
  53. $result=restrictedArea($user,'holiday',$id,'holiday');
  54. if (! $user->rights->holiday->read) accessforbidden();
  55. $accessallowed=1;
  56. }
  57. elseif ($modulepart == 'expensereport')
  58. {
  59. $result=restrictedArea($user,'expensereport',$id,'expensereport');
  60. if (! $user->rights->expensereport->lire) accessforbidden();
  61. $accessallowed=1;
  62. }
  63. elseif ($modulepart == 'user')
  64. {
  65. $result=restrictedArea($user,'user',$id,'user');
  66. if (! $user->rights->user->user->lire) accessforbidden();
  67. $accessallowed=1;
  68. }
  69. // Security:
  70. // Limit access if permissions are wrong
  71. if (! $accessallowed)
  72. {
  73. accessforbidden();
  74. }
  75. // Define dir according to modulepart
  76. if ($modulepart == 'produit' || $modulepart == 'product' || $modulepart == 'service' || $modulepart == 'produit|service')
  77. {
  78. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  79. $object = new Product($db);
  80. if ($id > 0)
  81. {
  82. $result = $object->fetch($id);
  83. if ($result <= 0) dol_print_error($db,'Failed to load object');
  84. $dir=$conf->product->multidir_output[$object->entity]; // By default
  85. if ($object->type == Product::TYPE_PRODUCT) $dir=$conf->product->multidir_output[$object->entity];
  86. if ($object->type == Product::TYPE_SERVICE) $dir=$conf->service->multidir_output[$object->entity];
  87. }
  88. }
  89. elseif ($modulepart == 'project')
  90. {
  91. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  92. $object = new Project($db);
  93. if ($id > 0)
  94. {
  95. $result = $object->fetch($id);
  96. if ($result <= 0) dol_print_error($db,'Failed to load object');
  97. $dir=$conf->projet->dir_output; // By default
  98. }
  99. }
  100. elseif ($modulepart == 'holiday')
  101. {
  102. require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
  103. $object = new Holiday($db);
  104. if ($id > 0)
  105. {
  106. $result = $object->fetch($id);
  107. if ($result <= 0) dol_print_error($db,'Failed to load object');
  108. $dir=$conf->holiday->dir_output; // By default
  109. }
  110. }
  111. elseif ($modulepart == 'user')
  112. {
  113. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  114. $object = new User($db);
  115. if ($id > 0)
  116. {
  117. $result = $object->fetch($id);
  118. if ($result <= 0) dol_print_error($db,'Failed to load object');
  119. $dir=$conf->user->dir_output; // By default
  120. }
  121. }
  122. elseif ($modulepart == 'expensereport')
  123. {
  124. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  125. $object = new ExpenseReport($db);
  126. if ($id > 0)
  127. {
  128. $result = $object->fetch($id);
  129. if ($result <= 0) dol_print_error($db,'Failed to load object');
  130. $dir=$conf->expensereport->dir_output; // By default
  131. }
  132. }
  133. if (empty($backtourl))
  134. {
  135. if (in_array($modulepart, array('product','produit','service','produit|service'))) $backtourl=DOL_URL_ROOT."/product/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  136. else if (in_array($modulepart, array('expensereport'))) $backtourl=DOL_URL_ROOT."/expensereport/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  137. else if (in_array($modulepart, array('holiday'))) $backtourl=DOL_URL_ROOT."/holiday/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  138. else if (in_array($modulepart, array('project'))) $backtourl=DOL_URL_ROOT."/projet/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  139. else if (in_array($modulepart, array('user'))) $backtourl=DOL_URL_ROOT."/user/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  140. }
  141. /*
  142. * Actions
  143. */
  144. if ($cancel)
  145. {
  146. if ($backtourl)
  147. {
  148. header("Location: ".$backtourl);
  149. exit;
  150. }
  151. else
  152. {
  153. dol_print_error('', 'Cancel on photo_resize with a not supported value of modulepart='.$modulepart);
  154. exit;
  155. }
  156. }
  157. if ($action == 'confirm_resize' && (isset($_POST["file"]) != "") && (isset($_POST["sizex"]) != "") && (isset($_POST["sizey"]) != ""))
  158. {
  159. $fullpath=$dir."/".$original_file;
  160. $result=dol_imageResizeOrCrop($fullpath,0,$_POST['sizex'],$_POST['sizey']);
  161. if ($result == $fullpath)
  162. {
  163. $object->addThumbs($fullpath);
  164. // Update/create database for file $fullpath
  165. $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $fullpath);
  166. $rel_filename = preg_replace('/^[\\/]/','',$rel_filename);
  167. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  168. $ecmfile=new EcmFiles($db);
  169. $result = $ecmfile->fetch(0, '', $rel_filename);
  170. if ($result > 0) // If found
  171. {
  172. $filename = basename($rel_filename);
  173. $rel_dir = dirname($rel_filename);
  174. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  175. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  176. $ecmfile->label = md5_file(dol_osencode($fullpath));
  177. $result = $ecmfile->update($user);
  178. }
  179. elseif ($result == 0) // If not found
  180. {
  181. $filename = basename($rel_filename);
  182. $rel_dir = dirname($rel_filename);
  183. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  184. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  185. $ecmfile->filepath = $rel_dir;
  186. $ecmfile->filename = $filename;
  187. $ecmfile->label = md5_file(dol_osencode($fullpath)); // $fullpath is a full path to file
  188. $ecmfile->fullpath_orig = $fullpath;
  189. $ecmfile->gen_or_uploaded = 'unknown';
  190. $ecmfile->description = ''; // indexed content
  191. $ecmfile->keyword = ''; // keyword content
  192. $result = $ecmfile->create($user);
  193. if ($result < 0)
  194. {
  195. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  196. }
  197. $result = $ecmfile->create($user);
  198. }
  199. if ($backtourl)
  200. {
  201. header("Location: ".$backtourl);
  202. exit;
  203. }
  204. else
  205. {
  206. dol_print_error('', 'confirm_resize on photo_resize without backtourl defined for modulepart='.$modulepart);
  207. exit;
  208. }
  209. }
  210. else
  211. {
  212. setEventMessages($result, null, 'errors');
  213. $_GET['file']=$_POST["file"];
  214. $action='';
  215. }
  216. }
  217. // Crop d'une image
  218. if ($action == 'confirm_crop')
  219. {
  220. $fullpath=$dir."/".$original_file;
  221. $result=dol_imageResizeOrCrop($fullpath,1,$_POST['w'],$_POST['h'],$_POST['x'],$_POST['y']);
  222. if ($result == $fullpath)
  223. {
  224. $object->addThumbs($fullpath);
  225. // Update/create database for file $fullpath
  226. $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $fullpath);
  227. $rel_filename = preg_replace('/^[\\/]/','',$rel_filename);
  228. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  229. $ecmfile=new EcmFiles($db);
  230. $result = $ecmfile->fetch(0, '', $rel_filename);
  231. if ($result > 0) // If found
  232. {
  233. $filename = basename($rel_filename);
  234. $rel_dir = dirname($rel_filename);
  235. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  236. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  237. $ecmfile->label = md5_file(dol_osencode($fullpath));
  238. $result = $ecmfile->update($user);
  239. }
  240. elseif ($result == 0) // If not found
  241. {
  242. $filename = basename($rel_filename);
  243. $rel_dir = dirname($rel_filename);
  244. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  245. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  246. $ecmfile->filepath = $rel_dir;
  247. $ecmfile->filename = $filename;
  248. $ecmfile->label = md5_file(dol_osencode($fullpath)); // $fullpath is a full path to file
  249. $ecmfile->fullpath_orig = $fullpath;
  250. $ecmfile->gen_or_uploaded = 'unknown';
  251. $ecmfile->description = ''; // indexed content
  252. $ecmfile->keyword = ''; // keyword content
  253. $result = $ecmfile->create($user);
  254. if ($result < 0)
  255. {
  256. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  257. }
  258. $result = $ecmfile->create($user);
  259. }
  260. if ($backtourl)
  261. {
  262. header("Location: ".$backtourl);
  263. exit;
  264. }
  265. else
  266. {
  267. dol_print_error('', 'confirm_crop on photo_resize without backtourl defined for modulepart='.$modulepart);
  268. exit;
  269. }
  270. }
  271. else
  272. {
  273. setEventMessages($result, null, 'errors');
  274. $_GET['file']=$_POST["file"];
  275. $action='';
  276. }
  277. }
  278. /*
  279. * View
  280. */
  281. 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'));
  282. print load_fiche_titre($langs->trans("ImageEditor"));
  283. $infoarray=dol_getImageSize($dir."/".GETPOST("file"));
  284. $height=$infoarray['height'];
  285. $width=$infoarray['width'];
  286. print $langs->trans("CurrentInformationOnImage").': ';
  287. print $langs->trans("Width").': <strong>'.$width.'</strong> x '.$langs->trans("Height").': <strong>'.$height.'</strong><br>';
  288. print '<br>'."\n";
  289. /*
  290. * Resize image
  291. */
  292. print '<!-- Form to resize -->'."\n";
  293. print '<form name="redim_file" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
  294. print '<fieldset id="redim_file">';
  295. print '<legend>'.$langs->trans("Resize").'</legend>';
  296. print $langs->trans("ResizeDesc").'<br>';
  297. print $langs->trans("NewLength").': <input name="sizex" type="number" class="flat maxwidth50"> px &nbsp; '.$langs->trans("or").' &nbsp; ';
  298. print $langs->trans("NewHeight").': <input name="sizey" type="number" class="flat maxwidth50"> px &nbsp; <br>';
  299. print '<input type="hidden" name="file" value="'.dol_escape_htmltag(GETPOST('file')).'" />';
  300. print '<input type="hidden" name="action" value="confirm_resize" />';
  301. print '<input type="hidden" name="product" value="'.$id.'" />';
  302. print '<input type="hidden" name="modulepart" value="'.dol_escape_htmltag($modulepart).'" />';
  303. print '<input type="hidden" name="id" value="'.$id.'" />';
  304. print '<br>';
  305. print '<input class="button" id="submitresize" name="sendit" value="'.dol_escape_htmltag($langs->trans("Resize")).'" type="submit" />';
  306. print '&nbsp;';
  307. print '<input type="submit" id="cancelresize" name="cancel" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" />';
  308. print '</fieldset>'."\n";
  309. print '</form>';
  310. print '<br>'."\n";
  311. /*
  312. * Crop image
  313. */
  314. print '<br>'."\n";
  315. if (! empty($conf->use_javascript_ajax))
  316. {
  317. $infoarray=dol_getImageSize($dir."/".GETPOST("file"));
  318. $height=$infoarray['height'];
  319. $width=$infoarray['width'];
  320. $widthforcrop=$width; $refsizeforcrop='orig'; $ratioforcrop=1;
  321. // If image is too large, we use another scale.
  322. if (! empty($_SESSION['dol_screenwidth']) && ($widthforcrop > round($_SESSION['dol_screenwidth']/2)))
  323. {
  324. $widthforcrop=round($_SESSION['dol_screenwidth']/2);
  325. $refsizeforcrop='screenwidth';
  326. $ratioforcrop=1;
  327. }
  328. print '<!-- Form to crop -->'."\n";
  329. print '<fieldset id="redim_file">';
  330. print '<legend>'.$langs->trans("Recenter").'</legend>';
  331. print $langs->trans("DefineNewAreaToPick").'...<br>';
  332. print '<br><div class="center">';
  333. print '<div style="border: 1px solid #888888; width: '.$widthforcrop.'px;">';
  334. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$object->entity.'&file='.$original_file.'" alt="" id="cropbox" width="'.$widthforcrop.'px"/>';
  335. print '</div>';
  336. print '</div><br>';
  337. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">
  338. <div class="jc_coords">
  339. '.$langs->trans("NewSizeAfterCropping").':
  340. <label>X1 <input type="number" class="flat maxwidth50" id="x" name="x" /></label>
  341. <label>Y1 <input type="number" class="flat maxwidth50" id="y" name="y" /></label>
  342. <label>X2 <input type="number" class="flat maxwidth50" id="x2" name="x2" /></label>
  343. <label>Y2 <input type="number" class="flat maxwidth50" id="y2" name="y2" /></label>
  344. <label>W <input type="number" class="flat maxwidth50" id="w" name="w" /></label>
  345. <label>H <input type="number" class="flat maxwidth50" id="h" name="h" /></label>
  346. </div>
  347. <input type="hidden" id="file" name="file" value="'.dol_escape_htmltag($original_file).'" />
  348. <input type="hidden" id="action" name="action" value="confirm_crop" />
  349. <input type="hidden" id="product" name="product" value="'.dol_escape_htmltag($id).'" />
  350. <input type="hidden" id="refsizeforcrop" name="refsizeforcrop" value="'.$refsizeforcrop.'" />
  351. <input type="hidden" id="ratioforcrop" name="ratioforcrop" value="'.$ratioforcrop.'" />
  352. <input type="hidden" name="modulepart" value="'.dol_escape_htmltag($modulepart).'" />
  353. <input type="hidden" name="id" value="'.dol_escape_htmltag($id).'" />
  354. <br>
  355. <input type="submit" id="submitcrop" name="submitcrop" class="button" value="'.dol_escape_htmltag($langs->trans("Recenter")).'" />
  356. &nbsp;
  357. <input type="submit" id="cancelcrop" name="cancel" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" />
  358. </form>'."\n";
  359. print '</fieldset>'."\n";
  360. print '<br>';
  361. }
  362. /* Check that mandatory fields are filled */
  363. print '<script type="text/javascript" language="javascript">
  364. jQuery(document).ready(function() {
  365. $("#submitcrop").click(function(e) {
  366. console.log("We click on submitcrop");
  367. var idClicked = e.target.id;
  368. if (parseInt(jQuery(\'#w\').val())) return true;
  369. alert(\''.dol_escape_js($langs->trans("ErrorFieldRequired", $langs->trans("Dimension"))).'\');
  370. return false;
  371. });
  372. });
  373. </script>';
  374. llxFooter();
  375. $db->close();