gallery-api/src/App/Service/GalleryService.php

181 lines
4.8 KiB
PHP
Raw Normal View History

2017-07-21 19:59:16 +02:00
<?php
namespace App\Service;
2017-08-12 22:56:25 +02:00
use App\Entity\Image;
use Doctrine\ORM\EntityManager;
2017-07-21 19:59:16 +02:00
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Symfony\Component\Yaml\Parser;
use Zend\Config\Reader\Yaml;
class GalleryService
{
2017-08-09 15:47:05 +02:00
const CONFIG_FILE = 'data/galleries/config.yaml';
const IMAGE_BASEDIR = 'data/galleries/%s/';
2017-08-12 17:39:17 +02:00
const THUMBNAIL_SIZE = 500;
2017-08-09 15:47:05 +02:00
protected $contentTypeMap = [
2017-08-12 22:56:25 +02:00
'jpg' => 'image/jpeg',
2017-08-09 15:47:05 +02:00
'jpeg' => 'image/jpeg',
2017-08-12 22:56:25 +02:00
'png' => 'image/png',
2017-08-09 15:47:05 +02:00
];
protected $config;
2017-08-12 22:56:25 +02:00
/**
* @var EntityManager
*/
protected $em;
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
2017-08-09 15:47:05 +02:00
public function loadGalleryData($includeHidden = false)
{
$config = $this->getConfig();
2017-08-12 22:56:25 +02:00
$albums = [];
2017-08-09 15:47:05 +02:00
foreach ($config['galleries'] as $id => $data) {
if ($includeHidden || $data['public']) {
2017-08-12 22:56:25 +02:00
$albums[] = [
2017-08-09 15:47:05 +02:00
'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']),
];
}
2017-07-21 19:59:16 +02:00
}
2017-08-12 22:56:25 +02:00
return $albums;
2017-08-09 15:47:05 +02:00
}
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
2017-08-12 22:56:25 +02:00
* @return Image[]
2017-08-09 15:47:05 +02:00
* @todo implement label for image in some way
*/
2017-08-12 22:56:25 +02:00
private function loadGalleryImages(string $dir): array
2017-08-09 15:47:05 +02:00
{
$images = array_map('basename', glob(
sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir),
GLOB_BRACE
));
2017-08-12 22:56:25 +02:00
$imagine = new Imagine();
return array_map(function ($image) use ($dir, $imagine) {
$imageEntity = $this->em->getRepository(Image::class)->findOneBy([
'dir' => $dir,
2017-08-09 15:47:05 +02:00
'path' => $image,
2017-08-12 22:56:25 +02:00
]);
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;
2017-08-09 15:47:05 +02:00
}, $images);
}
2017-08-12 22:56:25 +02:00
private function processImageSizes(string $dir, Image $image, Imagine $imagine)
2017-08-09 15:47:05 +02:00
{
2017-08-12 22:56:25 +02:00
$img = $imagine->open(sprintf(self::IMAGE_BASEDIR, $dir) . $image->getPath());
$imgSize = $img->getSize();
$image->setWidth($imgSize->getWidth())
->setHeight($imgSize->getHeight());
2017-08-09 15:47:05 +02:00
}
2017-08-12 22:56:25 +02:00
/**
* @param string $image
* @return Image
*/
private function getCoverImage(string $image): Image
2017-08-09 15:47:05 +02:00
{
2017-08-12 22:56:25 +02:00
$img = new Image();
return $img->setLabel("")
->setPath($image)
->setWidth(0)
->setHeight(0);
2017-08-09 15:47:05 +02:00
}
2017-07-21 19:59:16 +02:00
2017-08-09 15:47:05 +02:00
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;
}
2017-07-21 19:59:16 +02:00
2017-08-09 15:47:05 +02:00
public function exportGallery(string $slug)
{
$config = $this->getConfig();
2017-07-21 19:59:16 +02:00
2017-08-09 15:47:05 +02:00
if (!isset($config['galleries'][$slug])) {
return false;
}
2017-07-21 19:59:16 +02:00
2017-08-09 15:47:05 +02:00
$dir = $config['galleries'][$slug]['dir'];
$images = array_map('basename', glob(sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir), GLOB_BRACE));
2017-07-21 19:59:16 +02:00
2017-08-09 15:47:05 +02:00
$zipName = tempnam("data/tmp", "export");
$zipArchive = new \ZipArchive();
$zipArchive->open($zipName, \ZipArchive::CREATE);
foreach ($images as $image) {
$zipArchive->addFile("{$dir}/{$image}");
2017-07-21 19:59:16 +02:00
}
2017-08-09 15:47:05 +02:00
$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)) {
2017-08-12 17:39:17 +02:00
$thumbSize = new Box($numericSize, $numericSize*10);
2017-08-09 15:47:05 +02:00
$imagine = new Imagine();
$image = $imagine->open($galleryDirectory . $imageName)
2017-08-12 17:39:17 +02:00
->thumbnail($thumbSize, ImageInterface::THUMBNAIL_INSET);
2017-08-09 15:47:05 +02:00
$image->effects()->sharpen();
$image->save($thumbImageName);
}
return $thumbImageName;
}
2017-07-21 19:59:16 +02:00
2017-08-09 15:47:05 +02:00
protected function getConfig()
{
if (null === $this->config) {
$parser = new Parser();
$configReader = new Yaml([$parser, 'parse']);
$this->config = $configReader->fromFile(self::CONFIG_FILE);
2017-07-21 19:59:16 +02:00
}
2017-08-09 15:47:05 +02:00
return $this->config;
}
2017-07-21 19:59:16 +02:00
}