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