summaryrefslogtreecommitdiff
path: root/model/Image.php
diff options
context:
space:
mode:
Diffstat (limited to 'model/Image.php')
-rw-r--r--model/Image.php37
1 files changed, 25 insertions, 12 deletions
diff --git a/model/Image.php b/model/Image.php
index d71b96a..dbdad62 100644
--- a/model/Image.php
+++ b/model/Image.php
@@ -11,6 +11,8 @@ class Image
11 11
12 public $path; 12 public $path;
13 public $pathMini; 13 public $pathMini;
14 public $FileName; // après sécurisation
15 public $width;
14 public $pathInfos; 16 public $pathInfos;
15 public $erreur; 17 public $erreur;
16 18
@@ -33,20 +35,31 @@ class Image
33 $this->pathMini = 'data/' . $this->folder . '/images-mini/'; 35 $this->pathMini = 'data/' . $this->folder . '/images-mini/';
34 } 36 }
35 37
38 public function setFileName($fileName)
39 {
40 $this->FileName = $fileName;
41 }
42
43 public function setThumbnailWidth($width)
44 {
45 $this->width = $width;
46 }
36 47
37 public function upload() 48 public function upload()
38 { 49 {
39 // traitement et enregistrement de l'image 50 // traitement et enregistrement de l'image
40 if (isset($_FILES['upload']) AND $_FILES['upload']['error'] == 0) // 0 signifie ok 51 if (isset($_FILES['upload']) AND $_FILES['upload']['error'] == 0) // 0 signifie ok
41 { 52 {
42 $this->pathInfos = pathinfo($_FILES['upload']['name']); 53 //$this->pathInfos = pathinfo($_FILES['upload']['name']);
54 $this->pathInfos = pathinfo($this->FileName);
55
43 $extension = $this->pathInfos['extension']; 56 $extension = $this->pathInfos['extension'];
44 $extautorisées = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff'); 57 $extautorisées = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff');
45 // on prend la même liste que celle côté javascript, le SVG est bloqué pour raison de sécurité (javascript à l'intérieur) 58 // on prend la même liste que celle côté javascript, le SVG est bloqué pour raison de sécurité (javascript à l'intérieur)
46 if (in_array($extension, $extautorisées)) 59 if (in_array($extension, $extautorisées))
47 { 60 {
48 move_uploaded_file($_FILES['upload']['tmp_name'], $this->path . $_FILES['upload']['name']); 61 move_uploaded_file($_FILES['upload']['tmp_name'], $this->path . $this->FileName);
49 chmod($this->path . $_FILES['upload']['name'], 0666); 62 chmod($this->path . $this->FileName, 0666);
50 } 63 }
51 else 64 else
52 { 65 {
@@ -63,24 +76,24 @@ class Image
63 if($this->ajax && empty($Image->erreur)) 76 if($this->ajax && empty($Image->erreur))
64 { 77 {
65 // chemin en JSON attendu par l'éditeur 78 // chemin en JSON attendu par l'éditeur
66 $this->reponseAjax = '{"url": "data/' . $this->folder . '/images/' . $_FILES['upload']['name'] . '"}'; 79 $this->reponseAjax = '{"url": "data/' . $this->folder . '/images/' . $this->FileName . '"}';
67 } 80 }
68 } 81 }
69 82
70 // miniatures des photos 83 // miniatures des photos
71 public function makeThumbnail($largeur) 84 public function makeThumbnail()
72 { 85 {
73 global $imageLibrary; 86 global $imageLibrary;
74 87
75 if($imageLibrary == 'imagick') 88 if($imageLibrary == 'imagick')
76 { 89 {
77 $Image = new Imagick($this->path . $_FILES['upload']['name']); 90 $Image = new Imagick($this->path . $this->FileName);
78 91
79 $source = $Image->getImageGeometry(); 92 $source = $Image->getImageGeometry();
80 if($source['width'] > $largeur) 93 if($source['width'] > $this->width)
81 { 94 {
82 // 0 signifie qu'on conserve les proportions 95 // 0 signifie qu'on conserve les proportions
83 $Image->thumbnailImage($largeur, 0); 96 $Image->thumbnailImage($this->width, 0);
84 } 97 }
85 98
86 // écriture dans un fichier 99 // écriture dans un fichier
@@ -89,18 +102,18 @@ class Image
89 elseif($imageLibrary == 'gd') 102 elseif($imageLibrary == 'gd')
90 { 103 {
91 // cette fonction fonctionne pour tous les formats 104 // cette fonction fonctionne pour tous les formats
92 $source = imagecreatefromstring(file_get_contents($this->path . $_FILES['upload']['name'])); 105 $source = imagecreatefromstring(file_get_contents($this->path . $this->FileName));
93 $nomMiniImage = $this->pathMini . $this->pathInfos['filename'] . '-mini.jpg'; 106 $nomMiniImage = $this->pathMini . $this->pathInfos['filename'] . '-mini.jpg';
94 $forme = imagesy($source) / imagesx($source); 107 $forme = imagesy($source) / imagesx($source);
95 108
96 if(imagesx($source) > $largeur) 109 if(imagesx($source) > $this->width)
97 { 110 {
98 // créer un rectangle noir 111 // créer un rectangle noir
99 $destination = imagecreatetruecolor($largeur, $largeur * $forme); 112 $destination = imagecreatetruecolor($this->width, $this->width * $forme);
100 113
101 // sélectionne un rectangle dans l'image source 114 // sélectionne un rectangle dans l'image source
102 // et le place dans un rectangle dans la nouvelle 115 // et le place dans un rectangle dans la nouvelle
103 imagecopyresampled($destination, $source, 0, 0, 0, 0, $largeur, $largeur * $forme, imagesx($source), imagesy($source)); 116 imagecopyresampled($destination, $source, 0, 0, 0, 0, $this->width, $this->width * $forme, imagesx($source), imagesy($source));
104 117
105 // envoie l'image dans un fichier 118 // envoie l'image dans un fichier
106 imagejpeg($destination, $nomMiniImage); 119 imagejpeg($destination, $nomMiniImage);