mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Improved PreviewGenerator by composing an ImageBuilder that creates new Image objects fore each URL
This commit is contained in:
10
module/Common/src/Image/ImageBuilder.php
Normal file
10
module/Common/src/Image/ImageBuilder.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Image;
|
||||
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
use Zend\ServiceManager\AbstractPluginManager;
|
||||
|
||||
class ImageBuilder extends AbstractPluginManager implements ImageBuilderInterface
|
||||
{
|
||||
protected $instanceOf = Image::class;
|
||||
}
|
||||
31
module/Common/src/Image/ImageBuilderFactory.php
Normal file
31
module/Common/src/Image/ImageBuilderFactory.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Image;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Interop\Container\Exception\ContainerException;
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
class ImageBuilderFactory implements FactoryInterface
|
||||
{
|
||||
/**
|
||||
* Create an object
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
* @param string $requestedName
|
||||
* @param null|array $options
|
||||
* @return object
|
||||
* @throws ServiceNotFoundException if unable to resolve the service.
|
||||
* @throws ServiceNotCreatedException if an exception is raised when
|
||||
* creating a service.
|
||||
* @throws ContainerException if any other error occurs
|
||||
*/
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
||||
{
|
||||
return new ImageBuilder($container, ['factories' => [
|
||||
Image::class => ImageFactory::class,
|
||||
]]);
|
||||
}
|
||||
}
|
||||
8
module/Common/src/Image/ImageBuilderInterface.php
Normal file
8
module/Common/src/Image/ImageBuilderInterface.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Image;
|
||||
|
||||
use Zend\ServiceManager\ServiceLocatorInterface;
|
||||
|
||||
interface ImageBuilderInterface extends ServiceLocatorInterface
|
||||
{
|
||||
}
|
||||
@@ -25,6 +25,12 @@ class ImageFactory implements FactoryInterface
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
||||
{
|
||||
$config = $container->get('config')['phpwkhtmltopdf'];
|
||||
return new Image(isset($config['images']) ? $config['images'] : null);
|
||||
$image = new Image(isset($config['images']) ? $config['images'] : null);
|
||||
|
||||
if (isset($options['url'])) {
|
||||
$image->setPage($options['url']);
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,11 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Doctrine\Common\Cache\Cache;
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
|
||||
use Shlinkio\Shlink\Common\Image\ImageBuilder;
|
||||
use Shlinkio\Shlink\Common\Image\ImageBuilderInterface;
|
||||
|
||||
class PreviewGenerator implements PreviewGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @var Image
|
||||
*/
|
||||
private $image;
|
||||
/**
|
||||
* @var Cache
|
||||
*/
|
||||
@@ -20,20 +18,24 @@ class PreviewGenerator implements PreviewGeneratorInterface
|
||||
* @var string
|
||||
*/
|
||||
private $location;
|
||||
/**
|
||||
* @var ImageBuilderInterface
|
||||
*/
|
||||
private $imageBuilder;
|
||||
|
||||
/**
|
||||
* PreviewGenerator constructor.
|
||||
* @param Image $image
|
||||
* @param ImageBuilderInterface $imageBuilder
|
||||
* @param Cache $cache
|
||||
* @param string $location
|
||||
*
|
||||
* @Inject({Image::class, Cache::class, "config.phpwkhtmltopdf.files_location"})
|
||||
* @Inject({ImageBuilder::class, Cache::class, "config.preview_generation.files_location"})
|
||||
*/
|
||||
public function __construct(Image $image, Cache $cache, $location)
|
||||
public function __construct(ImageBuilderInterface $imageBuilder, Cache $cache, $location)
|
||||
{
|
||||
$this->image = $image;
|
||||
$this->cache = $cache;
|
||||
$this->location = $location;
|
||||
$this->imageBuilder = $imageBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,17 +47,19 @@ class PreviewGenerator implements PreviewGeneratorInterface
|
||||
*/
|
||||
public function generatePreview($url)
|
||||
{
|
||||
$cacheId = sprintf('preview_%s.%s', urlencode($url), $this->image->type);
|
||||
/** @var Image $image */
|
||||
$image = $this->imageBuilder->build(Image::class, ['url' => $url]);
|
||||
|
||||
$cacheId = sprintf('preview_%s.%s', urlencode($url), $image->type);
|
||||
if ($this->cache->contains($cacheId)) {
|
||||
return $this->cache->fetch($cacheId);
|
||||
}
|
||||
|
||||
$path = $this->location . '/' . $cacheId;
|
||||
$this->image->setPage($url);
|
||||
$this->image->saveAs($path);
|
||||
$image->saveAs($path);
|
||||
|
||||
// Check if an error occurred
|
||||
$error = $this->image->getError();
|
||||
$error = $image->getError();
|
||||
if (! empty($error)) {
|
||||
throw PreviewGenerationException::fromImageError($error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user