diff options
Diffstat (limited to 'model/Image.php')
-rw-r--r-- | model/Image.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/model/Image.php b/model/Image.php new file mode 100644 index 0000000..0070b70 --- /dev/null +++ b/model/Image.php | |||
@@ -0,0 +1,59 @@ | |||
1 | <?php | ||
2 | // model/Image.php | ||
3 | |||
4 | class Image | ||
5 | { | ||
6 | private const MAX_WEIGHT = 2000000; // taille max des images (par défaut 2Mo dans php.ini) | ||
7 | private $page; | ||
8 | private $ajax = false; | ||
9 | private $erreur = ''; | ||
10 | |||
11 | public function __construct($ajax) | ||
12 | { | ||
13 | // get envoyé avec le javascript | ||
14 | $this->page = $_GET['page']; | ||
15 | $this->ajax = $ajax; | ||
16 | } | ||
17 | |||
18 | // GET | ||
19 | public function getError() | ||
20 | { | ||
21 | return($this->erreur); | ||
22 | } | ||
23 | |||
24 | public function upload() | ||
25 | { | ||
26 | |||
27 | // traitement et enregistrement de l'image | ||
28 | if (isset($_FILES['upload']) AND $_FILES['upload']['error'] == 0) // 0 signifie ok | ||
29 | { | ||
30 | if ($_FILES['upload']['size'] <= self::MAX_WEIGHT) | ||
31 | { | ||
32 | $infos = pathinfo($_FILES['upload']['name']); | ||
33 | $extension = $infos['extension']; | ||
34 | $extautorisées = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff'); | ||
35 | // on prend la même liste que celle côté javascript, le SVG est bloqué pour raison de sécurité (javascript à l'intérieur) | ||
36 | if (in_array($extension, $extautorisées)) | ||
37 | { | ||
38 | move_uploaded_file($_FILES['upload']['tmp_name'], 'data/' . $this->page . '/images/' . $_FILES['upload']['name']); | ||
39 | chmod('data/' . $this->page . '/images/' . $_FILES['upload']['name'], 0666); | ||
40 | } | ||
41 | else{$this->erreur = 'mauvais format, veuillez utiliser une image comportant un de ces formats: jpg ou jpeg, png, gif, bmp, webp, tiff<br />le format svg n\'est pas supporté';} | ||
42 | } | ||
43 | else{$this->erreur = 'fichier trop lourd';} | ||
44 | } | ||
45 | else | ||
46 | { | ||
47 | $this->erreur = $_FILES['upload']['error']; | ||
48 | } | ||
49 | |||
50 | // retour des rêquetes AJAX | ||
51 | if($this->ajax) | ||
52 | { | ||
53 | // nouveau chemin à renvoyer en format json | ||
54 | $chemin = '{"url": "data/' . $this->page . '/images/' . $_FILES['upload']['name'] . '"}'; | ||
55 | //echo json_encode($chemin); | ||
56 | echo $chemin; | ||
57 | } | ||
58 | } | ||
59 | } \ No newline at end of file | ||