Initial commit
This commit is contained in:
104
src/App/Service/GalleryService.php
Normal file
104
src/App/Service/GalleryService.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?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
|
||||
{
|
||||
|
||||
const CONFIG_FILE = 'data/galleries/gallery.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 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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $config['galleries'][$slug]['name'],
|
||||
'type' => $config['galleries'][$slug]['type'],
|
||||
'images' => $images,
|
||||
];
|
||||
}
|
||||
|
||||
public function getImage(string $slug, string $image, bool $size)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
$galleryDir = sprintf(self::IMAGE_BASEDIR, $config['galleries'][$slug]['dir']);
|
||||
$imageFileName = $size
|
||||
? $galleryDir . $image
|
||||
: $this->getResizedImageName($galleryDir, $image, $size);
|
||||
|
||||
return fopen($imageFileName, 'r');
|
||||
}
|
||||
|
||||
protected function getResizedImageName($galleryDirectory, $imageName, $size)
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user