Completed
Push — master ( 189a73...575cdd )
by Nicolas
03:18
created

ImageTranscoder::craft_convert_custom_cmd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 1
eloc 14
nc 1
nop 5
1
<?php
2
3
/**
4
 * This class handled Images transcoding
5
 * Here we the input image
6
 * We transcode and generate an output image using ImageMagic (convert)
7
 */
8
9
10
require_once __DIR__.'/BasicTranscoder.php';
11
12
use SA\CpeSdk;
13
14
class ImageTranscoder extends BasicTranscoder
15
{
16
    /***********************
17
     * TRANSCODE INPUT IMAGE
18
     * Below is the code used to transcode images based on $outputWanted. 
19
     **********************/
20
21
    // $metadata should contain the ffprobe video stream array.
22
23
    // Start Convert for output transcoding
24
    public function transcode_asset(
25
        $tmpPathInput,
26
        $pathToInputFile, 
27
        $pathToOutputFiles,
28
        $metadata = null, 
29
        $outputWanted)
30
    {
31
        if ($metadata) {
32
            // Extract an sanitize metadata
33
            $metadata = $this->_extractFileInfo($metadata);
0 ignored issues
show
Bug introduced by
The method _extractFileInfo() does not seem to exist on object<ImageTranscoder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
        }
35
36
        $this->cpeLogger->log_out(
37
            "INFO", 
38
            basename(__FILE__), 
39
            "Start Transcoding Asset '$pathToInputFile' ...",
40
            $this->activityLogKey
41
        );
42
43 View Code Duplication
        if ($metadata)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            $this->cpeLogger->log_out(
45
                "INFO", 
46
                basename(__FILE__), 
47
                "Input Video metadata: " . print_r($metadata, true),
48
                $this->activityLogKey
49
            );
50
51
        try {
52
            $convertCmd = "";
53
54
            // Update output extension file if it ends with '.*'
55
            // Output will take the same extension as input
56
            $this->_updateOutputExtension(
57
                $pathToInputFile,
58
                $outputWanted);
59
            
60
            // Custom command
61
            if (isset($outputWanted->{'custom_cmd'}) &&
62
                $outputWanted->{'custom_cmd'}) {
63
                $convertCmd = $this->craft_convert_custom_cmd(
64
                    $tmpPathInput,
65
                    $pathToInputFile,
66
                    $pathToOutputFiles,
67
                    $metadata, 
68
                    $outputWanted
69
                );
70
            }
71
            else {
72
                $convertCmd = $this->craft_convert_cmd(
73
                    $tmpPathInput,
74
                    $pathToInputFile,
75
                    $pathToOutputFiles,
76
                    $metadata, 
77
                    $outputWanted
78
                );
79
            }
80
81
            $this->cpeLogger->log_out(
82
                "INFO",
83
                basename(__FILE__),
84
                "CONVERT CMD:\n$convertCmd\n",
85
                $this->activityLogKey
86
            );
87
88
            // Use executer to start Converter command
89
            // Use 'capture_progression' function as callback
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
            // Pass video 'duration' as parameter
91
            // Sleep 1sec between turns and callback every 10 turns
92
            // Output progression logs (true)
93
            $this->executer->execute(
94
                $convertCmd, 
95
                1, 
96
                array(2 => array("pipe", "w")),
97
                array($this, "capture_progression"), 
98
                null, 
99
                true, 
100
                10
101
            );
102
103
            // Test if we have an output file !
104 View Code Duplication
            if (!file_exists($pathToOutputFiles) || 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
                $this->is_dir_empty($pathToOutputFiles)) {
106
                throw new CpeSdk\CpeException(
107
                    "Output file '$pathToOutputFiles' hasn't been created successfully or is empty !",
108
                    self::TRANSCODE_FAIL
109
                );
110
            }
111
112
            // FFProbe the output file and return its information
113
            // XXX: Remove FFprobe for image convertion. Save time
114
            $output_info =
115
                $this->get_asset_info($pathToOutputFiles."/".$outputWanted->{'output_file_info'}['basename']);
116
        }
117
        catch (\Exception $e) {
118
            $this->cpeLogger->log_out(
119
                "ERROR", 
120
                basename(__FILE__), 
121
                "Execution of command '".$convertCmd."' failed: " . print_r($metadata, true). ". ".$e->getMessage(),
122
                $this->activityLogKey
123
            );
124
            return false;
125
        }
126
127
        // No error. Transcode successful
128
        $this->cpeLogger->log_out(
129
            "INFO", 
130
            basename(__FILE__), 
131
            "Transcoding successfull !",
132
            $this->activityLogKey
133
        );
134
135
        return $output_info;
136
    }
137
138
    // Craft command based on JSON input
139
    private function craft_convert_cmd(
140
        $tmpPathInput,
0 ignored issues
show
Unused Code introduced by
The parameter $tmpPathInput is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
        $pathToInputFile,
142
        $pathToOutputFiles,
143
        $metadata, 
0 ignored issues
show
Unused Code introduced by
The parameter $metadata is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
        $outputWanted)
145
    {
146
        $convertArgs = "$pathToInputFile ";
147
148
        if (isset($outputWanted->{'quality'})) {
149
            $quality = $outputWanted->{'quality'};
150
            $convertArgs .= "-quality $quality ";
151
        }
152
        
153
        if (isset($outputWanted->{'resize'})) {
154
            $resize = $outputWanted->{'resize'};
155
            $convertArgs .= "-resize $resize ";
156
        }
157
158
        if (isset($outputWanted->{'thumbnail'})) {
159
            $thumbnail = $outputWanted->{'thumbnail'};
160
            $convertArgs .= "-thumbnail $thumbnail ";
161
        }
162
        
163
        if (isset($outputWanted->{'crop'})) {
164
            $crop = $outputWanted->{'crop'};
165
            $convertArgs .= "-crop $crop ";
166
        }
167
        
168
        // Append output filename to path
169
        $pathToOutputFiles .=
170
            "/" . $outputWanted->{'output_file_info'}['basename'];
171
        
172
        $convertCmd = "convert $convertArgs $pathToOutputFiles";
173
174
        return ($convertCmd);
175
    }
176
    
177
    // Craft custom command
178
    private function craft_convert_custom_cmd(
179
        $tmpPathInput,
0 ignored issues
show
Unused Code introduced by
The parameter $tmpPathInput is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
        $pathToInputFile,
181
        $pathToOutputFiles,
182
        $metadata, 
0 ignored issues
show
Unused Code introduced by
The parameter $metadata is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
        $outputWanted)
184
    {
185
        $convertCmd = $outputWanted->{'custom_cmd'};
186
187
        // Replace ${input_file} by input file path
188
        $pathToInputFile = escapeshellarg($pathToInputFile);
189
        $convertCmd = preg_replace('/\$\{input_file\}/',
190
            $pathToInputFile, $convertCmd);
191
192
        // Append output filename to path
193
        $pathToOutputFiles .= "/" . $outputWanted->{'output_file_info'}['basename'];
194
        // Replace ${output_file} by output filename and path to local disk
195
        $convertCmd = preg_replace('/\$\{output_file\}/',
196
            $pathToOutputFiles, $convertCmd);
197
        
198
        return ($convertCmd);
199
    }
200
201
    // Check if output file need an update on the extension
202
    private function _updateOutputExtension(
203
        $pathToInputFile,
204
        &$outputWanted)
205
    {
206
        $inputExtension =
207
            pathinfo($pathToInputFile)['extension'];
208
        
209
        // REplace output extension if == * with the input extension
210
        $outputPathInfo =
211
            pathinfo($outputWanted->{'output_file_info'}['basename']);
212
        $outputExtension = $outputPathInfo['extension'];
213
        if ($outputExtension == "*") {
214
            $outputWanted->{'output_file_info'}['basename'] = preg_replace(
215
                '/\*/',
216
                $inputExtension,
217
                $outputWanted->{'output_file_info'}['basename']);
218
        }
219
    }
220
}