From ce10c0ff1dd0b47bd494ae88ab2eaf6ab354b2b3 Mon Sep 17 00:00:00 2001 From: polo Date: Fri, 2 Jun 2023 01:21:06 +0200 Subject: filtre nom des images + reorg code Image --- model/Album.php | 31 +------------------------------ model/Article.php | 2 -- model/Image.php | 37 +++++++++++++++++++++++++------------ 3 files changed, 26 insertions(+), 44 deletions(-) (limited to 'model') diff --git a/model/Album.php b/model/Album.php index d2537f7..801afef 100644 --- a/model/Album.php +++ b/model/Album.php @@ -39,25 +39,10 @@ class Album extends Article // fonctions CRUD // create - public function createVignette($titre, $annee, $pochette, $pochetteMini) + public function createVignetteJSON($titre, $annee, $pochette, $pochetteMini) { //$this->format = 'json'; - if($pochette != '') - { - //echo($pochette); die(); // = nom sans chemin - - // télécharger la pochette - require('model/Image.php'); - $Image = new Image($this->page, $this->folder, false); - $Image->upload(); - $Image->makeThumbnail(201); - - $erreur = $Image->erreur; - if(!empty($erreur)) - {} - } - $albumJSON = json_encode([$titre, $annee, $pochette, $pochetteMini]); //var_dump($albumJSON); die(); @@ -171,20 +156,6 @@ class Album extends Article $pochette = $albumJSON[2]; $pochetteMini = $albumJSON[3]; } - else - { - // télécharger la pochette - require('model/Image.php'); - $Image = new Image($this->page, $this->folder, false); - $Image->upload(); - $Image->makeThumbnail(201); - $erreur = $Image->erreur; - if(!empty($erreur)) - { - // remettre pochette d'avant - //$pochette - } - } $albumJSON = json_encode([$titre, $annee, $pochette, $pochetteMini]); diff --git a/model/Article.php b/model/Article.php index 0e8db61..d94ba01 100644 --- a/model/Article.php +++ b/model/Article.php @@ -198,8 +198,6 @@ class Article fputs($file, $content); fclose($file); chmod($fileName, 0666); - - } // read 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 public $path; public $pathMini; + public $FileName; // après sécurisation + public $width; public $pathInfos; public $erreur; @@ -33,20 +35,31 @@ class Image $this->pathMini = 'data/' . $this->folder . '/images-mini/'; } + public function setFileName($fileName) + { + $this->FileName = $fileName; + } + + public function setThumbnailWidth($width) + { + $this->width = $width; + } public function upload() { // traitement et enregistrement de l'image if (isset($_FILES['upload']) AND $_FILES['upload']['error'] == 0) // 0 signifie ok { - $this->pathInfos = pathinfo($_FILES['upload']['name']); + //$this->pathInfos = pathinfo($_FILES['upload']['name']); + $this->pathInfos = pathinfo($this->FileName); + $extension = $this->pathInfos['extension']; $extautorisées = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff'); // on prend la même liste que celle côté javascript, le SVG est bloqué pour raison de sécurité (javascript à l'intérieur) if (in_array($extension, $extautorisées)) { - move_uploaded_file($_FILES['upload']['tmp_name'], $this->path . $_FILES['upload']['name']); - chmod($this->path . $_FILES['upload']['name'], 0666); + move_uploaded_file($_FILES['upload']['tmp_name'], $this->path . $this->FileName); + chmod($this->path . $this->FileName, 0666); } else { @@ -63,24 +76,24 @@ class Image if($this->ajax && empty($Image->erreur)) { // chemin en JSON attendu par l'éditeur - $this->reponseAjax = '{"url": "data/' . $this->folder . '/images/' . $_FILES['upload']['name'] . '"}'; + $this->reponseAjax = '{"url": "data/' . $this->folder . '/images/' . $this->FileName . '"}'; } } // miniatures des photos - public function makeThumbnail($largeur) + public function makeThumbnail() { global $imageLibrary; if($imageLibrary == 'imagick') { - $Image = new Imagick($this->path . $_FILES['upload']['name']); + $Image = new Imagick($this->path . $this->FileName); $source = $Image->getImageGeometry(); - if($source['width'] > $largeur) + if($source['width'] > $this->width) { // 0 signifie qu'on conserve les proportions - $Image->thumbnailImage($largeur, 0); + $Image->thumbnailImage($this->width, 0); } // écriture dans un fichier @@ -89,18 +102,18 @@ class Image elseif($imageLibrary == 'gd') { // cette fonction fonctionne pour tous les formats - $source = imagecreatefromstring(file_get_contents($this->path . $_FILES['upload']['name'])); + $source = imagecreatefromstring(file_get_contents($this->path . $this->FileName)); $nomMiniImage = $this->pathMini . $this->pathInfos['filename'] . '-mini.jpg'; $forme = imagesy($source) / imagesx($source); - if(imagesx($source) > $largeur) + if(imagesx($source) > $this->width) { // créer un rectangle noir - $destination = imagecreatetruecolor($largeur, $largeur * $forme); + $destination = imagecreatetruecolor($this->width, $this->width * $forme); // sélectionne un rectangle dans l'image source // et le place dans un rectangle dans la nouvelle - imagecopyresampled($destination, $source, 0, 0, 0, 0, $largeur, $largeur * $forme, imagesx($source), imagesy($source)); + imagecopyresampled($destination, $source, 0, 0, 0, 0, $this->width, $this->width * $forme, imagesx($source), imagesy($source)); // envoie l'image dans un fichier imagejpeg($destination, $nomMiniImage); -- cgit v1.2.3