dolibarr  9.0.0
photos_resize.php
Go to the documentation of this file.
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 
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array("products","other"));
33 
34 $id=GETPOST('id','int');
35 $action=GETPOST('action','alpha');
36 $modulepart=GETPOST('modulepart','alpha')?GETPOST('modulepart','alpha'):'produit|service';
37 $original_file = GETPOST("file");
38 $backtourl=GETPOST('backtourl');
39 $cancel=GETPOST('cancel','alpha');
40 
41 // Security check
42 if (empty($modulepart)) accessforbidden('Bad value for modulepart');
43 $accessallowed=0;
44 if ($modulepart == 'produit' || $modulepart == 'product' || $modulepart == 'service' || $modulepart == 'produit|service')
45 {
46  $result=restrictedArea($user,'produit|service',$id,'product&product');
47  if ($modulepart=='produit|service' && (! $user->rights->produit->lire && ! $user->rights->service->lire)) accessforbidden();
48  $accessallowed=1;
49 }
50 elseif ($modulepart == 'project')
51 {
52  $result=restrictedArea($user,'projet',$id);
53  if (! $user->rights->projet->lire) accessforbidden();
54  $accessallowed=1;
55 }
56 elseif ($modulepart == 'expensereport')
57 {
58  $result=restrictedArea($user,'expensereport',$id,'expensereport');
59  if (! $user->rights->expensereport->lire) accessforbidden();
60  $accessallowed=1;
61 }
62 elseif ($modulepart == 'holiday')
63 {
64  $result=restrictedArea($user,'holiday',$id,'holiday');
65  if (! $user->rights->holiday->read) accessforbidden();
66  $accessallowed=1;
67 }
68 elseif ($modulepart == 'member')
69 {
70  $result=restrictedArea($user, 'adherent', $id, '', '', 'fk_soc', 'rowid');
71  if (! $user->rights->adherent->lire) accessforbidden();
72  $accessallowed=1;
73 }
74 elseif ($modulepart == 'user')
75 {
76  $result=restrictedArea($user,'user',$id,'user');
77  if (! $user->rights->user->user->lire) accessforbidden();
78  $accessallowed=1;
79 }
80 elseif ($modulepart == 'societe')
81 {
82  $result=restrictedArea($user,'societe',$id,'societe');
83  if (! $user->rights->societe->lire) accessforbidden();
84  $accessallowed=1;
85 }
86 elseif ($modulepart == 'tax')
87 {
88  $result=restrictedArea($user, 'tax', $id, 'chargesociales','charges');
89  if (! $user->rights->tax->charges->lire) accessforbidden();
90  $accessallowed=1;
91 }
92 elseif ($modulepart == 'ticket')
93 {
94  $result=restrictedArea($user,'ticket',$id,'ticket');
95  if (! $user->rights->ticket->read) accessforbidden();
96  $accessallowed=1;
97 }
98 
99 // Security:
100 // Limit access if permissions are wrong
101 if (! $accessallowed)
102 {
103  accessforbidden();
104 }
105 
106 // Define dir according to modulepart
107 if ($modulepart == 'produit' || $modulepart == 'product' || $modulepart == 'service' || $modulepart == 'produit|service')
108 {
109  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
110  $object = new Product($db);
111  if ($id > 0)
112  {
113  $result = $object->fetch($id);
114  if ($result <= 0) dol_print_error($db,'Failed to load object');
115  $dir=$conf->product->multidir_output[$object->entity]; // By default
116  if ($object->type == Product::TYPE_PRODUCT) $dir=$conf->product->multidir_output[$object->entity];
117  if ($object->type == Product::TYPE_SERVICE) $dir=$conf->service->multidir_output[$object->entity];
118  }
119 }
120 elseif ($modulepart == 'project')
121 {
122  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
123  $object = new Project($db);
124  if ($id > 0)
125  {
126  $result = $object->fetch($id);
127  if ($result <= 0) dol_print_error($db,'Failed to load object');
128  $dir=$conf->projet->dir_output; // By default
129  }
130 }
131 elseif ($modulepart == 'holiday')
132 {
133  require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
134  $object = new Holiday($db);
135  if ($id > 0)
136  {
137  $result = $object->fetch($id);
138  if ($result <= 0) dol_print_error($db,'Failed to load object');
139  $dir=$conf->holiday->dir_output; // By default
140  }
141 }
142 elseif ($modulepart == 'member')
143 {
144  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
145  $object = new Adherent($db);
146  if ($id > 0)
147  {
148  $result = $object->fetch($id);
149  if ($result <= 0) dol_print_error($db,'Failed to load object');
150  $dir=$conf->adherent->dir_output; // By default
151  }
152 }
153 elseif ($modulepart == 'societe')
154 {
155  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
156  $object = new Societe($db);
157  if ($id > 0)
158  {
159  $result = $object->fetch($id);
160  if ($result <= 0) dol_print_error($db,'Failed to load object');
161  $dir=$conf->societe->dir_output;
162  }
163 }
164 elseif ($modulepart == 'user')
165 {
166  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
167  $object = new User($db);
168  if ($id > 0)
169  {
170  $result = $object->fetch($id);
171  if ($result <= 0) dol_print_error($db,'Failed to load object');
172  $dir=$conf->user->dir_output; // By default
173  }
174 }
175 elseif ($modulepart == 'expensereport')
176 {
177  require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
178  $object = new ExpenseReport($db);
179  if ($id > 0)
180  {
181  $result = $object->fetch($id);
182  if ($result <= 0) dol_print_error($db,'Failed to load object');
183  $dir=$conf->expensereport->dir_output; // By default
184  }
185 }
186 elseif ($modulepart == 'tax')
187 {
188  require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
189  $object = new ChargeSociales($db);
190  if ($id > 0)
191  {
192  $result = $object->fetch($id);
193  if ($result <= 0) dol_print_error($db,'Failed to load object');
194  $dir=$conf->tax->dir_output; // By default
195  }
196 }
197 elseif ($modulepart == 'ticket')
198 {
199  require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
200  $object = new Ticket($db);
201  if ($id > 0)
202  {
203  $result = $object->fetch($id);
204  if ($result <= 0) dol_print_error($db,'Failed to load object');
205  $dir=$conf->ticket->dir_output; // By default
206  }
207 }
208 else {
209  print 'Action crop for module part '.$modulepart.' is not supported yet.';
210 }
211 
212 if (empty($backtourl))
213 {
214  if (in_array($modulepart, array('product','produit','service','produit|service'))) $backtourl=DOL_URL_ROOT."/product/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
215  else if (in_array($modulepart, array('expensereport'))) $backtourl=DOL_URL_ROOT."/expensereport/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
216  else if (in_array($modulepart, array('holiday'))) $backtourl=DOL_URL_ROOT."/holiday/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
217  else if (in_array($modulepart, array('member'))) $backtourl=DOL_URL_ROOT."/adherents/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
218  else if (in_array($modulepart, array('project'))) $backtourl=DOL_URL_ROOT."/projet/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
219  else if (in_array($modulepart, array('societe'))) $backtourl=DOL_URL_ROOT."/societe/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
220  else if (in_array($modulepart, array('tax'))) $backtourl=DOL_URL_ROOT."/compta/sociales/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
221  else if (in_array($modulepart, array('ticket'))) $backtourl=DOL_URL_ROOT."/ticket/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
222  else if (in_array($modulepart, array('user'))) $backtourl=DOL_URL_ROOT."/user/document.php?id=".$id.'&file='.urldecode($_POST["file"]);
223 }
224 
225 
226 /*
227  * Actions
228  */
229 
230 if ($cancel)
231 {
232  if ($backtourl)
233  {
234  header("Location: ".$backtourl);
235  exit;
236  }
237  else
238  {
239  dol_print_error('', 'Cancel on photo_resize with a not supported value of modulepart='.$modulepart);
240  exit;
241  }
242 }
243 
244 if ($action == 'confirm_resize' && (isset($_POST["file"]) != "") && (isset($_POST["sizex"]) != "") && (isset($_POST["sizey"]) != ""))
245 {
246  $fullpath=$dir."/".$original_file;
247 
248  $result=dol_imageResizeOrCrop($fullpath,0,$_POST['sizex'],$_POST['sizey']);
249 
250  if ($result == $fullpath)
251  {
252  $object->addThumbs($fullpath);
253 
254  // Update/create database for file $fullpath
255  $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $fullpath);
256  $rel_filename = preg_replace('/^[\\/]/','',$rel_filename);
257 
258  include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
259  $ecmfile=new EcmFiles($db);
260  $result = $ecmfile->fetch(0, '', $rel_filename);
261  if ($result > 0) // If found
262  {
263  $filename = basename($rel_filename);
264  $rel_dir = dirname($rel_filename);
265  $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
266  $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
267 
268  $ecmfile->label = md5_file(dol_osencode($fullpath));
269  $result = $ecmfile->update($user);
270  }
271  elseif ($result == 0) // If not found
272  {
273  $filename = basename($rel_filename);
274  $rel_dir = dirname($rel_filename);
275  $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
276  $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
277 
278  $ecmfile->filepath = $rel_dir;
279  $ecmfile->filename = $filename;
280  $ecmfile->label = md5_file(dol_osencode($fullpath)); // $fullpath is a full path to file
281  $ecmfile->fullpath_orig = $fullpath;
282  $ecmfile->gen_or_uploaded = 'unknown';
283  $ecmfile->description = ''; // indexed content
284  $ecmfile->keyword = ''; // keyword content
285  $result = $ecmfile->create($user);
286  if ($result < 0)
287  {
288  setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
289  }
290  $result = $ecmfile->create($user);
291  }
292 
293  if ($backtourl)
294  {
295  header("Location: ".$backtourl);
296  exit;
297  }
298  else
299  {
300  dol_print_error('', 'confirm_resize on photo_resize without backtourl defined for modulepart='.$modulepart);
301  exit;
302  }
303  }
304  else
305  {
306  setEventMessages($result, null, 'errors');
307  $_GET['file']=$_POST["file"];
308  $action='';
309  }
310 }
311 
312 // Crop d'une image
313 if ($action == 'confirm_crop')
314 {
315  $fullpath=$dir."/".$original_file;
316 
317  //var_dump($_POST['w'].'x'.$_POST['h'].'-'.$_POST['x'].'x'.$_POST['y']);exit;
318  $result=dol_imageResizeOrCrop($fullpath,1,$_POST['w'],$_POST['h'],$_POST['x'],$_POST['y']);
319 
320  if ($result == $fullpath)
321  {
322  $object->addThumbs($fullpath);
323 
324  // Update/create database for file $fullpath
325  $rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $fullpath);
326  $rel_filename = preg_replace('/^[\\/]/','',$rel_filename);
327 
328  include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
329  $ecmfile=new EcmFiles($db);
330  $result = $ecmfile->fetch(0, '', $rel_filename);
331  if ($result > 0) // If found
332  {
333  $filename = basename($rel_filename);
334  $rel_dir = dirname($rel_filename);
335  $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
336  $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
337 
338  $ecmfile->label = md5_file(dol_osencode($fullpath));
339  $result = $ecmfile->update($user);
340  }
341  elseif ($result == 0) // If not found
342  {
343  $filename = basename($rel_filename);
344  $rel_dir = dirname($rel_filename);
345  $rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
346  $rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
347 
348  $ecmfile->filepath = $rel_dir;
349  $ecmfile->filename = $filename;
350  $ecmfile->label = md5_file(dol_osencode($fullpath)); // $fullpath is a full path to file
351  $ecmfile->fullpath_orig = $fullpath;
352  $ecmfile->gen_or_uploaded = 'unknown';
353  $ecmfile->description = ''; // indexed content
354  $ecmfile->keyword = ''; // keyword content
355  $result = $ecmfile->create($user);
356  if ($result < 0)
357  {
358  setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
359  }
360  $result = $ecmfile->create($user);
361  }
362 
363  if ($backtourl)
364  {
365  header("Location: ".$backtourl);
366  exit;
367  }
368  else
369  {
370  dol_print_error('', 'confirm_crop on photo_resize without backtourl defined for modulepart='.$modulepart);
371  exit;
372  }
373  }
374  else
375  {
376  setEventMessages($result, null, 'errors');
377  $_GET['file']=$_POST["file"];
378  $action='';
379  }
380 }
381 
382 
383 /*
384  * View
385  */
386 
387 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'));
388 
389 
390 print load_fiche_titre($langs->trans("ImageEditor"));
391 
392 $infoarray=dol_getImageSize($dir."/".GETPOST("file",'alpha'));
393 $height=$infoarray['height'];
394 $width=$infoarray['width'];
395 print $langs->trans("CurrentInformationOnImage").': ';
396 print $langs->trans("Width").': <strong>'.$width.'</strong> x '.$langs->trans("Height").': <strong>'.$height.'</strong><br>';
397 
398 print '<br>'."\n";
399 
400 
401 /*
402  * Resize image
403  */
404 
405 print '<!-- Form to resize -->'."\n";
406 print '<form name="redim_file" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
407 
408 print '<fieldset id="redim_file">';
409 print '<legend>'.$langs->trans("Resize").'</legend>';
410 print $langs->trans("ResizeDesc").'<br>';
411 print $langs->trans("NewLength").': <input name="sizex" type="number" class="flat maxwidth50"> px &nbsp; '.$langs->trans("or").' &nbsp; ';
412 print $langs->trans("NewHeight").': <input name="sizey" type="number" class="flat maxwidth50"> px &nbsp; <br>';
413 
414 print '<input type="hidden" name="file" value="'.dol_escape_htmltag(GETPOST('file')).'" />';
415 print '<input type="hidden" name="action" value="confirm_resize" />';
416 print '<input type="hidden" name="product" value="'.$id.'" />';
417 print '<input type="hidden" name="modulepart" value="'.dol_escape_htmltag($modulepart).'" />';
418 print '<input type="hidden" name="id" value="'.$id.'" />';
419 print '<br>';
420 print '<input class="button" id="submitresize" name="sendit" value="'.dol_escape_htmltag($langs->trans("Resize")).'" type="submit" />';
421 print '&nbsp;';
422 print '<input type="submit" id="cancelresize" name="cancel" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" />';
423 print '</fieldset>'."\n";
424 print '</form>';
425 print '<br>'."\n";
426 
427 
428 /*
429  * Crop image
430  */
431 
432 print '<br>'."\n";
433 
434 if (! empty($conf->use_javascript_ajax))
435 {
436  $infoarray=dol_getImageSize($dir."/".GETPOST("file"));
437  $height=$infoarray['height'];
438  $width=$infoarray['width'];
439  $widthforcrop=$width; $refsizeforcrop='orig'; $ratioforcrop=1;
440  // If image is too large, we use another scale.
441  if (! empty($_SESSION['dol_screenwidth']) && ($widthforcrop > round($_SESSION['dol_screenwidth']/2)))
442  {
443  $ratioforcrop=2;
444  $widthforcrop=round($_SESSION['dol_screenwidth'] / $ratioforcrop);
445  $refsizeforcrop='screenwidth';
446  }
447 
448  print '<!-- Form to crop -->'."\n";
449  print '<fieldset id="redim_file">';
450  print '<legend>'.$langs->trans("Recenter").'</legend>';
451  print $langs->trans("DefineNewAreaToPick").'...<br>';
452  print '<br><div class="center">';
453  print '<div style="border: 1px solid #888888; width: '.$widthforcrop.'px;">';
454  print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$object->entity.'&file='.urlencode($original_file).'" alt="" id="cropbox" width="'.$widthforcrop.'px"/>';
455  print '</div>';
456  print '</div><br>';
457  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">
458  <div class="jc_coords">
459  '.$langs->trans("NewSizeAfterCropping").':
460  <label>X1 <input type="number" class="flat maxwidth50" id="x" name="x" /></label>
461  <label>Y1 <input type="number" class="flat maxwidth50" id="y" name="y" /></label>
462  <label>X2 <input type="number" class="flat maxwidth50" id="x2" name="x2" /></label>
463  <label>Y2 <input type="number" class="flat maxwidth50" id="y2" name="y2" /></label>
464  <label>W <input type="number" class="flat maxwidth50" id="w" name="w" /></label>
465  <label>H <input type="number" class="flat maxwidth50" id="h" name="h" /></label>
466  </div>
467 
468  <input type="hidden" id="file" name="file" value="'.dol_escape_htmltag($original_file).'" />
469  <input type="hidden" id="action" name="action" value="confirm_crop" />
470  <input type="hidden" id="product" name="product" value="'.dol_escape_htmltag($id).'" />
471  <input type="hidden" id="refsizeforcrop" name="refsizeforcrop" value="'.$refsizeforcrop.'" />
472  <input type="hidden" id="ratioforcrop" name="ratioforcrop" value="'.$ratioforcrop.'" /><!-- field used by core/lib/lib_photoresize.js -->
473  <input type="hidden" name="modulepart" value="'.dol_escape_htmltag($modulepart).'" />
474  <input type="hidden" name="id" value="'.dol_escape_htmltag($id).'" />
475  <br>
476  <input type="submit" id="submitcrop" name="submitcrop" class="button" value="'.dol_escape_htmltag($langs->trans("Recenter")).'" />
477  &nbsp;
478  <input type="submit" id="cancelcrop" name="cancel" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" />
479  </form>'."\n";
480  print '</fieldset>'."\n";
481  print '<br>';
482 }
483 
484 /* Check that mandatory fields are filled */
485 print '<script type="text/javascript" language="javascript">
486 jQuery(document).ready(function() {
487  $("#submitcrop").click(function(e) {
488  console.log("We click on submitcrop");
489  var idClicked = e.target.id;
490  if (parseInt(jQuery(\'#w\').val())) return true;
491  alert(\''.dol_escape_js($langs->trans("ErrorFieldRequired", $langs->trans("Dimension"))).'\');
492  return false;
493  });
494 });
495 </script>';
496 
497 llxFooter();
498 $db->close();
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Class of the module paid holiday.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_getImageSize($file, $url=false)
Return size of image file on disk (Supported extensions are gif, jpg, png and bmp) ...
Definition: images.lib.php:75
Class to manage products or services.
Class to manage Dolibarr users.
Definition: user.class.php:41
const TYPE_SERVICE
Service.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
const TYPE_PRODUCT
Regular product.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage projects.
Class to manage members of a foundation.
llxHeader()
Empty header.
Definition: wrapper.php:44
Classe permettant la generation du formulaire d&#39;un nouveau ticket.
Class to manage Trips and Expenses.
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
Classe permettant la gestion des paiements des charges La tva collectee n&#39;est calculee que sur les fa...
Class to manage ECM files.
dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $src_y=0)
Resize or crop an image file (Supported extensions are gif, jpg, png and bmp)
Definition: images.lib.php:109