2017-07-21 19:59:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/';
|
|
|
|
|
const THUMBNAIL_SIZE = 250;
|
|
|
|
|
|
|
|
|
|
protected $contentTypeMap = [
|
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
|
'jpeg' => 'image/jpeg',
|
|
|
|
|
'png' => 'image/png',
|
|
|
|
|
];
|
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
|
|
public function loadGalleryData($includeHidden = false)
|
|
|
|
|
{
|
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
$slugs = [];
|
|
|
|
|
foreach ($config['galleries'] as $id => $data) {
|
|
|
|
|
if ($includeHidden || $data['public']) {
|
|
|
|
|
$slugs[] = [
|
|
|
|
|
'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-09 15:47:05 +02:00
|
|
|
return $slugs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 array
|
|
|
|
|
* @todo implement label for image in some way
|
|
|
|
|
*/
|
|
|
|
|
private function loadGalleryImages(string $dir)
|
|
|
|
|
{
|
|
|
|
|
$images = array_map('basename', glob(
|
|
|
|
|
sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir),
|
|
|
|
|
GLOB_BRACE
|
|
|
|
|
));
|
|
|
|
|
return array_map(function ($image) {
|
|
|
|
|
return [
|
|
|
|
|
'label' => '',
|
|
|
|
|
'path' => $image,
|
|
|
|
|
];
|
|
|
|
|
}, $images);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $image
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getCoverImage(string $image)
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'label' => '',
|
|
|
|
|
'path' => $image,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function listGalleries($includeHidden = false)
|
|
|
|
|
{
|
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
$slugs = [];
|
|
|
|
|
foreach ($config['galleries'] as $id => $data) {
|
|
|
|
|
if ($includeHidden || $data['public']) {
|
|
|
|
|
$slugs[] = [
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'label' => $data['name'],
|
|
|
|
|
'new' => $data['new'],
|
2017-07-21 19:59:16 +02:00
|
|
|
];
|
2017-08-09 15:47:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $slugs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getGallery(string $slug)
|
|
|
|
|
{
|
|
|
|
|
$galleryList = $this->listGalleries(true);
|
|
|
|
|
$slugs = array_map(function ($item) {
|
|
|
|
|
return $item['id'];
|
|
|
|
|
}, $galleryList);
|
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
$images = [];
|
|
|
|
|
|
|
|
|
|
if (in_array($slug, $slugs)) {
|
|
|
|
|
$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
|
|
|
return [
|
|
|
|
|
'name' => $config['galleries'][$slug]['name'],
|
|
|
|
|
'type' => $config['galleries'][$slug]['type'],
|
|
|
|
|
'images' => $images,
|
|
|
|
|
];
|
|
|
|
|
}
|
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)) {
|
|
|
|
|
$thumbSize = new Box($numericSize, $numericSize);
|
|
|
|
|
$imagine = new Imagine();
|
|
|
|
|
$image = $imagine->open($galleryDirectory . $imageName)
|
|
|
|
|
->thumbnail($thumbSize, ImageInterface::THUMBNAIL_OUTBOUND);
|
|
|
|
|
$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
|
|
|
}
|