photos_resize.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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@inodbox.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. // Load translation files required by the page
  29. $langs->loadLangs(array("products","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','alpha');
  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 == 'expensereport')
  52. {
  53. $result=restrictedArea($user,'expensereport',$id,'expensereport');
  54. if (! $user->rights->expensereport->lire) accessforbidden();
  55. $accessallowed=1;
  56. }
  57. elseif ($modulepart == 'holiday')
  58. {
  59. $result=restrictedArea($user,'holiday',$id,'holiday');
  60. if (! $user->rights->holiday->read) accessforbidden();
  61. $accessallowed=1;
  62. }
  63. elseif ($modulepart == 'member')
  64. {
  65. $result=restrictedArea($user, 'adherent', $id, '', '', 'fk_soc', 'rowid');
  66. if (! $user->rights->adherent->lire) accessforbidden();
  67. $accessallowed=1;
  68. }
  69. elseif ($modulepart == 'user')
  70. {
  71. $result=restrictedArea($user,'user',$id,'user');
  72. if (! $user->rights->user->user->lire) accessforbidden();
  73. $accessallowed=1;
  74. }
  75. elseif ($modulepart == 'societe')
  76. {
  77. $result=restrictedArea($user,'societe',$id,'societe');
  78. if (! $user->rights->societe->lire) accessforbidden();
  79. $accessallowed=1;
  80. }
  81. elseif ($modulepart == 'tax')
  82. {
  83. $result=restrictedArea($user, 'tax', $id, 'chargesociales','charges');
  84. if (! $user->rights->tax->charges->lire) accessforbidden();
  85. $accessallowed=1;
  86. }
  87. elseif ($modulepart == 'ticket')
  88. {
  89. $result=restrictedArea($user,'ticket',$id,'ticket');
  90. if (! $user->rights->ticket->read) accessforbidden();
  91. $accessallowed=1;
  92. }
  93. // Security:
  94. // Limit access if permissions are wrong
  95. if (! $accessallowed)
  96. {
  97. accessforbidden();
  98. }
  99. // Define dir according to modulepart
  100. if ($modulepart == 'produit' || $modulepart == 'product' || $modulepart == 'service' || $modulepart == 'produit|service')
  101. {
  102. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  103. $object = new Product($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->product->multidir_output[$object->entity]; // By default
  109. if ($object->type == Product::TYPE_PRODUCT) $dir=$conf->product->multidir_output[$object->entity];
  110. if ($object->type == Product::TYPE_SERVICE) $dir=$conf->service->multidir_output[$object->entity];
  111. }
  112. }
  113. elseif ($modulepart == 'project')
  114. {
  115. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  116. $object = new Project($db);
  117. if ($id > 0)
  118. {
  119. $result = $object->fetch($id);
  120. if ($result <= 0) dol_print_error($db,'Failed to load object');
  121. $dir=$conf->projet->dir_output; // By default
  122. }
  123. }
  124. elseif ($modulepart == 'holiday')
  125. {
  126. require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
  127. $object = new Holiday($db);
  128. if ($id > 0)
  129. {
  130. $result = $object->fetch($id);
  131. if ($result <= 0) dol_print_error($db,'Failed to load object');
  132. $dir=$conf->holiday->dir_output; // By default
  133. }
  134. }
  135. elseif ($modulepart == 'member')
  136. {
  137. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  138. $object = new Adherent($db);
  139. if ($id > 0)
  140. {
  141. $result = $object->fetch($id);
  142. if ($result <= 0) dol_print_error($db,'Failed to load object');
  143. $dir=$conf->adherent->dir_output; // By default
  144. }
  145. }
  146. elseif ($modulepart == 'societe')
  147. {
  148. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  149. $object = new Societe($db);
  150. if ($id > 0)
  151. {
  152. $result = $object->fetch($id);
  153. if ($result <= 0) dol_print_error($db,'Failed to load object');
  154. $dir=$conf->societe->dir_output;
  155. }
  156. }
  157. elseif ($modulepart == 'user')
  158. {
  159. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  160. $object = new User($db);
  161. if ($id > 0)
  162. {
  163. $result = $object->fetch($id);
  164. if ($result <= 0) dol_print_error($db,'Failed to load object');
  165. $dir=$conf->user->dir_output; // By default
  166. }
  167. }
  168. elseif ($modulepart == 'expensereport')
  169. {
  170. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  171. $object = new ExpenseReport($db);
  172. if ($id > 0)
  173. {
  174. $result = $object->fetch($id);
  175. if ($result <= 0) dol_print_error($db,'Failed to load object');
  176. $dir=$conf->expensereport->dir_output; // By default
  177. }
  178. }
  179. elseif ($modulepart == 'tax')
  180. {
  181. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  182. $object = new ChargeSociales($db);
  183. if ($id > 0)
  184. {
  185. $result = $object->fetch($id);
  186. if ($result <= 0) dol_print_error($db,'Failed to load object');
  187. $dir=$conf->tax->dir_output; // By default
  188. }
  189. }
  190. elseif ($modulepart == 'ticket')
  191. {
  192. require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
  193. $object = new Ticket($db);
  194. if ($id > 0)
  195. {
  196. $result = $object->fetch($id);
  197. if ($result <= 0) dol_print_error($db,'Failed to load object');
  198. $dir=$conf->ticket->dir_output; // By default
  199. }
  200. }
  201. else {
  202. print 'Action crop for module part '.$modulepart.' is not supported yet.';
  203. }
  204. if (empty($backtourl))
  205. {
  206. if (in_array($modulepart, array('product','produit','service','produit|service'))) $backtourl=DOL_URL_ROOT."/product/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  207. else if (in_array($modulepart, array('expensereport'))) $backtourl=DOL_URL_ROOT."/expensereport/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  208. else if (in_array($modulepart, array('holiday'))) $backtourl=DOL_URL_ROOT."/holiday/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  209. else if (in_array($modulepart, array('member'))) $backtourl=DOL_URL_ROOT."/adherents/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  210. else if (in_array($modulepart, array('project'))) $backtourl=DOL_URL_ROOT."/projet/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  211. else if (in_array($modulepart, array('societe'))) $backtourl=DOL_URL_ROOT."/societe/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  212. else if (in_array($modulepart, array('tax'))) $backtourl=DOL_URL_ROOT."/compta/sociales/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  213. else if (in_array($modulepart, array('ticket'))) $backtourl=DOL_URL_ROOT."/ticket/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  214. else if (in_array($modulepart, array('user'))) $backtourl=DOL_URL_ROOT."/user/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
  215. }
  216. /*
  217. * Actions
  218. */
  219. if ($cancel)
  220. {
  221. if ($backtourl)
  222. {
  223. header("Location: ".$backtourl);
  224. exit;
  225. }
  226. else
  227. {
  228. dol_print_error('', 'Cancel on photo_resize with a not supported value of modulepart='.$modulepart);
  229. exit;
  230. }
  231. }
  232. if ($action == 'confirm_resize' && (isset($_POST["file"]) != "") && (isset($_POST["sizex"]) != "") && (isset($_POST["sizey"]) != ""))
  233. {
  234. $fullpath=$dir."/".$original_file;
  235. $result=dol_imageResizeOrCrop($fullpath,0,$_POST['sizex'],$_POST['sizey']);
  236. if ($result == $fullpath)
  237. {
  238. $object->addThumbs($fullpath);
  239. // Update/create database for file $fullpath
  240. $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $fullpath);
  241. $rel_filename = preg_replace('/^[\\/]/','',$rel_filename);
  242. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  243. $ecmfile=new EcmFiles($db);
  244. $result = $ecmfile->fetch(0, '', $rel_filename);
  245. if ($result > 0) // If found
  246. {
  247. $filename = basename($rel_filename);
  248. $rel_dir = dirname($rel_filename);
  249. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  250. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  251. $ecmfile->label = md5_file(dol_osencode($fullpath));
  252. $result = $ecmfile->update($user);
  253. }
  254. elseif ($result == 0) // If not found
  255. {
  256. $filename = basename($rel_filename);
  257. $rel_dir = dirname($rel_filename);
  258. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  259. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  260. $ecmfile->filepath = $rel_dir;
  261. $ecmfile->filename = $filename;
  262. $ecmfile->label = md5_file(dol_osencode($fullpath)); // $fullpath is a full path to file
  263. $ecmfile->fullpath_orig = $fullpath;
  264. $ecmfile->gen_or_uploaded = 'unknown';
  265. $ecmfile->description = ''; // indexed content
  266. $ecmfile->keyword = ''; // keyword content
  267. $result = $ecmfile->create($user);
  268. if ($result < 0)
  269. {
  270. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  271. }
  272. $result = $ecmfile->create($user);
  273. }
  274. if ($backtourl)
  275. {
  276. header("Location: ".$backtourl);
  277. exit;
  278. }
  279. else
  280. {
  281. dol_print_error('', 'confirm_resize on photo_resize without backtourl defined for modulepart='.$modulepart);
  282. exit;
  283. }
  284. }
  285. else
  286. {
  287. setEventMessages($result, null, 'errors');
  288. $_GET['file']=$_POST["file"];
  289. $action='';
  290. }
  291. }
  292. // Crop d'une image
  293. if ($action == 'confirm_crop')
  294. {
  295. $fullpath=$dir."/".$original_file;
  296. //var_dump($_POST['w'].'x'.$_POST['h'].'-'.$_POST['x'].'x'.$_POST['y']);exit;
  297. $result=dol_imageResizeOrCrop($fullpath,1,$_POST['w'],$_POST['h'],$_POST['x'],$_POST['y']);
  298. if ($result == $fullpath)
  299. {
  300. $object->addThumbs($fullpath);
  301. // Update/create database for file $fullpath
  302. $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $fullpath);
  303. $rel_filename = preg_replace('/^[\\/]/','',$rel_filename);
  304. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  305. $ecmfile=new EcmFiles($db);
  306. $result = $ecmfile->fetch(0, '', $rel_filename);
  307. if ($result > 0) // If found
  308. {
  309. $filename = basename($rel_filename);
  310. $rel_dir = dirname($rel_filename);
  311. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  312. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  313. $ecmfile->label = md5_file(dol_osencode($fullpath));
  314. $result = $ecmfile->update($user);
  315. }
  316. elseif ($result == 0) // If not found
  317. {
  318. $filename = basename($rel_filename);
  319. $rel_dir = dirname($rel_filename);
  320. $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
  321. $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
  322. $ecmfile->filepath = $rel_dir;
  323. $ecmfile->filename = $filename;
  324. $ecmfile->label = md5_file(dol_osencode($fullpath)); // $fullpath is a full path to file
  325. $ecmfile->fullpath_orig = $fullpath;
  326. $ecmfile->gen_or_uploaded = 'unknown';
  327. $ecmfile->description = ''; // indexed content
  328. $ecmfile->keyword = ''; // keyword content
  329. $result = $ecmfile->create($user);
  330. if ($result < 0)
  331. {
  332. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  333. }
  334. $result = $ecmfile->create($user);
  335. }
  336. if ($backtourl)
  337. {
  338. header("Location: ".$backtourl);
  339. exit;
  340. }
  341. else
  342. {
  343. dol_print_error('', 'confirm_crop on photo_resize without backtourl defined for modulepart='.$modulepart);
  344. exit;
  345. }
  346. }
  347. else
  348. {
  349. setEventMessages($result, null, 'errors');
  350. $_GET['file']=$_POST["file"];
  351. $action='';
  352. }
  353. }
  354. /*
  355. * View
  356. */
  357. 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'));
  358. print load_fiche_titre($langs->trans("ImageEditor"));
  359. $infoarray=dol_getImageSize($dir."/".GETPOST("file",'alpha'));
  360. $height=$infoarray['height'];
  361. $width=$infoarray['width'];
  362. print $langs->trans("CurrentInformationOnImage").': ';
  363. print $langs->trans("Width").': <strong>'.$width.'</strong> x '.$langs->trans("Height").': <strong>'.$height.'</strong><br>';
  364. print '<br>'."\n";
  365. /*
  366. * Resize image
  367. */
  368. print '<!-- Form to resize -->'."\n";
  369. print '<form name="redim_file" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
  370. print '<fieldset id="redim_file">';
  371. print '<legend>'.$langs->trans("Resize").'</legend>';
  372. print $langs->trans("ResizeDesc").'<br>';
  373. print $langs->trans("NewLength").': <input name="sizex" type="number" class="flat maxwidth50"> px &nbsp; '.$langs->trans("or").' &nbsp; ';
  374. print $langs->trans("NewHeight").': <input name="sizey" type="number" class="flat maxwidth50"> px &nbsp; <br>';
  375. print '<input type="hidden" name="file" value="'.dol_escape_htmltag(GETPOST('file')).'" />';
  376. print '<input type="hidden" name="action" value="confirm_resize" />';
  377. print '<input type="hidden" name="product" value="'.$id.'" />';
  378. print '<input type="hidden" name="modulepart" value="'.dol_escape_htmltag($modulepart).'" />';
  379. print '<input type="hidden" name="id" value="'.$id.'" />';
  380. print '<br>';
  381. print '<input class="button" id="submitresize" name="sendit" value="'.dol_escape_htmltag($langs->trans("Resize")).'" type="submit" />';
  382. print '&nbsp;';
  383. print '<input type="submit" id="cancelresize" name="cancel" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" />';
  384. print '</fieldset>'."\n";
  385. print '</form>';
  386. print '<br>'."\n";
  387. /*
  388. * Crop image
  389. */
  390. print '<br>'."\n";
  391. if (! empty($conf->use_javascript_ajax))
  392. {
  393. $infoarray=dol_getImageSize($dir."/".GETPOST("file"));
  394. $height=$infoarray['height'];
  395. $width=$infoarray['width'];
  396. $widthforcrop=$width; $refsizeforcrop='orig'; $ratioforcrop=1;
  397. // If image is too large, we use another scale.
  398. if (! empty($_SESSION['dol_screenwidth']) && ($widthforcrop > round($_SESSION['dol_screenwidth']/2)))
  399. {
  400. $ratioforcrop=2;
  401. $widthforcrop=round($_SESSION['dol_screenwidth'] / $ratioforcrop);
  402. $refsizeforcrop='screenwidth';
  403. }
  404. print '<!-- Form to crop -->'."\n";
  405. print '<fieldset id="redim_file">';
  406. print '<legend>'.$langs->trans("Recenter").'</legend>';
  407. print $langs->trans("DefineNewAreaToPick").'...<br>';
  408. print '<br><div class="center">';
  409. print '<div style="border: 1px solid #888888; width: '.$widthforcrop.'px;">';
  410. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$object->entity.'&file='.urlencode($original_file).'" alt="" id="cropbox" width="'.$widthforcrop.'px"/>';
  411. print '</div>';
  412. print '</div><br>';
  413. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">
  414. <div class="jc_coords">
  415. '.$langs->trans("NewSizeAfterCropping").':
  416. <label>X1 <input type="number" class="flat maxwidth50" id="x" name="x" /></label>
  417. <label>Y1 <input type="number" class="flat maxwidth50" id="y" name="y" /></label>
  418. <label>X2 <input type="number" class="flat maxwidth50" id="x2" name="x2" /></label>
  419. <label>Y2 <input type="number" class="flat maxwidth50" id="y2" name="y2" /></label>
  420. <label>W <input type="number" class="flat maxwidth50" id="w" name="w" /></label>
  421. <label>H <input type="number" class="flat maxwidth50" id="h" name="h" /></label>
  422. </div>
  423. <input type="hidden" id="file" name="file" value="'.dol_escape_htmltag($original_file).'" />
  424. <input type="hidden" id="action" name="action" value="confirm_crop" />
  425. <input type="hidden" id="product" name="product" value="'.dol_escape_htmltag($id).'" />
  426. <input type="hidden" id="refsizeforcrop" name="refsizeforcrop" value="'.$refsizeforcrop.'" />
  427. <input type="hidden" id="ratioforcrop" name="ratioforcrop" value="'.$ratioforcrop.'" /><!-- field used by core/lib/lib_photoresize.js -->
  428. <input type="hidden" name="modulepart" value="'.dol_escape_htmltag($modulepart).'" />
  429. <input type="hidden" name="id" value="'.dol_escape_htmltag($id).'" />
  430. <br>
  431. <input type="submit" id="submitcrop" name="submitcrop" class="button" value="'.dol_escape_htmltag($langs->trans("Recenter")).'" />
  432. &nbsp;
  433. <input type="submit" id="cancelcrop" name="cancel" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" />
  434. </form>'."\n";
  435. print '</fieldset>'."\n";
  436. print '<br>';
  437. }
  438. /* Check that mandatory fields are filled */
  439. print '<script type="text/javascript" language="javascript">
  440. jQuery(document).ready(function() {
  441. $("#submitcrop").click(function(e) {
  442. console.log("We click on submitcrop");
  443. var idClicked = e.target.id;
  444. if (parseInt(jQuery(\'#w\').val())) return true;
  445. alert(\''.dol_escape_js($langs->trans("ErrorFieldRequired", $langs->trans("Dimension"))).'\');
  446. return false;
  447. });
  448. });
  449. </script>';
  450. llxFooter();
  451. $db->close();