Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #119 from atrocore/pdf-thumb-adaptation
Browse files Browse the repository at this point in the history
Optimized PDF thumbnail
  • Loading branch information
rratsun authored Oct 3, 2023
2 parents b41987a + 875122e commit c91bb44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/Core/Thumbnail/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function createImageFromPdf(string $pdfPath): string

$original = $dirPath . '/page-1.png';
if (!file_exists($original)) {
$pdflib = new PDFLib();
$pdflib = new PDFLib($this->getConfig());
$pdflib->setPdfPath($pdfPath);
$pdflib->setOutputPath($dirPath);
$pdflib->setImageFormat(PDFLib::$IMAGE_FORMAT_PNG);
Expand Down
18 changes: 14 additions & 4 deletions app/Core/Utils/PDFLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Dam\Core\Utils;

use Espo\Core\Utils\Config;

/**
* Class PDFLib
*/
Expand All @@ -38,7 +40,9 @@ class PDFLib
private $gs_is_64 = null;
private $gs_path = null;

public function __construct()
private $config = null;

public function __construct(Config $config = null)
{
$this->resolution = 0;
$this->jpeg_quality = 100;
Expand All @@ -63,6 +67,10 @@ public function __construct()
throw new \Exception("Your version of GhostScript $gs_version is not compatible with the library", 403);
}
}

if (!empty($config)) {
$this->config = $config;
}
}

/**
Expand Down Expand Up @@ -154,17 +162,19 @@ public function setFilePrefix($fileprefix)
*
* @return self
*/
public function setImageFormat($imageformat, $pngScaleFactor = null)
public function setImageFormat($imageformat)
{
$pngScaleFactor = !empty($this->config) ? $this->config->get('gsDownScaleFactor') : null;

if ($imageformat == self::$IMAGE_FORMAT_JPEG) {
$this->imageDeviceCommand = "jpeg";
$this->imageExtention = "jpg";
$this->pngDownScaleFactor = isset($pngScaleFactor) ? "-dDownScaleFactor=" . $pngScaleFactor : "";
$this->pngDownScaleFactor = !empty($pngScaleFactor) ? "-dDownScaleFactor=" . $pngScaleFactor : "";
} else {
if ($imageformat == self::$IMAGE_FORMAT_PNG) {
$this->imageDeviceCommand = "png16m";
$this->imageExtention = "png";
$this->pngDownScaleFactor = "";
$this->pngDownScaleFactor = !empty($pngScaleFactor) ? "-dDownScaleFactor=" . $pngScaleFactor : "";
}
}
return $this;
Expand Down
18 changes: 18 additions & 0 deletions app/Repositories/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,23 @@ protected function afterRemove(Entity $entity, array $options = [])
if (!$res) {
parent::afterRemove($entity, $options);
}

if ($this->isPdf($entity)) {
$dirPath = $this->getConfig()->get('filesPath', 'upload/files/') . $entity->getStorageFilePath();

$this->getFileManager()->unlink($dirPath . '/page-1.png');
}
}

/**
* @param Entity $entity
*
* @return bool
*/
protected function isPdf(Entity $entity): bool
{
$parts = explode('.', $entity->get('name'));

return strtolower(array_pop($parts)) === 'pdf';
}
}

0 comments on commit c91bb44

Please sign in to comment.