path = $path; } } /** * @param UploadedFileInterface $file * @param null|string $oldFile * @return null|string */ public function upload(UploadedFileInterface $file, ?string $oldFile = null): ?string { //echo "getError = "; //var_dump($file->getError()); if ($file->getError() === UPLOAD_ERR_OK) { // echo 'Erreur !'; // echo "oldFile = ".$oldFile; // die(); $this->delete($oldFile); //$targetPath = $this->addCopySuffix($this->path . DIRECTORY_SEPARATOR . $file->getClientFilename()); $targetPath = $this->path . DIRECTORY_SEPARATOR . $file->getClientFilename(); $dirname = pathinfo($targetPath, PATHINFO_DIRNAME); if (!file_exists($dirname)) { mkdir($dirname, 777, true); } $file->moveTo($targetPath); $this->generateFormats($targetPath); return pathinfo($targetPath)['basename']; } return null; } private function addCopySuffix(string $targetPath): string { if (file_exists($targetPath)) { return $this->addCopySuffix($this->getPathWithSuffix($targetPath, 'copy')); } return $targetPath; } public function delete(?string $oldFile): void { if ($oldFile) { $oldFile = $this->path . DIRECTORY_SEPARATOR . $oldFile; if (file_exists($oldFile)) { unlink($oldFile); } foreach ($this->formats as $format => $_) { $oldFileWithFormat = $this->getPathWithSuffix($oldFile, $format); if (file_exists($oldFileWithFormat)) { unlink($oldFileWithFormat); } } } } private function getPathWithSuffix(string $path, string $suffix): string { $info = pathinfo($path); return $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '_' . $suffix .'.' . $info['extension']; } private function generateFormats($targetPath) { foreach ($this->formats as $format => $size) { $manager = new ImageManager(['driver' => 'imagick']); $destination = $this->getPathWithSuffix($targetPath, $format); //echo "destination"; //var_dump($destination); //echo "


"; [$width , $height] = $size; //echo 'size ='; //var_dump($size); //echo "


"; //echo "targetPath ="; //var_dump($targetPath); $manager->make($targetPath)->fit($width, $height)->save($destination); } } }