| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 	namespace core\images; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	use core\App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	class Images { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		//initialisée pour le bon fonctionnement de la class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 		private $autorized_extention = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "JPEG", "GIF"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 		private $erreur; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		//var init dans constructeur | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		private $poid_max; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		private $width_max; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		private $height_max; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		private $dossier_image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		private $old_image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		private $chemin_image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		private $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		private $nom_image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		 * Recoit un tableau contenant les parametres pour la/les images qui vont être upload | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		 * index du tableau | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		 * - poid_max de l'img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		 * - width_max de l'img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		 * - height_max de l'img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		 * - dossier_image dans lequel se trouve l'ancienne et ou se trovera la nouvelle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		 * @param $parameter = array() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		public function __construct($parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			$this->poid_max = $parameter[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			$this->width_max = $parameter[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			$this->height_max = $parameter[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			$this->dossier_image = $parameter[3]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		//-------------------------- GETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		public function getOldImage() { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 			return $this->old_image; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		public function getCheminImage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			return $this->chemin_image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		public function getErreur() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			return $this->erreur; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		public function getNomImage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			return $this->nom_image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		private function getImage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			if ($this->chemin_image != "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 				$explode = explode("/", $this->chemin_image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 				$this->image = end($explode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 				return $this->image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 				$this->erreur = "Impossible de trouver votre image, vuellez réessayer dans un instant"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 				return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		//-------------------------- FIN GETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		//-------------------------- SETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		 * fonction qui permet d'upload une image sur le serveur | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		 * @param $old_image_req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		 * @param int $autorize_empty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		 * @param int $delete_old_img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		 * @return null|boolean -> renvoi false si err sinon renvoi le chemin vers l'img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		public function setEnvoyerImage($name, $old_image_req = null, $autorize_empty = 1, $delete_old_img = 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			$dbc = App::getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			$this->old_image = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			$this->chemin_image = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			$this->nom_image = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			$image = $_FILES[$name]['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			if ((empty($_FILES[$name]['name'])) && ($autorize_empty == 0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 				$this->erreur = "Vous devez obligatoirement ajouter une image"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 				//test si il y a deja une img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 				if ($old_image_req != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 					$query = $dbc->query($old_image_req); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 					if ((is_array($query)) && (count($query) > 0)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 						foreach ($query as $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 							$this->old_image = $obj->$name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 						} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				//recuperation info sur img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 				$uniqid = uniqid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 				$infos_img = getimagesize($_FILES[$name]['tmp_name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 				if ((!in_array(substr($image, -3), $this->autorized_extention)) || (($infos_img[0] >= $this->width_max) && ($infos_img[1] >= $this->height_max) && ($_FILES[$name]['size'] >= $this->poid_max))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 					$this->erreur = "Problème dans les dimensions, taille ou format (extension) de l'image."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 					return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 				else if (move_uploaded_file($_FILES[$name]['tmp_name'], $this->dossier_image."/".$uniqid.substr($image, -4))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 					$imageok = $uniqid.substr($image, -4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 					$urlimg = $this->dossier_image."/$imageok"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 					$this->chemin_image = $urlimg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 					$this->nom_image = $imageok; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 					if (($delete_old_img == 1) && ($this->old_image != "") && (!empty($_FILES[$name]['name']))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 						$this->setDeleteImage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 					return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 				else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 					$this->erreur = "Impossible d'envoyer votre image sur le serveur, veuillez réessayer dans une instant, si l'erreur se reproduit, contactez votre administrateur"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		 * fonction qui permet de resize des images en fonction d'une taille et d'une hauteur donnée | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		 * si $req_img == null, on prend l'image active dans la class via chemin_image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		 * @param $width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		 * @param $height | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		 * @param $prefixe | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		 * @param null $req_img | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		public function setResizeImage($width, $height, $prefixe, $delete_old = 1, $req_img = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			if (($req_img == null) && ($this->chemin_image != "")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 				$this->getImage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 				$resize = new Resize($this->chemin_image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 				$resize->resizeImage($width, $height, 'crop'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				$img_resize = $prefixe."_".$this->image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 				$resize->saveImage($this->dossier_image."/".$img_resize, 100); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 				$this->nom_image = $img_resize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 				$this->nom_image = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 			if (($delete_old == 1) && ($req_img == null) && ($this->chemin_image != "")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 				unlink($this->chemin_image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		 * fonction qui permet de supprimer une image en fonction d'une requete ou de la variable old_image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		 * @param null|string $nom_image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		 * @return boolean|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		public function setDeleteImage($nom_image = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			//si pas de requete et qu'on a une old_img on la supprime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 			if (($this->old_image != "") && ($nom_image === null)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 				$old_image = explode("/", $this->old_image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 				if (end($old_image) !== "defaut.png") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 					unlink($this->dossier_image."/".end($old_image)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 					return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 			else if (($nom_image !== null) && (unlink($this->dossier_image."/".$nom_image))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 				return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 				$this->erreur = "Impossible de supprimer cette image, veuillez réesayer dans un instant, sinon contacter l'administrateur de votre site"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 				return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 182 |  |  | 		//-------------------------- FIN SETTER ----------------------------------------------------------------------------// | 
            
                                                        
            
                                    
            
            
                | 183 |  |  | 	} | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.