Added wkhtmltopdf stuff and created preview generator service

This commit is contained in:
Alejandro Celaya
2016-08-18 10:19:33 +02:00
parent 20e43aac90
commit 26adf48b48
8 changed files with 180 additions and 3 deletions

View 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);
}
}

View 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;
}
}

View 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);
}