Paperyard
Thumbnail.php
Go to the documentation of this file.
1 <?php
2 
4 
9 use Imagick;
10 
11 class Thumbnail
12 {
14  protected $logger;
15 
16  public function __construct(LoggerInterface $logger)
17  {
18  $this->logger = $logger;
19  }
20 
21  public function __invoke(Request $request, Response $response, $args)
22  {
23  $path = base64_decode($request->getAttribute('path'));
24  $page = (int)$request->getAttribute('page');
25  $resolution = (int)$request->getAttribute('resolution');
26 
27  // set default resolution
28  if ($resolution == 0) {
29  $resolution = 72;
30  }
31 
32  // build
33  $document_hash = (new Document($path))->hash;
34  $thumbnail_path = 'static/img/cache/' . $document_hash . '[' . $page . '][' . $resolution . '].jpg';
35 
36  // check if image is in cache
37  if (file_exists($thumbnail_path)) {
38  $cached_thumbnail_response = $response->withHeader('Content-type', 'image/jpeg');
39  readfile($thumbnail_path);
40  return $cached_thumbnail_response;
41  }
42 
43  // load page and cache to disk
44  $im = new Imagick();
45  $im->setResolution($resolution, $resolution);
46  $im->readImage($path . '[' . $page . ']');
47  $im->setImageFormat('jpg');
48  $im->writeImage($thumbnail_path);
49 
50  // add content type and output image data
51  $thumbnail_response = $response->withHeader('Content-type', 'image/jpeg');
52  echo $im;
53 
54  // free memory and return response
55  $im->clear();
56  $im->destroy();
57  return $thumbnail_response;
58  }
59 
60 }
__construct(LoggerInterface $logger)
Definition: Thumbnail.php:16
__invoke(Request $request, Response $response, $args)
Definition: Thumbnail.php:21