mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-17 15:03:25 +00:00
Extract thumbnail manager classes from appbox
This commit is contained in:
126
lib/Alchemy/Phrasea/Core/Thumbnail/AbstractThumbnailManager.php
Normal file
126
lib/Alchemy/Phrasea/Core/Thumbnail/AbstractThumbnailManager.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Alchemy\Phrasea\Core\Thumbnail;
|
||||
|
||||
use Alchemy\Phrasea\Application;
|
||||
use MediaAlchemyst\Alchemyst;
|
||||
use MediaAlchemyst\Specification\Image as ImageSpecification;
|
||||
use MediaVorus\Media\Image;
|
||||
use MediaVorus\Media\MediaInterface;
|
||||
use MediaVorus\Media\Video;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
abstract class AbstractThumbnailManager
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected static $allowedMimeTypes = [
|
||||
'image/gif',
|
||||
'image/png',
|
||||
'image/jpeg',
|
||||
'image/jpg',
|
||||
'image/pjpeg'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var Alchemyst
|
||||
*/
|
||||
protected $alchemyst;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $application;
|
||||
|
||||
/**
|
||||
* @var Filesystem
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $rootPath;
|
||||
|
||||
/**
|
||||
* @param Application $application
|
||||
* @param Alchemyst $alchemyst
|
||||
* @param Filesystem $filesystem
|
||||
* @param $rootPath
|
||||
*/
|
||||
public function __construct(Application $application, Alchemyst $alchemyst, Filesystem $filesystem, $rootPath)
|
||||
{
|
||||
$this->alchemyst = $alchemyst;
|
||||
$this->application = $application;
|
||||
$this->filesystem = $filesystem;
|
||||
$this->rootPath = $rootPath;
|
||||
}
|
||||
|
||||
protected function validateFileMimeType(File $file)
|
||||
{
|
||||
if (!in_array(mb_strtolower($file->getMimeType()), self::$allowedMimeTypes)) {
|
||||
throw new \InvalidArgumentException('Invalid file format');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MediaInterface $media
|
||||
* @param int $maxWidth
|
||||
* @param int $maxHeight
|
||||
* @return bool
|
||||
*/
|
||||
protected function shouldResize($media, $maxWidth, $maxHeight)
|
||||
{
|
||||
if (! $media instanceof Image && ! $media instanceof Video) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $media->getWidth() > $maxWidth || $media->getHeight() > $maxHeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImageSpecification $imageSpec
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*/
|
||||
protected function setSpecificationSize(ImageSpecification $imageSpec, $width, $height)
|
||||
{
|
||||
$imageSpec->setResizeMode(ImageSpecification::RESIZE_MODE_INBOUND_FIXEDRATIO);
|
||||
$imageSpec->setDimensions($width, $height);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param File $file
|
||||
* @param $imageSpec
|
||||
* @return string
|
||||
* @throws \MediaAlchemyst\Exception\FileNotFoundException
|
||||
*/
|
||||
protected function resizeMediaFile(File $file, $imageSpec)
|
||||
{
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'tmpdatabox') . '.jpg';
|
||||
$this->alchemyst->turninto($file->getPathname(), $tmp, $imageSpec);
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $target
|
||||
* @param string $filename
|
||||
*/
|
||||
protected function copyFile($target, $filename)
|
||||
{
|
||||
if (is_file($target)) {
|
||||
$this->filesystem->remove($target);
|
||||
}
|
||||
|
||||
if (null === $target || null === $filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->filesystem->mkdir(dirname($target), 0750);
|
||||
$this->filesystem->copy($filename, $target, true);
|
||||
$this->filesystem->chmod($target, 0760);
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Alchemy\Phrasea\Core\Thumbnail;
|
||||
|
||||
use Alchemy\Phrasea\Application;
|
||||
use MediaAlchemyst\Specification\Image as ImageSpecification;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
class CollectionThumbnailManager extends AbstractThumbnailManager implements ThumbnailManager
|
||||
{
|
||||
|
||||
/**
|
||||
* @param ThumbnailedElement $element
|
||||
* @param $thumbnailType
|
||||
* @param File $file
|
||||
*/
|
||||
public function setThumbnail(ThumbnailedElement $element, $thumbnailType, File $file = null)
|
||||
{
|
||||
$filename = null;
|
||||
|
||||
if (!is_null($file)) {
|
||||
$this->validateFileMimeType($file);
|
||||
$filename = $this->generateThumbnail($thumbnailType, $file);
|
||||
}
|
||||
|
||||
$logoFile = $this->rootPath . '/config/' . $thumbnailType . '/' . $element->getRootIdentifier();
|
||||
$custom_path = $this->rootPath . '/www/custom/' . $thumbnailType . '/' . $element->getRootIdentifier();
|
||||
|
||||
foreach ([$logoFile, $custom_path] as $target) {
|
||||
$this->copyFile($target, $filename);
|
||||
}
|
||||
|
||||
$element->updateThumbnail($thumbnailType, $file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $thumbnailType
|
||||
* @param File $file
|
||||
* @return string
|
||||
*/
|
||||
protected function generateThumbnail($thumbnailType, File $file)
|
||||
{
|
||||
$filename = $file->getPathname();
|
||||
$imageSpec = new ImageSpecification();
|
||||
|
||||
if ($thumbnailType === ThumbnailManager::TYPE_LOGO) {
|
||||
//resize collection logo
|
||||
$media = $this->application->getMediaFromUri($filename);
|
||||
|
||||
if ($this->shouldResize($media, 120, 24)) {
|
||||
$this->setSpecificationSize($imageSpec, 120, 24);
|
||||
}
|
||||
|
||||
$filename = $this->resizeMediaFile($file, $imageSpec);
|
||||
|
||||
return $filename;
|
||||
} elseif ($thumbnailType === ThumbnailManager::TYPE_PRESENTATION) {
|
||||
//resize collection logo
|
||||
$this->setSpecificationSize($imageSpec, 650, 200);
|
||||
|
||||
$filename = $this->resizeMediaFile($file, $imageSpec);
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Alchemy\Phrasea\Core\Thumbnail;
|
||||
|
||||
use Alchemy\Phrasea\Application;
|
||||
use MediaAlchemyst\Specification\Image as ImageSpecification;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
class DataboxThumbnailManager extends AbstractThumbnailManager implements ThumbnailManager
|
||||
{
|
||||
public function setThumbnail(ThumbnailedElement $element, $thumbnailType, File $file = null)
|
||||
{
|
||||
$filename = null;
|
||||
|
||||
if ($thumbnailType !== ThumbnailManager::TYPE_PDF) {
|
||||
throw new \InvalidArgumentException('Unsupported thumbnail type.');
|
||||
}
|
||||
|
||||
if (!is_null($file)) {
|
||||
$this->validateFileMimeType($file);
|
||||
$filename = $this->generateThumbnail($file);
|
||||
}
|
||||
|
||||
$logoFile = $this->rootPath . '/config/minilogos/' . $thumbnailType . '_' . $element->getRootIdentifier() . '.jpg';
|
||||
$custom_path = $this->rootPath . '/www/custom/minilogos/' . $thumbnailType . '_' . $element->getRootIdentifier() . '.jpg';
|
||||
|
||||
foreach ([$logoFile, $custom_path] as $target) {
|
||||
$this->copyFile($target, $filename);
|
||||
}
|
||||
|
||||
$element->updateThumbnail($thumbnailType, $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param File $file
|
||||
* @return string
|
||||
*/
|
||||
protected function generateThumbnail(File $file)
|
||||
{
|
||||
$imageSpec = new ImageSpecification();
|
||||
|
||||
$this->setSpecificationSize($imageSpec, 120, 35);
|
||||
|
||||
$filename = $this->resizeMediaFile($file, $imageSpec);
|
||||
|
||||
return $filename;
|
||||
}
|
||||
}
|
21
lib/Alchemy/Phrasea/Core/Thumbnail/ThumbnailManager.php
Normal file
21
lib/Alchemy/Phrasea/Core/Thumbnail/ThumbnailManager.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Alchemy\Phrasea\Core\Thumbnail;
|
||||
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
interface ThumbnailManager
|
||||
{
|
||||
|
||||
const TYPE_LOGO = 'minilogos';
|
||||
|
||||
const TYPE_PDF = 'logopdf';
|
||||
|
||||
const TYPE_WM = 'wm';
|
||||
|
||||
const TYPE_STAMP = 'stamp';
|
||||
|
||||
const TYPE_PRESENTATION = 'presentation';
|
||||
|
||||
public function setThumbnail(ThumbnailedElement $element, $thumbnailType, File $file = null);
|
||||
}
|
13
lib/Alchemy/Phrasea/Core/Thumbnail/ThumbnailedElement.php
Normal file
13
lib/Alchemy/Phrasea/Core/Thumbnail/ThumbnailedElement.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Alchemy\Phrasea\Core\Thumbnail;
|
||||
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
interface ThumbnailedElement
|
||||
{
|
||||
|
||||
public function getRootIdentifier();
|
||||
|
||||
public function updateThumbnail($thumbnailType, File $file = null);
|
||||
}
|
Reference in New Issue
Block a user