From 89a5f95b714acfd8e111993ec9b84496b4459a74 Mon Sep 17 00:00:00 2001
From: Knah Tsaeb
Date: Wed, 28 Jun 2023 12:29:20 +0200
Subject: [PATCH] Add select img format type (jpeg, png)
---
app/Controllers/Backend.php | 8 +++++++-
app/Soshot/GetFav.php | 8 ++++----
app/Soshot/GetOg.php | 3 ++-
app/Soshot/GetThumb.php | 12 +++++++-----
app/Soshot/MakeThumb.php | 6 +++++-
app/Utils/Page.php | 10 ++++++++--
app/Utils/ShowImg.php | 6 +++---
datas/config.default.json | 3 ++-
tpl/nav.php | 2 +-
tpl/settings.php | 9 ++++++++-
10 files changed, 47 insertions(+), 20 deletions(-)
diff --git a/app/Controllers/Backend.php b/app/Controllers/Backend.php
index 3dfd38e..c97cffe 100644
--- a/app/Controllers/Backend.php
+++ b/app/Controllers/Backend.php
@@ -45,7 +45,7 @@ class Backend {
}
if (!empty($params->showImg)) {
- $img = new ShowImg($params->type, $params->showImg);
+ $img = new ShowImg($params->type, $params->showImg, $conf->fileFormat);
exit();
}
@@ -182,6 +182,10 @@ class Backend {
$uConfig['permitType'] = [];
}
+ if (!empty($post->fileFormat)) {
+ $uConfig['fileFormat'] = $post->fileFormat;
+ }
+
if (!empty($post->password)) {
$uConfig['password'] = password_hash($post->password, PASSWORD_DEFAULT);
}
@@ -192,6 +196,8 @@ class Backend {
if (!empty($post->chromePath)) {
$uConfig['chromePath'] = $post->chromePath;
+ } else {
+ $uConfig['chromePath'] = '';
}
$uConfig->toFile($userConfig);
diff --git a/app/Soshot/GetFav.php b/app/Soshot/GetFav.php
index 5459c5d..ab3fc97 100644
--- a/app/Soshot/GetFav.php
+++ b/app/Soshot/GetFav.php
@@ -21,18 +21,18 @@ class GetFav extends GetThumb {
if (!is_null($info->favicon)) {
if (!$favicon = @file_get_contents($info->favicon)) {
- copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
+ copy(__DIR__ . '/../../src/images/error_fav.'.$this->fileFormat, $this->demande->requestImg);
$this->db->addUpdate(2);
return true;
}
} elseif (!is_null($info->icon)) {
if (!$favicon = @file_get_contents($info->icon)) {
- copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
+ copy(__DIR__ . '/../../src/images/error_fav.'.$this->fileFormat, $this->demande->requestImg);
$this->db->addUpdate(2);
return true;
}
} else {
- copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
+ copy(__DIR__ . '/../../src/images/error_fav.'.$this->fileFormat, $this->demande->requestImg);
$this->db->addUpdate(2);
return true;
}
@@ -78,7 +78,7 @@ class GetFav extends GetThumb {
//n_print($this);
//echo '';
- header("Content-type: image/png");
+ header("Content-type: image/$this->fileFormat");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
echo file_get_contents($this->requestImg);
exit();
diff --git a/app/Soshot/GetOg.php b/app/Soshot/GetOg.php
index 5e84773..94213dc 100644
--- a/app/Soshot/GetOg.php
+++ b/app/Soshot/GetOg.php
@@ -38,7 +38,8 @@ class GetOg extends GetThumb {
file_put_contents($this->demande->requestImg, $image);
- if (in_array(mime_content_type($this->demande->requestImg), ['image/jpg', 'image/jpeg', 'image/bmp', 'image/webp'])) {
+ if (in_array(mime_content_type($this->demande->requestImg), ['image/jpg', 'image/jpeg', 'image/bmp', 'image/webp']) && $this->fileFormat === 'png') {
+ // @todo convertToWebp convertToJpg
ConvertToPng::convertToPng($this->demande);
}
} catch (\Exception $e) {
diff --git a/app/Soshot/GetThumb.php b/app/Soshot/GetThumb.php
index ddbda96..3b97726 100644
--- a/app/Soshot/GetThumb.php
+++ b/app/Soshot/GetThumb.php
@@ -19,6 +19,7 @@ class GetThumb {
protected $conf;
protected $db;
protected $demande;
+ protected $fileFormat;
function __construct($params, $conf) {
$hmac = new Hmac($conf->key);
@@ -33,13 +34,14 @@ class GetThumb {
$this->params = $params;
$this->type = $params->type;
+ $this->fileFormat = $this->conf->fileFormat;
$this->filePath = $hmac->makeFilePath($this->receiveHmac);
if ($this->type === 'pdf') {
$this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.pdf';
} else {
- $this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.png';
+ $this->requestImg = $this->filePath . $this->receiveHmac . '_' . $this->type . '.'.$this->fileFormat;
}
- $this->complete = __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.png';
+ $this->complete = __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.'.$this->fileFormat;
$this->demande = (object)[
'url' => $this->params->url,
@@ -63,7 +65,7 @@ class GetThumb {
echo file_get_contents($this->requestImg);
exit();
} else if (file_exists($this->requestImg) && in_array($this->type, ['complete', 'full', 'hd', 'thumb'])) {
- header("Content-type: image/png");
+ header("Content-type: image/$this->fileFormat");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
echo file_get_contents($this->requestImg);
exit();
@@ -75,7 +77,7 @@ class GetThumb {
'type' => $this->type
])) {
$this->db->addUpdate(1);
- header("Content-type: image/png");
+ header("Content-type: image/$this->fileFormat");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
echo file_get_contents($this->requestImg);
exit();
@@ -89,7 +91,7 @@ class GetThumb {
'hmac' => $this->params->hmac,
'url' => $this->params->url,
'filePath' => $this->requestImg,
- 'complete' => __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.png'
+ 'complete' => __DIR__ . '/../../cache/img/' . substr($this->params->hmac, 0, 4) . '/' . $this->params->hmac . '_complete.'.$this->fileFormat
]);
$completeDemande = (object)[
diff --git a/app/Soshot/MakeThumb.php b/app/Soshot/MakeThumb.php
index 19bb111..8dbc9dd 100644
--- a/app/Soshot/MakeThumb.php
+++ b/app/Soshot/MakeThumb.php
@@ -28,6 +28,7 @@ class MakeThumb {
private $conf;
private $db;
private $chromePath = '';
+ private $tmpDir = '/tmp/chrome_soshot';
function __construct() {
@@ -111,6 +112,7 @@ class MakeThumb {
'keepAlive' => true,
'headless' => true,
'windowSize' => [1920, 1080],
+ 'userDataDir' => $this->tmpDir,
'customFlags' => [
'--disable-dev-shm-usage',
'--disable-gpu'
@@ -144,12 +146,13 @@ class MakeThumb {
$page->screenshot([
'captureBeyondViewport' => true,
'clip' => $page->getFullPageClip(),
+ 'format' => $this->conf->fileFormat,
])->saveToFile($demande->complete);
if ($demande->type === 'pdf' || $this->conf->alwaysMakePdf === true) {
if ($this->conf->alwaysMakePdf === true) {
$pdfFile = str_replace($demande->type, 'pdf', $demande->filePath);
- $pdfFile = str_replace('png', 'pdf', $pdfFile);
+ $pdfFile = str_replace($this->conf->fileFormat, 'pdf', $pdfFile);
} else {
$pdfFile = $demande->filePath;
}
@@ -187,6 +190,7 @@ class MakeThumb {
'keepAlive' => true,
'headless' => true,
'windowSize' => [1920, 1080],
+ 'userDataDir' => $this->tmpDir,
'customFlags' => [
'--disable-dev-shm-usage',
'--disable-gpu'
diff --git a/app/Utils/Page.php b/app/Utils/Page.php
index 5726f11..82b8034 100644
--- a/app/Utils/Page.php
+++ b/app/Utils/Page.php
@@ -17,8 +17,14 @@ class Page {
}
static function selected($test, $attend) {
- if (in_array($test, $attend)) {
- return 'selected';
+ if (is_array($attend)) {
+ if (in_array($test, $attend)) {
+ return 'selected';
+ }
+ } else {
+ if ($test === $attend) {
+ return 'selected';
+ }
}
}
diff --git a/app/Utils/ShowImg.php b/app/Utils/ShowImg.php
index abf169e..58e8c1f 100644
--- a/app/Utils/ShowImg.php
+++ b/app/Utils/ShowImg.php
@@ -6,11 +6,11 @@ class ShowImg {
private $thumbPath = __DIR__ . '/../../cache/img/';
- function __construct($type, $hmac) {
+ function __construct($type, $hmac, $fileFormat) {
- $img = $this->thumbPath . substr($hmac, 0, 4) . '/' . $hmac . '_' . $type . '.png';
+ $img = $this->thumbPath . substr($hmac, 0, 4) . '/' . $hmac . '_' . $type . '.'.$fileFormat;
- header("Content-type: image/png");
+ header("Content-type: image/$fileFormat");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
if (file_exists($img)) {
diff --git a/datas/config.default.json b/datas/config.default.json
index 5e10bef..c3f2d16 100644
--- a/datas/config.default.json
+++ b/datas/config.default.json
@@ -8,5 +8,6 @@
"expireCache": 12,
"maxGenPerBatch": 5,
"icoSize": 48,
- "chromePath" : ""
+ "chromePath" : "",
+ "fileFormat" : "jpeg"
}
\ No newline at end of file
diff --git a/tpl/nav.php b/tpl/nav.php
index 4b93d46..32a22b9 100644
--- a/tpl/nav.php
+++ b/tpl/nav.php
@@ -17,7 +17,7 @@ use App\Utils\Page;
Infos
Settings
- Logout
+ Logout
diff --git a/tpl/settings.php b/tpl/settings.php
index fbd2e18..0e5b8f4 100644
--- a/tpl/settings.php
+++ b/tpl/settings.php
@@ -24,6 +24,13 @@ use App\Utils\Page;
conf->alwaysMakePdf, true); ?>>
+
+
+
+
@@ -66,7 +73,7 @@ use App\Utils\Page;
-
+