Completed
Push — master ( 8678bd...f43f8a )
by Maxim
07:58
created

PHPThumb::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php namespace Helpers;
2
use Exception;
3
use Spatie\ImageOptimizer\OptimizerChainFactory;
0 ignored issues
show
Bug introduced by
The type Spatie\ImageOptimizer\OptimizerChainFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 */
0 ignored issues
show
Bug introduced by
The type phpthumb was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
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)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
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)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
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