mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Added wkhtmltopdf stuff and created preview generator service
This commit is contained in:
30
module/Common/src/Image/ImageFactory.php
Normal file
30
module/Common/src/Image/ImageFactory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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 ImageFactory 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)
|
||||
{
|
||||
$config = $container->get('config')['phpwkhtmltopdf'];
|
||||
return new Image(isset($config['images']) ? $config['images'] : null);
|
||||
}
|
||||
}
|
||||
58
module/Common/src/Service/PreviewGenerator.php
Normal file
58
module/Common/src/Service/PreviewGenerator.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Service;
|
||||
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Doctrine\Common\Cache\Cache;
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
|
||||
class PreviewGenerator implements PreviewGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @var Image
|
||||
*/
|
||||
private $image;
|
||||
/**
|
||||
* @var Cache
|
||||
*/
|
||||
private $cache;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location;
|
||||
|
||||
/**
|
||||
* PreviewGenerator constructor.
|
||||
* @param Image $image
|
||||
* @param Cache $cache
|
||||
* @param string $location
|
||||
*
|
||||
* @Inject({Image::class, Cache::class, "config.phpwkhtmltopdf.images.files_location"})
|
||||
*/
|
||||
public function __construct(Image $image, Cache $cache, $location)
|
||||
{
|
||||
$this->image = $image;
|
||||
$this->cache = $cache;
|
||||
$this->location = $location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates and stores preview for provided website and returns the path to the image file
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
public function generatePreview($url)
|
||||
{
|
||||
$cacheId = sprintf('preview_%s.png', urlencode($url));
|
||||
if ($this->cache->contains($cacheId)) {
|
||||
return $this->cache->fetch($cacheId);
|
||||
}
|
||||
|
||||
$path = $this->location . '/' . $cacheId;
|
||||
$this->image->setPage($url);
|
||||
$this->image->saveAs($path);
|
||||
$this->cache->save($cacheId, $path);
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
13
module/Common/src/Service/PreviewGeneratorInterface.php
Normal file
13
module/Common/src/Service/PreviewGeneratorInterface.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Service;
|
||||
|
||||
interface PreviewGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generates and stores preview for provided website and returns the path to the image file
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
public function generatePreview($url);
|
||||
}
|
||||
Reference in New Issue
Block a user