1<?php
2/* Copyright (C) 2018	Andreu Bisquerra	<jove@bisquerra.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18//if (! defined('NOREQUIREUSER'))	define('NOREQUIREUSER','1');	// Not disabled cause need to load personalized language
19//if (! defined('NOREQUIREDB'))		define('NOREQUIREDB','1');		// Not disabled cause need to load personalized language
20if (!defined('NOREQUIRESOC'))		define('NOREQUIRESOC', '1');
21//if (! defined('NOREQUIRETRAN'))		define('NOREQUIRETRAN','1');
22if (!defined('NOCSRFCHECK'))		define('NOCSRFCHECK', '1');
23if (!defined('NOTOKENRENEWAL'))	define('NOTOKENRENEWAL', '1');
24if (!defined('NOREQUIREMENU'))		define('NOREQUIREMENU', '1');
25if (!defined('NOREQUIREHTML'))		define('NOREQUIREHTML', '1');
26if (!defined('NOREQUIREAJAX'))		define('NOREQUIREAJAX', '1');
27
28if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) require '../../main.inc.php'; // Load $user and permissions
29
30$id = GETPOST('id', 'int');
31$w = GETPOST('w', 'int');
32$h = GETPOST('h', 'int');
33$query = GETPOST('query', 'alpha');
34
35
36
37/*
38 * View
39 */
40
41if ($query == "cat")
42{
43	require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
44	require_once DOL_DOCUMENT_ROOT.'/core/lib/categories.lib.php';
45
46	$object = new Categorie($db);
47	$result = $object->fetch($id);
48
49	$upload_dir = $conf->categorie->multidir_output[$object->entity];
50	$pdir = get_exdir($object->id, 2, 0, 0, $object, 'category').$object->id."/photos/";
51	$dir = $upload_dir.'/'.$pdir;
52
53	foreach ($object->liste_photos($dir) as $key => $obj)
54	{
55		if ($obj['photo_vignette'])
56		{
57			$filename = $obj['photo_vignette'];
58		} else {
59			$filename = $obj['photo'];
60		}
61		$file = DOL_URL_ROOT.'/viewimage.php?cache=1&publictakepos=1&modulepart=category&entity='.$object->entity.'&file='.urlencode($pdir.$filename);
62		header('Location: '.$file);
63		exit;
64	}
65	header('Location: ../../public/theme/common/nophoto.png');
66} elseif ($query == "pro")
67{
68	require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
69
70	$objProd = new Product($db);
71	$objProd->fetch($id);
72	$image = $objProd->show_photos('product', $conf->product->multidir_output[$objProd->entity], 'small', 1);
73
74	preg_match('@src="([^"]+)"@', $image, $match);
75	$file = array_pop($match);
76	if ($file == "") {
77		header('Location: ../../public/theme/common/nophoto.png');
78	} else {
79		if (!defined('INCLUDE_PHONEPAGE_FROM_PUBLIC_PAGE')) {
80			header('Location: '.$file.'&cache=1');
81		} else {
82			header('Location: '.$file.'&cache=1&publictakepos=1&modulepart=product');
83		}
84	}
85} else {
86	// TODO We don't need this. Size of image must be defined on HTML page, image must NOT be resize when downloaded.
87
88	// The file
89	$filename = $query.".jpg";
90
91	// Dimensions
92	list($width, $height) = getimagesize($filename);
93	$new_width = $w;
94	$new_height = $h;
95
96	// Resample
97	$image_p = imagecreatetruecolor($new_width, $new_height);
98	$image = imagecreatefromjpeg($filename);
99	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
100
101	// Output
102	imagejpeg($image_p, null, 100);
103}
104