Récupérer une image d'un dossier en php

Par Motel, il y a 8 ans


Bonjour,

Je cherche a récupérer la première image d'un dossier je ne sais pas du tout comment m'y prendre j'ai réussi à récupérer plusieurs images d'un dossier grâce à la fonction ci-dessous mais je n'ai pas trouvé comment récupérer qu'une seule image :/

public function galleryProduct($gallery) { $path = "assets/img/".$gallery."/*"; foreach (glob($path) as $file){ echo "<li class='card-thim' data-thumb='$file'> <div class='full'> <div class='full-screen' style='background-image: url($file);'></div> </div> </li>"; } }

1 réponse

nico41, il y a 8 ans
<?php /** * fonction qui renvoi un tableau des infos ( type SplFileInfo ) des fichiers * images du dossier ( $absolutePath ) filtrées par extension si nécssaire ( $extension ) * * SplFileInfo : http://php.net/manual/fr/class.splfileinfo.php * * @param string $absolutePath : chemin absolu vers le dossier des images * @param string $extension : ex jpeg, jpg, gif, png ou rien pour toutes * * @return SplFileInfo[] * @throws Exception */ function getImages($absolutePath = '', $extension = '') { $allowedExtensions = "gif|jpe?g|png"; $extension = strtolower($extension); if($extension && !preg_match("/^(".$allowedExtensions.")$/", $exension)) { throw new \Exception("Extension de fichier non autorisée"); } $absolutePath = $absolutePath == '' ? '.' : $absolutePath; $extension = $extension == '' ? $allowedExtensions : $extension; $pattern = "/\.(".$extension.")$/i"; /** * http://php.net/manual/fr/class.filesystemiterator.php */ $filesystemIterator = new FilesystemIterator($absolutePath); /** * http://php.net/manual/fr/class.regexiterator.php */ $regexIterator = new RegexIterator($filesystemIterator, $pattern); $imgs = []; foreach ($regexIterator as $k=>$splFileInfo) { $imgs[]=$splFileInfo; } return $imgs; } /** * Renvoi le chemin relatif de la première image ou d'une image par defaut ou rien * * @param string $webPath : chemin relatif * @param string $absolutePathToImagesDirectory : chemin absolu * @param string $extension : ex jpeg, jpg, gif, png ou rien pour toutes * @param string $defaultImagePath : pour un affichage par défaut * * @return string le chemin relatif de la première image ou une image par défaut si le dossier ciblé est vide * @throws Exception */ function getFirstImage($webPath = '', $absolutePathToImagesDirectory = '', $extension = '', $defaultImagePath = '') { $images = getImages($absolutePathToImagesDirectory, $extension); if(!empty($images)) { /** * @var SplFileInfo[] $images */ return rtrim($webPath, "/") . '/' . $images[0]->getFilename(); } return $defaultImagePath; } /** * @param string $webPath : chemin relatif * @param string $absolutePathToImagesDirectory : chemin absolu * @param string $extension : ex jpeg, jpg, gif, png ou rien pour toutes * @param string $defaultImagePath : pour un affichage par défaut * * @return string le tag html img pour la première image ou une image par défaut si le dossier ciblé est vide, ou rien * @throws Exception */ function displayFirstImage($webPath = '', $absolutePathToImagesDirectory = '', $extension = '', $defaultImagePath = '') { $image = getFirstImage($webPath, $absolutePathToImagesDirectory, $extension, $defaultImagePath); if($image) { return "<img src='$image'/>"; } return ""; } /* exemple */ $gallery = 'myBeautifullGallery'; $webPath = "/assets/img/$gallery"; $absolutePathToImagesDirectory = "/var/www/monsite/public/" . ltrim($webPath, "/"); $defaultImagePath = "/assets/img/default.jpg"; /* première image quelque soit son extension */ echo displayFirstImage($webPath, $absolutePathToImagesDirectory, null, $defaultImagePath); /* première image jpg */ echo displayFirstImage($webPath, $absolutePathToImagesDirectory, 'jpg', $defaultImagePath);