'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', ]; protected $config; /** * @var EntityManager */ protected $em; public function __construct(EntityManager $entityManager) { $this->em = $entityManager; } public function loadGalleryData($includeHidden = false) { $config = $this->getConfig(); $albums = []; foreach ($config['galleries'] as $id => $data) { if ($includeHidden || $data['public']) { $albums[] = [ 'slug' => $id, 'name' => $data['name'], 'coverImage' => isset($data['cover']) ? $this->getCoverImage($data['cover']) : null, 'date' => $this->getGalleryDate($data['dir']), 'type' => $data['type'], 'isNew' => $data['new'], 'isPublic' => $data['public'], 'images' => $this->loadGalleryImages($data['dir']), ]; } } return $albums; } private function getGalleryDate(string $dir): string { $timestamp = sprintf("@%s", filemtime(sprintf(self::IMAGE_BASEDIR, $dir))); $date = new \DateTime($timestamp); return $date->format("Y-m-d"); } /** * @param string $dir * @return Image[] * @todo implement label for image in some way */ private function loadGalleryImages(string $dir): array { $images = array_map('basename', glob( sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir), GLOB_BRACE )); $imagine = new Imagine(); return array_map(function ($image) use ($dir, $imagine) { $imageEntity = $this->em->getRepository(Image::class)->findOneBy([ 'dir' => $dir, 'path' => $image, ]); if (null === $imageEntity) { $imageEntity = new Image(); $imageEntity->setLabel("") ->setDir($dir) ->setPath($image); $this->processImageSizes($dir, $imageEntity, $imagine); $this->em->persist($imageEntity); $this->em->flush(); } return $imageEntity; }, $images); } private function processImageSizes(string $dir, Image $image, Imagine $imagine) { $img = $imagine->open(sprintf(self::IMAGE_BASEDIR, $dir) . $image->getPath()); $imgSize = $img->getSize(); $image->setWidth($imgSize->getWidth()) ->setHeight($imgSize->getHeight()); } /** * @param string $image * @return Image */ private function getCoverImage(string $image): Image { $img = new Image(); return $img->setLabel("") ->setPath($image) ->setWidth(0) ->setHeight(0); } public function getImage(string $slug, string $image, $size = false): string { $config = $this->getConfig(); $galleryDir = sprintf(self::IMAGE_BASEDIR, $config['galleries'][$slug]['dir']); $imageFileName = $size ? $this->getResizedImageName($galleryDir, $image, $size) : ($galleryDir . $image); return $imageFileName; } public function exportGallery(string $slug) { $config = $this->getConfig(); if (!isset($config['galleries'][$slug])) { return false; } $dir = $config['galleries'][$slug]['dir']; $images = array_map('basename', glob(sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir), GLOB_BRACE)); $zipName = tempnam("data/tmp", "export"); $zipArchive = new \ZipArchive(); $zipArchive->open($zipName, \ZipArchive::CREATE); foreach ($images as $image) { $zipArchive->addFile("{$dir}/{$image}"); } $zipArchive->close(); return $zipName; } protected function getResizedImageName($galleryDirectory, $imageName, $size): string { $numericSize = $size == 'thumb' ? self::THUMBNAIL_SIZE : $size; $thumbPath = $galleryDirectory . "thumb_" . $numericSize; @mkdir($thumbPath); $thumbImageName = $thumbPath . "/" . $imageName; if (!file_exists($thumbImageName)) { $thumbSize = new Box($numericSize, $numericSize*10); $imagine = new Imagine(); $image = $imagine->open($galleryDirectory . $imageName) ->thumbnail($thumbSize, ImageInterface::THUMBNAIL_INSET); $image->effects()->sharpen(); $image->save($thumbImageName); } return $thumbImageName; } protected function getConfig() { if (null === $this->config) { $parser = new Parser(); $configReader = new Yaml([$parser, 'parse']); $this->config = $configReader->fromFile(self::CONFIG_FILE); } return $this->config; } }