|
1
|
|
|
<?php namespace Helpers; |
|
2
|
|
|
use Exception; |
|
3
|
|
|
use Spatie\ImageOptimizer\OptimizerChainFactory; |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
include_once(MODX_BASE_PATH . 'assets/snippets/phpthumb/phpthumb.class.php'); |
|
6
|
|
|
require_once(MODX_BASE_PATH . 'assets/lib/Helpers/FS.php'); |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class PHPThumb |
|
10
|
|
|
* @package Helpers |
|
11
|
|
|
*/ |
|
12
|
|
|
class PHPThumb |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** @var \phpthumb */ |
|
|
|
|
|
|
16
|
|
|
private $thumb; |
|
17
|
|
|
/** @var FS */ |
|
18
|
|
|
protected $fs; |
|
19
|
|
|
public $debugMessages = ''; //TODO refactor debug |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* PHPThumb constructor. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->thumb = new \phpthumb(); |
|
27
|
|
|
$this->fs = FS::getInstance(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $inputFile |
|
32
|
|
|
* @param $outputFile |
|
33
|
|
|
* @param $options |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
|
|
public function create($inputFile, $outputFile, $options) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->thumb->sourceFilename = $inputFile; |
|
39
|
|
|
$ext = explode('.', $inputFile); |
|
40
|
|
|
$ext = str_replace('jpeg', 'jpg', strtolower(array_pop($ext))); |
|
41
|
|
|
$options = 'f=' . $ext . '&' . $options; |
|
42
|
|
|
$this->setOptions($options); |
|
43
|
|
|
if ($this->thumb->GenerateThumbnail() && $this->thumb->RenderToFile($outputFile)) { |
|
44
|
|
|
return true; |
|
45
|
|
|
} else { |
|
46
|
|
|
if (!empty($this->thumb->debugmessages)) { |
|
|
|
|
|
|
47
|
|
|
$this->debugMessages = implode('<br>', $this->thumb->debugmessages); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $type |
|
56
|
|
|
*/ |
|
57
|
|
|
public function optimize($file) |
|
58
|
|
|
{ |
|
59
|
|
|
$file = MODX_BASE_PATH . $this->fs->relativePath($file); |
|
60
|
|
|
if (class_exists('Spatie\ImageOptimizer\OptimizerChain') && function_exists('proc_open')) { |
|
61
|
|
|
try { |
|
62
|
|
|
$optimizerChain = OptimizerChainFactory::create(); |
|
63
|
|
|
$optimizerChain->optimize($file); |
|
64
|
|
|
} catch (Exception $e) { |
|
65
|
|
|
if (!empty($this->debugMessages)) { |
|
|
|
|
|
|
66
|
|
|
$this->debugMessages .= '<br>'; |
|
67
|
|
|
} |
|
68
|
|
|
$this->debugMessages .= $e->getMessage(); |
|
69
|
|
|
}; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $options |
|
75
|
|
|
*/ |
|
76
|
|
|
private function setOptions($options) |
|
77
|
|
|
{ |
|
78
|
|
|
$options = strtr($options, array("," => "&", "_" => "=", '{' => '[', '}' => ']')); |
|
79
|
|
|
parse_str($options, $params); |
|
80
|
|
|
if (!is_array($params)) { |
|
|
|
|
|
|
81
|
|
|
$params = array(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
foreach ($params as $key => $value) { |
|
85
|
|
|
$this->thumb->setParameter($key, $value); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getMessages() { |
|
93
|
|
|
return $this->debugMessages; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths